CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    85

    Problems converting a string to a Uint16

    I'm trying to convert std::string to a Uint16 and I'm having problems... Here's what I'm doing...
    Code:
    std::string str = "111111111";
    Uint16 myInt = atoi(str.c_str()); // myInt becomes 27591 !!
    Am I mixing up Uints & ints...strings & CStrings?? I'm sooo confused....

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    What is a Uint16 ???

    is it an unsigned short ? (16 bits)

    if so the range of values it can hold is 0 to 65535

    instead of Unit16, use int and it should work.

  3. #3
    Join Date
    Aug 2002
    Posts
    58
    Just to beat the dead horse,

    (111111111 % 65536) = 27591

    so your code is correct to the precision of an unsigned 16-bit integer. Like Phillip said, if you want more than 65535, you should use an int.

    [edit] Err, well, I guess you would use a Uint32 [/edit]

    Bassman

  4. #4
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    85
    Thanks for the help! Seems I was using the "11111111" for test data in my app and I neglected to take into consideration the 65xxx limit of the 16 bit int. **sigh** Musta been a loooooong day! Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured