Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   casting to string (http://www.go4expert.com/forums/casting-to-string-t2253/)

felisca 15Dec2006 21:59

casting to string
 
Hey guys,
I need to convert double data type to a string. I don't want to convert the value.
That's mean : if I have
double a = 1;
string b = a ;
I want to get b as following:
b.size = 8 (8 bytes)
b[0-6]='\0' and b[7]=(soh)


What is the best way to implement it?
If I didn't explain myself well enought,let me know please- I will do my best :)

Thanks for all
Lena

shabbir 15Dec2006 22:24

Re: casting to string
 
Quote:

Originally Posted by felisca
If I didn't explain myself well enought,let me know please- I will do my best :)

Yup, I am unable to get what conversion you are looking for.

felisca 15Dec2006 23:01

Re: casting to string
 
I want to convert double to string, when binary value stays the same.
Better ?

shabbir 15Dec2006 23:07

Re: casting to string
 
the xxtoa's functions can help you?

DaWei 15Dec2006 23:15

Re: casting to string
 
In the first place, are you sure you want to use a double? A double is not a traditional binary value, but a floating point representation. If you set the value of a double to 1, the binary pattern will not be ....00000000000001. If it's an integer, on the other hand, and you know the value is not going to exceed 127 or 255 (I'm presuming you're dealing with ASCII/ANSI from the 'SOH' usage), then you can simply cast the int to a char (you may get a warning about possible loss of information) and stash it in the last byte of the string. Since a string is represented by a class, it's size is dynamically controlled. You would need to initialize the string to 8 zeroes, then stash the char using the [] operator. Bear in mind that construction of a string from characters is different from constructing a string from a C string (char array). Consequently, if you're initializing with the char '\0', bear that in mind.

If you have something else in mind, try to be clearer. If you don't understand my comment regarding the binary value of a double, then you need to do some investigation. You might start with IEEE 754, although not all implementations conform strictly to that standard.

felisca 15Dec2006 23:27

Re: casting to string
 
Quote:

Originally Posted by shabbir
the xxtoa's functions can help you?

do you mean itoa? No, it represents an integer/double.

felisca 15Dec2006 23:32

Re: casting to string
 
Quote:

Bear in mind that construction of a string from characters is different from constructing a string from a C string (char array). Consequently, if you're initializing with the char '\0', bear that in mind
What is the difference?

tnx

Aztec 16Dec2006 08:33

Re: casting to string
 
Look up std::ostringstream

DaWei 16Dec2006 09:30

Re: casting to string
 
Quote:

What is the difference?
You can construct a string-class string from a C-string directly, as in "string myString ("ABCDEFG");". To construct with a character, however, you must supply a length, as in "string myString (8, '\0');".

DaWei 16Dec2006 09:46

Re: casting to string
 
If you use a string stream to convert an integer to text, you're going to get a textual representation of the digits according to your locale. If the conversion were from 1 to ASCII, for instance, you would get '1' (0x31), not SOH (0x01).


All times are GMT +5.5. The time now is 13:54.