Thread: generic ascii to hex in c

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    22

    generic ascii to hex in c

    Hello everyone
    I have a buffer which contains ascii characters(representing 4 bytes eg AB0400) which I want to convert first two numbers back into hex(0400).I can retrieve hex numbers only if they dont contain numbers.The code below shows this process.

    Code:
    uint8_t buffer[32]; 
    
    receive_buffer(buffer,sizeof(buffer));//now buffer contains ascii characters(eg: AB0400)
    func(((buffer[2]-0x30)<<8)+(buffer[3]-0x30));//retrieves first two ascii characters,converts it into hex and sticks it into the function.
    As seen from above, the code works only for numbers,however, say i had a buffer which contains (AB0A00)what to do then. Therefore I need a more generic routine which can tackle any ascii character.There are lot of notes on the internet ,that confuses me ,So I am looking for something that would suite my requirements.Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amaturequestion
    I have a buffer which contains ascii characters(representing 4 bytes eg AB0400)
    I note that "AB0400" represents 3 bytes, not 4. Perhaps the example input is "00AB0400"?

    Quote Originally Posted by amaturequestion
    I want to convert first two numbers back into hex(0400)
    You need to be clear on this: the numeric string already represents the numbers in hexadecimal representation, so no conversion to hex is necessary: it is already in hexadecimal. What you probably really want to do is to convert them into integer values to be stored in a variable.

    Quote Originally Posted by amaturequestion
    I can retrieve hex numbers only if they dont contain numbers.The code below shows this process.
    The problem is that you have not written the code to account for the alphabetic characters used in canonical hexadecimal notation. Furthermore, instead of using '0', you used the magic number 0x30, i.e., 48. This is a Bad Thing.

    Quote Originally Posted by amaturequestion
    Therefore I need a more generic routine which can tackle any ascii character.There are lot of notes on the internet ,that confuses me ,So I am looking for something that would suite my requirements.
    I suggest that you copy each of the pairs of characters representing a byte into an array of 3 characters, null terminate it, then call strtol with the base set to 16.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    22
    Hello Laserlight

    >Sorry, the example input should have been 00AB0400
    >The buffer actually contains ascii characters but for understanding purpose I have written the hex numbers. Actually the four bytes are transmitted using serial communication and received in the buffer(where it is in ascii).
    Once I receive the buffer I need the first two bytes only(0400) and the other two gets discarded. (00AB)
    >yes, you are absolutely right.my code snippet only converts (some ascii characters)-->hex(0400)

    but i am looking for a function which would convert any ascii characters to hex characters.
    Is there a way of using sprintf to achieve the same.sorry for these silly questions.thanks for the support

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amaturequestion
    but i am looking for a function which would convert any ascii characters to hex characters.
    Sorry, but that does not make sense. If ASCII or a derivative is in use, then "hex characters" are "ASCII characters". I repeat:
    Quote Originally Posted by amaturequestion
    You need to be clear on this: the numeric string already represents the numbers in hexadecimal representation, so no conversion to hex is necessary: it is already in hexadecimal. What you probably really want to do is to convert them into integer values to be stored in a variable.
    In fact, this is precisely what your code on this line does:
    Code:
    func(((buffer[2]-0x30)<<8)+(buffer[3]-0x30));//retrieves first two ascii characters,converts it into hex and sticks it into the function.
    except that the comment is wrong: it does not "convert it into hex", but rather converts it to an integer value. This integer value can be printed in decimal representation, hexadecimal representation, etc.

    Quote Originally Posted by amaturequestion
    but i am looking for a function which would convert any ascii characters to hex characters.
    The requirement of "any ascii characters" is unreasonable: would you expect the function to convert "amaturequestion" to an integer value? Perhaps you would, but then what value should it be? 0? 10 (i.e., 0xa)?

    Quote Originally Posted by amaturequestion
    Is there a way of using sprintf to achieve the same.sorry for these silly questions.thanks for the support
    I have already suggested to you the use of strtol.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Tags for this Thread