0

Say I have a text file containing the 80bit hex number

0xabcdef0123456789abcd

My C++ program reads that using fstream into a char array called buffer. But then I want to store it in a uint16_t array such that:

uint16_t * key = {0xabcd, 0xef01, 0x2345, 0x6789, 0xabcd}

I have tried several approaches, but I continue to get decimal integers, for instance:

const std::size_t strLength = strlen(buffer);
std::vector<uint16_t> arr16bit((strLength / 2) + 1);

for (std::size_t i = 0; i < strLength; ++i)
{
    arr16bit[i / 2] <<= 8;
    arr16bit[i / 2] |= buffer[i];
}

Yields:

arr16bit = {24930, 25444, 25958, 12337, 12851}

There must be an easy way to do this that I'm just not seeing.

Here is the full solution I came up with based on the comments:

int hex_char_to_int(char c) {
    if (int(c) < 58) //numbers
        return c - 48;
    else if (int(c) < 91) //capital letters
        return c - 65 + 10;
    else if (int(c) < 123) //lower case letters
        return c - 97 + 10;
}
uint16_t ints_to_int16(int i0, int i1, int i2, int i3) {
    return (i3 * 16 * 16 * 16) + (i2 * 16 * 16) + (i1 * 16) + i0;
}

void readKey() {
    const int bufferSize = 25;
    char buffer[bufferSize] = { NULL };
    ifstream* pStream = new ifstream("key.txt");
    if (pStream->is_open() == true)
    {
        pStream->read(buffer, bufferSize);
    }
    cout << buffer << endl;
    const size_t strLength = strlen(buffer);
    int* hex_to_int = new int[strLength - 2];
    for (int i = 2; i < strLength; i++) {
        hex_to_int[i - 2] = hex_char_to_int(buffer[i]);
    }
    cout << endl;
    uint16_t* key16 = new uint16_t[5];
    int j = 0;
    for (int i = 0; i < 5; i++) {
        key16[i] = ints_to_int16(hex_to_int[j++], hex_to_int[j++], hex_to_int[j++], hex_to_int[j++]);
        cout << "0x" << hex << key16[i] << " ";
    }
    cout << endl;
}

This outputs:

0xabcdef0123456789abcd

0xabcd 0xef01 0x2345 0x6789 0xabcd
Steve Zee
  • 71
  • 5
  • Shouldn't you use `i / 4` if you want to use four characters from the text file for each element in `arr16bit`? – mkrieger1 Feb 20 '21 at 11:56
  • Also what happens with the characters `'0'` and `'x'` at the beginning of the text file? I don't see them being ignored anywhere in the code. – mkrieger1 Feb 20 '21 at 11:56
  • `a` is ascii `0x61`. `b` is ascii `0x62`. `0x6162` = 24930. Consider what you are doing. Chars aren't integers. – J... Feb 20 '21 at 11:57
  • 1
    And where do you convert characters like `'a'` or `'5'` to their actual numeric values 10 and 5? – mkrieger1 Feb 20 '21 at 11:57
  • Is this a binary file, or is that ASCII hex dumped data? Is it *literally* `"0x6162..."`? – tadman Feb 20 '21 at 12:04
  • 1. tadman I'm not sure; I may have an endian issue 2. mkrieger1 i/4 might be the right idea there 3. mkrieger1 I have ignored '0' and 'x in my ifstream read. 4. J you are correct that it is converting my chars to ints, but how can I avoid that? 5. mkrieger1 I'm not doing that, so maybe that is the way to fix it? 6. tadman it is a regular text file, literally 0xab... – Steve Zee Feb 20 '21 at 12:10
  • @SteveZee It's not `0xab`. The ascii character `a` is entirely unrelated to the hexadecimal value `0xa`. The ascii character `a` is an entire byte and has value `0x61`. I think if you spend some time learning about character and numeric formats this would all make much more sense. Feels like trying to run before you can walk. – J... Feb 20 '21 at 12:59
  • [Convert hex string (char) to int?](https://stackoverflow.com/q/10156409/327083) – J... Feb 20 '21 at 13:04
  • [How do I get a part of the a string in C++?](https://stackoverflow.com/q/2498119/327083) – J... Feb 20 '21 at 13:06
  • [Convert char to int in C and C++](https://stackoverflow.com/q/5029840/327083) – J... Feb 20 '21 at 13:07
  • J - I understand the difference between an ascii character and a hex value, I just thought there might be a way to read it as a literal hex value, but since that doesn't seem to be the case I am just going to add a step and convert the characters to integers corresponding to their literal hex values. – Steve Zee Feb 20 '21 at 13:12
  • @SteveZee Read the links. I'd think [`strol`](https://pubs.opengroup.org/onlinepubs/7908799/xsh/strtol.html) would be preferred. – J... Feb 20 '21 at 14:15

0 Answers0