2

I have a Hexadecimal-String looking like this:

char hexString = "1a";

and i want to convert it to a BYTE, so that it looks like this:

BYTE Anything[0x10] = { 0x1a };

after the converion. i need to do this for 16 strings so that i have sth looking like this at the end

BYTE Anything[0x10] = { hexToByte(hexString1), hexToByte(hexString2), 16 times };

any idea, because i have no clue on how to do this!

N.W.A
  • 75
  • 1
  • 10
  • Just like with decimals: repeated addition and multiplication, but multiplying with 16 instead of 10. If you want to do it by hand. (Note that `0x1a` is the same as `26`.) – molbdnilo Dec 19 '15 at 13:44
  • 2
    [`std::stoul`](http://en.cppreference.com/w/cpp/string/basic_string/stoul) – Brian Rodriguez Dec 19 '15 at 13:45

2 Answers2

1
BYTE Anything[0x10] = {
    (BYTE)std::stoul(hexString1, nullptr, 16),
    (BYTE)std::stoul(hexString2, nullptr, 16), ... };
Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20
0

There's only 256 values.... plenty small enough to look for in an array.

int hexToByte(const char* strHex)
{
   char* array[] = {"00", "01", "02", "03", "04", "05", "06", "07",
                    "08", "09", "0A", "0B", "0C", "0D", "0E", "0F",
                    "10", "11", "12", "13", "14", "15", "16", "17",
                    "18", "19", "1A", "1B", "1C", "1D", "1E", "1F"
                   /* Fill in the rest, up to FF */};

    int i;              
    for(i=0; strcmp(strHex,array[i]); ++i) ;

    return i;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159