www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - BigInt.toString

reply Ruby The Roobster <rubytheroobster yandex.com> writes:
How exactly can one store the string representation of a BigInt?  
The seemingly obvious

```d
//...
dchar[] ret; //dchar[] is necessary for my project
//Assume that val is a BigInt with a value set earlier:
val.toString(ret, "%d");
//...
```

doesn't work.  I am using x86_64 windows with -m64, and 
v2.100.1(LDC2 1.30.0).
Aug 03 2022
next sibling parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Thu, Aug 04, 2022 at 12:45:44AM +0000, Ruby The Roobster via
Digitalmars-d-learn wrote:
 How exactly can one store the string representation of a BigInt?  The
 seemingly obvious
 
 ```d
 //...
 dchar[] ret; //dchar[] is necessary for my project
 //Assume that val is a BigInt with a value set earlier:
 val.toString(ret, "%d");
 //...
 ```
 
 doesn't work.  I am using x86_64 windows with -m64, and v2.100.1(LDC2
 1.30.0).
Don't call .toString directly. Instead, use std.format.format: ```d import std; void main() { auto x = BigInt("123123123123123123123123123123123123"); string s = format("%s", x); // this gives you the string representation writeln(s); // prints "123123123123123123123123123123123123" // If you need to convert to dchar[]: dstring ds = s.to!(dchar[]); // Or if you want a direct formatting into dchar[] without going // through a string intermediate: auto app = appender!(dchar[]); app.formattedWrite("%s", x"); dchar[] dcharArray = app.data; // now you have your dchar[] } ``` Hope this is clear. T -- Which is worse: ignorance or apathy? Who knows? Who cares? -- Erich Schubert
Aug 03 2022
parent Ruby The Roobster <rubytheroobster yandex.com> writes:
On Thursday, 4 August 2022 at 01:05:31 UTC, H. S. Teoh wrote:
 Don't call .toString directly. Instead, use std.format.format:

 ```d
 import std;
 void main() {
 	auto x = BigInt("123123123123123123123123123123123123");
 	string s = format("%s", x); // this gives you the string 
 representation
 	writeln(s); // prints "123123123123123123123123123123123123"

 	// If you need to convert to dchar[]:
 	dstring ds = s.to!(dchar[]);

 	// Or if you want a direct formatting into dchar[] without 
 going
 	// through a string intermediate:
 	auto app = appender!(dchar[]);
 	app.formattedWrite("%s", x");
 	dchar[] dcharArray = app.data; // now you have your dchar[]
 }
 ```

 Hope this is clear.


 T
Thank you. This worked.
Aug 03 2022
prev sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Thursday, 4 August 2022 at 00:45:44 UTC, Ruby The Roobster 
wrote:
 How exactly can one store the string representation of a 
 BigInt?  The seemingly obvious:
 dchar[] is necessary for my project.
 Assume that val is a BigInt with
 a value set earlier:
 ```d
 val.toString(ret, "%d");
 ```
 doesn't work.
I guess I wrote the following anything like that you want. ```d void main() { import std.bigint, std.string : representation; BigInt i = 1001; auto val = i.to!(dchar[]); assert(val.representation == [49, 48, 48, 49]); } ``` Is that what you want? SDB 79
Aug 03 2022
parent Ruby The Roobster <rubytheroobster yandex.com> writes:
On Thursday, 4 August 2022 at 01:32:15 UTC, Salih Dincer wrote:
 I guess I wrote the following anything like that you want.

 ```d
 void main()
 {
   import std.bigint,
          std.string : representation;

   BigInt i = 1001;
   auto val = i.to!(dchar[]);
   assert(val.representation == [49, 48, 48, 49]);
 }
 ```

 Is that what you want?

 SDB 79
Not exactly. Anyways, it has already been solved.
Aug 03 2022