0

I have a program that takes the contents of a file and convert it into a hex. I would like to take those hex values and store them as elements of an unsigned char array I've called hexData. How would I go about this?

I've tried converting the hex data into a string literal (for example 0x5A) and casting it to a char*, but when I try running the code nothing happens (no output, no errors). It also seems like a very inefficient way of doing this. I've left the code in to see what I mean.


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

typedef unsigned char      BYTE;
typedef unsigned short int WORD;

int main()
{

    ifstream ifd("C:\\Users\\Shayon Shakoorzadeh\\Documents\\test.txt", ios::binary | ios::ate);
    int size = ifd.tellg();
    ifd.seekg(0, ios::beg);
    vector<char> buffer;
    buffer.resize(size);
    ifd.read(buffer.data(), size);

    static BYTE hexData[100000] =
    {

    };

    string tempStr;
    char *tempChar;

    const int N = 16;
    const char hex[] = "0123456789ABCDEF";
    char buf[N*4+5+2];

    for (int i = 0; i < buffer.size(); ++i)
    {
        int n = i % N;

        // little endian format
        if (i > 0 && i < buffer.size()-1)
            swap(buffer[i], buffer[i+1]);

        // checks to see if at the end
        if (n == 0)
        {
            memset(buf, 0x20, sizeof(buf));
            buf[sizeof(buf) - 3] = '\n';
            buf[sizeof(buf) - 1] = '\0';
        }
        unsigned char c = (unsigned char)buffer[i];

        // storing the hex values
        buf[n*3+0] = hex[c / 16];
        buf[n*3+1] = hex[c % 16];

        //encoded buffer
        buf[3*N+5+n] = (c>=' ' && c<='~') ? c : '.';

        //my attempt
        tempStr = "0x" + string(1, hex[c / 16]) + string(1, hex[c % 16]);
        tempChar = (char*)alloca(tempStr.size() + 1);
        memcpy(tempChar, tempStr.c_str(), tempStr.size() + 1);
        strcpy( (char*) hexData[i] , tempChar);


     }
 }

The static BYTE hexData[] array would need to have values that look like:

{0x5A, 0x00, 0x7F, 0x90, 0x8A,...} and so on

Any help would be greatly appreciated!

EDIT:

Let me specify, I am having trouble STORING these values into my array called "hexData." I am already getting the hex values from my file, but only as a string. I want to convert these strings into unsigned chars so they can be put into my hexData array. Sorry my original question was worded so poorly, but I do not believe the pointed to duplicate question answers my question.

  • Why do you want to convert values to hex? –  Jul 24 '18 at 20:07
  • All integers are hex, or binary, or base 10, or base-whatever-you-want-to-use. You just have to look at them differently. [Take a look at the `std::hex` manipulator](https://en.cppreference.com/w/cpp/io/manip/hex) for an easy way to print a number in hex. – user4581301 Jul 24 '18 at 20:14
  • Is there any reason you're not using [std::stringstream](http://www.cplusplus.com/reference/sstream/stringstream/)? – dgnuff Jul 24 '18 at 20:21
  • No duplication with the pointed answer. OP question is general, & doesn't restrict to an ostream solution. – Amit G. Jul 24 '18 at 22:56
  • @NeilButterworth I have another function that will take those unsigned chars and do a return a crc-16 value. I did not include that function in this code though. – Shayon Shakoorzadeh Jul 24 '18 at 23:55
  • There is no reason for converting them to hex in order to do that - you do not seem to understand how values are represented in binary inside a computer. –  Jul 25 '18 at 00:04
  • You are correct, memory is a very new thing for me. But the files I am working with are encoded (usually .hex format) and I have to convert that to a hex output anyway as another functionality for my program, which the given program already does. Could you please explain a better way to do this? – Shayon Shakoorzadeh Jul 25 '18 at 00:21

1 Answers1

0

This is how to respresent in hexadecimal a byte value

Helpful format specifiers are as follows: %x - output as hexadecimal, ex: 2d .. %02x - pad with zeroes, 2 chars min, ex: 0e .. "0x%02x" representation: 0x00.

void byte_to_hex_string(unsigned char decimal) // Print Hexadecimal
{
    if (decimal < 256)
    {
        char hexstring[8]{ 0 };
        sprintf(hexstring, "0x%02x", decimal);
        printf("%s\n", hexstring);
    }
}

int main()
{
    byte_to_hex_string('A');

    return 0;
}
Amit G.
  • 2,546
  • 2
  • 22
  • 30