Thread: How to change number data from base 256 to base 16?

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    Smile How to change number data from base 256 to base 16?

    If I have these below data (base 256)

    Code:
    data[0] = 154; // 9A in base 16
    data[1] = 203; // CB in base 16
    data[2] = 15;   // 0F in base 16
    data[3] = 54;   // 38in base 16
    data[4] = 248; // F8 in base 16
    data[5] = 175; // AF in base 16
    and I want to change data(base 256) to Ctring which have format in base 16 character

    Code:
    CString m_data;
    m_data = "9ACB0F38F8AF";
    Do you know how to do that?
    Thank you for your answer.

  2. #2
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    I think you mean you want to convert from base 10 to base 16...
    where the base 10 number is between 0 and 255
    I'll be back in a moment with code for that...
    Last edited by SPiRiToFCaT; 11-05-2002 at 12:10 AM.
    - Well that's my 4c Australian.

  3. #3
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    This should work.
    It's not the most efficient, but it should be fairly easy to understand
    Code:
    // converting the integer stored in data[i] to the for AB in hex.
    
    int temp = data[i];
    char first, second;
    int units = temp % 16; // Units now holds an integer 0 to 15, this is the B part
    temp = temp-units;
    temp = temp*0.1;
    temp = temp % 16; // Temp now holds an integer 0 to 15, this is the A part
    // Now convert the two integers to characters.
    switch(units)
    {
     case 0: second = '0'; break;
     case 1: second = '1'; break;
    // Other cases go in between
     case 9: second = '9'; break;
     case 10: second = 'A'; break;
    // More cases in here
     case 15: second = 'F'; break;
    }
    switch(temp)
    {
     // Same as before but into first.
    }
    cout<<data[i]<<" is "<<first<<second<<" in hex.";
    // Do some code to put first and second into your cstring.
    - Well that's my 4c Australian.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in 16 and 24-bit audio data into (32-bit) integer buffers
    By theblindwatchma in forum C Programming
    Replies: 2
    Last Post: 04-13-2008, 11:12 PM
  2. Virtual Data Base
    By aukh in forum C++ Programming
    Replies: 3
    Last Post: 04-04-2006, 05:58 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. Replies: 2
    Last Post: 11-08-2002, 03:22 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM