base conversion

Discussion in 'OT Technology' started by BaZ, Mar 11, 2003.

  1. BaZ

    BaZ 2004 ACC Champions

    Joined:
    Jun 12, 2001
    Messages:
    2,005
    Likes Received:
    0
    Location:
    Hokieville, USA
    ok, i am using a char to represent a single byte for writing to a binary file. is there a function that will take in a char, look at it on a bitwise basis, and return its hex or binary value?

    or, is there a function that will take in a in integer as its value and return the binary of hex value?
     
  2. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    in what language?
     
  3. BaZ

    BaZ 2004 ACC Champions

    Joined:
    Jun 12, 2001
    Messages:
    2,005
    Likes Received:
    0
    Location:
    Hokieville, USA
    c++
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    what do you mean by returning a binary or hex value?
    Do you want to take a number and get a string representation of its value in hex or binary?
     
  5. crotchfruit

    crotchfruit Guest

    here is how to convert a char to a 2-digit hex string:

    char myhexval = 16;
    char myhexstring[3];
    sprintf(myhexstring, "%02X", myhexval);

    this will store "10" in myhexstring.

    you can also use this with fprintf using the same %02X key. the 'X' means use the uppercase hex version of the value. a 'x' means use the lowercase hex version (a vs. A). the '02' means use 2-digits minimum, and if it's only 1 digit (like the value 4) append a 0 to the front of it to make it 2 digits.

    there is no such simple method for converting to binary representation, however, you can get individual bits by:

    int bitvalue(int value, int bit)
    {
    return (value >> bit) & 1;
    }

    and then construct your own string.
     

Share This Page