casting to string

felisca's Avatar, Join Date: Dec 2006
Go4Expert Member
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
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
felisca's Avatar, Join Date: Dec 2006
Go4Expert Member
I want to convert double to string, when binary value stays the same.
Better ?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
the xxtoa's functions can help you?
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.
0
felisca's Avatar, Join Date: Dec 2006
Go4Expert Member
Quote:
Originally Posted by shabbir
the xxtoa's functions can help you?
do you mean itoa? No, it represents an integer/double.
0
felisca's Avatar, Join Date: Dec 2006
Go4Expert Member
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
0
Aztec's Avatar, Join Date: May 2006
Contributor
Look up std::ostringstream
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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');".
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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).