itoa - going back to char.

This is a discussion on itoa - going back to char. within the C++ Programming forums, part of the General Programming Boards category; OK, I was expecting to see 3 integers converted to characters and then the characters trunckated together back into an ...

  1. #1
    Registered User Ray Schmidt's Avatar
    Join Date
    Feb 2003
    Posts
    17

    Question itoa - going back to char.

    OK, I was expecting to see 3 integers converted to characters and then the characters trunckated together back into an integer (like this 12 34 56 to 123456) How far off am I here?


    char string2[8] = {0,0,0,0,0,0,0,0};
    char gbf[2] = {0,0};
    char gbi[2] = {0,0};
    char gbs[2] = {0,0};

    feet = abs(workc);
    inch = abs((workc-feet)*12);
    six = abs((((workc-feet)*12)-inch)*16);

    itoa(feet,gbf,2);
    itoa(inch,gbi,2);
    itoa(six,gbs,2);
    sprintf(string2,"%2.d%2.d%2.d", &gbf, &gbi, &gbs);

    m_feet2 = atoi(string2);

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The last argument in itoa is the radix of the outputted number. Using 10 will give you a decimal number (the 'normal' kind). 2, like you've typed, will give you a binary number (1010101110101) which may not be what you expect...

    Also, remember that strings contain a NULL terminator so if you have 2 characters i it, it must be 3 characters wide.

    Oh, and the easiest way would be to add the integers first, then transform them into a string. Integer addition is a lot easier to perform than string concatenation.
    Code:
    int Total = (feet * 10000) + (inch * 100) + six
    itoa(Total, Buffer, 10);
    Assuming they are max 2 digits long each.
    Last edited by Magos; 02-08-2003 at 09:57 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  2. lvalue error trying to copy between structures
    By emanresu in forum C Programming
    Replies: 2
    Last Post: 11-16-2006, 06:53 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 10:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21