string to actual hex value conversion

This is a discussion on string to actual hex value conversion within the C Programming forums, part of the General Programming Boards category; I'm pretty new to programming c and trying to get from ascii 0x39, 0x39 (99) to the actual hex value ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    string to actual hex value conversion

    I'm pretty new to programming c and trying to get from ascii 0x39, 0x39 (99) to the actual hex value 0x63. I've searched many fora but couldn't find a good solution yet. For as far as I know there is no standard function to do this? I can get it to the string 0x36, 0x33 or to an integer, but the actual value 0x63 I don't know how to get. Hope you guys can point me in the right direction

    I have been trying this:
    Code:
    ascii f[2] = {0x39,0x39}; //0x39 , 0x39 , 0x39 , 0x39 , 0x2D , 0x31 , 0x32 , 0x2D , 0x33 , 0x31
    char y[2];
    int o;
    
    int hj = atoi(&f[0]);
    int jj = atoi(&f[1]);
    
    o=jj+10*hj;
    
    sprintf(y,"%x", o);
    I guess I should write a conversion function, but I don't know yet where to start..
    Last edited by Lens-art; 03-16-2012 at 05:00 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,681
    You probably want to use sscanf to convert your hex string into a numerical value. Once a number you can then print it out in decimal/hex/oct with a printf call.
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    I have a string with numbers which I have to convert to the hex value because the other side expects this value.
    I got this which works now does this seem a good solution?

    Code:
    ascii f[2] = {0x39,0x39};
    char gh[1];
    int hj = atoi(&f[0]);
    
        for(i = 0; i < 100; i++){
            if(hj==i){
                gh[0] = (char)i;
                break;
            }
        }
    Last edited by Lens-art; 03-16-2012 at 05:43 AM.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    The whole for loop was not necessary off course I was just looking for the (char)hj; which I didn't knew before

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    339
    I don't think you're understanding what you have to do. Try printf("%x", atoi(f)) and see how close that gets to what you're hoping to see. If it's different, explain what you hoped the answer would come out as.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    yes this is somewhat embarrassing it is what I meant, I am developing an open AT application and it was not the standard library so I had to add the right library. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Conversion
    By mcertini in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2011, 09:26 PM
  2. String Conversion
    By 6kyAngel in forum C Programming
    Replies: 2
    Last Post: 03-17-2009, 07:59 AM
  3. std::string conversion
    By MadCow257 in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2006, 06:16 PM
  4. string to int conversion
    By app in forum C Programming
    Replies: 4
    Last Post: 08-31-2005, 06:04 PM
  5. string object to null-terminating string conversion
    By sayz0 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2001, 11:15 AM

Tags for this Thread


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