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
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.
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');".
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).
Thank you very much!!!!!!!!!! I think I've done it. One more question: Regarding double and int. Actually I don't need double, but I need 64 bit int. Do I have an option to allocate it?
In C99, there is long long integer types, which are binary integer types at least 64 bits wide. C++ doesn't have this. You'll have to go for dynamic memory allocation or better why not use vector container. That will take care of all your shrinking and expanding of memory.
You need to make up your mind what the hell you want. I quote you: I have told you how to do that. Since there is only one content byte, you have no earthly use for a long long or an int 64 -- unless that is the particular method you wish to use to hold it. If that's the case, you shouldn't have stated that you want it in a string-class string (which is every bit as effective as a vector, in this case). It is impossible to tell whether you don't know what you want, or whether you just can't express it adequately. I suggest you back up a couple of steps, rethink your requirements, express them clearly, and post the outcome here.
Ok, I see that I didn't explain myself well enought. Sorry, that is since my poor English I will try again First of all the example I gave - was only an example. In my case all 8 bytes could have a value. I'm trying to write a function for MD5 hash function http://en.wikipedia.org/wiki/MD5 as mentioned in wikipedia. I was in a trouble with last 64 bits. That is what my question was about. That is why I have choosen double. Because I need 64 bits and all of them are usefull. What can I do? Thanks!
Despite the fact that there may not (yet) be an official long long type for C++, you can be sure that any reasonable compiler will support it by one name or another. That's what I would use, in unsigned form. I didn't read your link, and I don't know what you're trying to do, but a dollar to a doughnut says that an unsigned long long will fit the bill. Your original post was essentially a detriment to getting your problem solved. SOH hasn't a damned thing to do with it, apparently, except to mistakenly imply byte-sized entities. Specifying 7 other byte-sized entities ('\0') was just more mud in the water. Since my crystal is in the shop for ball joints, you might consider, next time, that we can't read your mind.