1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

casting to string

Discussion in 'C' started by felisca, Dec 15, 2006.

  1. felisca

    felisca New Member

    Joined:
    Dec 15, 2006
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Yup, I am unable to get what conversion you are looking for.
     
  3. felisca

    felisca New Member

    Joined:
    Dec 15, 2006
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    I want to convert double to string, when binary value stays the same.
    Better ?
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    the xxtoa's functions can help you?
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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.
     
  6. felisca

    felisca New Member

    Joined:
    Dec 15, 2006
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    do you mean itoa? No, it represents an integer/double.
     
  7. felisca

    felisca New Member

    Joined:
    Dec 15, 2006
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    What is the difference?

    tnx
     
  8. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    Look up std::eek:stringstream
     
  9. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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');".
     
  10. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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).
     
  11. felisca

    felisca New Member

    Joined:
    Dec 15, 2006
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  12. felisca

    felisca New Member

    Joined:
    Dec 15, 2006
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Yes, that is not what I need.
     
  13. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  14. felisca

    felisca New Member

    Joined:
    Dec 15, 2006
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Thank you!
    I will try it.
    I'm new in c++,so I need to explore all these options.
     
  15. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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.
     
  16. felisca

    felisca New Member

    Joined:
    Dec 15, 2006
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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!
     
  17. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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.
     

Share This Page