开发者

C++ char hex value to byte

开发者 https://www.devze.com 2023-03-28 11:41 出处:网络
I have a hex value in a char pointer (for example \'F3\'), and I would like to convert it to byte, because I want it to put in an array. I know that there are many solutions, but they are not what I w

I have a hex value in a char pointer (for example 'F3'), and I would like to convert it to byte, because I want it to put in an array. I know that there are many solutions, but they are not what I wanted.

Thanks in advance!

EDIT:

Okay, maybe I have not written everything. What I have now:

char aChar[5];
itoa (j, aChar, 16);

j is now 3, and I j开发者_JS百科ust want it in byte. Atoi, scanf doesn't help, those are the different solutions.


Since you've tagged this C++ and not C, I'm not going to use any C functions (except assert() to demonstrate the behaviour, edge conditions, et cetera). Here's a sample file. Let's call it hex2byte.cpp:

#include <sstream>
#include <cassert>

unsigned char hex2byte(const char* hex)
{
    unsigned short byte = 0;
    std::istringstream iss(hex);
    iss >> std::hex >> byte;
    return byte % 0x100;
}

int main()
{
    const char* hex = "F3";
    assert(hex2byte(hex) == 243);
    assert(hex2byte("") == 0);
    assert(hex2byte("00") == 0);
    assert(hex2byte("A") == 10);
    assert(hex2byte("0A") == 10);
    assert(hex2byte("FF") == 255);
    assert(hex2byte("EEFF") == 255);
    assert(hex2byte("GG") == 00);
    assert(hex2byte("a") == 10);
    assert(hex2byte("0a") == 10);
    assert(hex2byte("f3") == 243);
    assert(hex2byte("ff") == 255);
    assert(hex2byte("eeff") == 255);
    assert(hex2byte("gg") == 00);
}

Make it:

% make hex2byte
g++ -Wall -Wextra -Wshadow -pedantic -Weffc++ -Werror hex2byte.cpp -o hex2byte

Run it:

% ./hex2byte

No assertions. Add error handling to taste (such as checking for when hex == NULL, et cetera).


A byte is usually simply an unsigned char

myArray[n] = (unsigned char)*p;

Or do you mean that you have a string representation of an hex value?


Given a char * with "F3":

char *hexstr = "F3";

Then you can do this:

unsigned char byteval =
    (((hexstr[0] >= 'A' && hexstr[0] <= 'Z') ? (10 + hexstr[0] - 'A') : 
    (hexstr[0] >= 'a' && hexstr[0] <= 'z') ? (10 + hexstr[0] - 'a') : 
    (hexstr[0] >= '0' && hexstr[0] <= '9') ? (hexstr[0] - '0') : 0) << 4) |
    ((hexstr[1] >= 'A' && hexstr[1] <= 'Z') ? (10 + hexstr[1] - 'A') : 
    (hexstr[1] >= 'a' && hexstr[1] <= 'z') ? (10 + hexstr[1] - 'a') : 
    (hexstr[1] >= '0' && hexstr[1] <= '9') ? (hexstr[1] - '0') : 0);

I'm sorry for its ugliness; I'm sure it can be improved.

You can turn this into a function:

inline unsigned char hextobyte(const char *s) {
    return
    (((s[0] >= 'A' && s[0] <= 'Z') ? (10 + s[0] - 'A') : 
    (s[0] >= 'a' && s[0] <= 'z') ? (10 + s[0] - 'a') : 
    (s[0] >= '0' && s[0] <= '9') ? (s[0] - '0') : 0) << 4) |
    ((s[1] >= 'A' && s[1] <= 'Z') ? (10 + s[1] - 'A') : 
    (s[1] >= 'a' && s[1] <= 'z') ? (10 + s[1] - 'a') : 
    (s[1] >= '0' && s[1] <= '9') ? (s[1] - '0') : 0);
}


I can think of at least five ways:

  1. using sscanf with %x
  2. using strtol with the correct base
  3. using istringstream (though you'll have to down cast from unsigned short to unsigned char)
  4. boost::spirit
  5. hand rolled loop to parse character-by-character

None of the ways you listed would work. But your question still isn't really clear, you have a char which somehow you converted to hex using itoa and now you want to convert to a byte!?! what's wrong with a cast? e.g. unsigned char byte = static_cast<unsigned char>(charvalue);

0

精彩评论

暂无评论...
验证码 换一张
取 消