3

If I have an array such as this

unsigned char data[2850] = "";

that after running through a part of my program will eventually be full of byte elements such that if I loop through it and log it out I will see the following

    for (size_t i= 0; i < sizeof(data); i++) {
        printf("%02X ", data[i]);
    }

    //log window shows 01 24 81 00 0E 1C 44 etc...

Is there a way to take all the elements of the data array and put them into a const char? Or just one large string? Hopefully I'm being clear enough. My goal is to eventually turn all the elements of this data array into a base64 encoded string that I can use else where in the program.

Stavros_S
  • 2,145
  • 7
  • 31
  • 75
  • 1
    What do you mean? Are you implying the array isn't null terminated and you want it to be? Except it appears to contain nulls. Please be more precise in what you want. – ikegami Mar 07 '14 at 20:23
  • You want the string representation of the Hex values? Please be specific. – OldProgrammer Mar 07 '14 at 20:23
  • 4
    are those hex values, `00` is really `0x00`? If so, you CAN'T use it as a string, because C uses hex 0 as the string terminator. – Marc B Mar 07 '14 at 20:23
  • Is your goal to print them, or manipulate them? If you just want to print them you don't need to convert this to a string first. – Mike Mar 07 '14 at 20:27
  • 2
    You will be more likely to get a sting if the array contains more B's – pat Mar 07 '14 at 20:27
  • @pat - That was funny, LOL :-) – trumpetlicks Mar 07 '14 at 20:28
  • declaring the variable `data[2850]` causes the compiler to add 2850 bytes to the stack. This memory is initially random garbage (uninitialized). However, you then initialize the variable with ` = "" ` This initialization copies an empty string into `data`. However, an empty string in memory is represented by the single byte, 0. This is the 'null terminator'. The data after the null terminator is not technically part of the string, just space that has not been initialized. – problemPotato Mar 07 '14 at 20:31
  • @OldProgrammer I have updated the code example – Stavros_S Mar 07 '14 at 20:35
  • I guess he wants to code it base16, e.g. "012481000E1C44". If it's this, you don't need this step to convert to base64, do it directly from the array. – m0skit0 Mar 07 '14 at 20:37
  • @MarcB they are hex values that are being returned from a device, I essentially send a report array of hex values(bytes) to the device and it responds back with the same format. – Stavros_S Mar 07 '14 at 20:37

3 Answers3

3

A string (char array) in C is a sequencial sequence of chars terminated by a sentianal character (the null terminator: '\0')

This means that if you have a byte of the value 0x00 anywhere in your array, it has thusly terminated the "string" potentially before the end of the sequence. In your example if you converted your data array into a string the end of it would be the first element:

data[]{00, EB, 23, EC, FF, etc... };

Now if you want to make a string out of the values of the data in here, you can do that with sprintf(), for example:

unsigned char val = 00;
data[0] = val;
char dump[5] = {0};

sprintf(dump, "%02x", data[0]);

Now you have a string with the value "00" in it, You can make it fancer with something like:

sprintf(dump, "0x%02x", data[0]);

So that it has "0x00" to show it off as hex data.

You could loop something like this to give the full string... but keep in mind that the printable string needs to be at least 2x+1 the size of the data string (2x for ASCII and +1 for null), and you have to fill the new string in steps. Example:

unsigned char data[5] = {0x00, 0x12, 0xB3, 0xFF, 0xF0};
char printstr[11] = {00};

for(int x = 0; x < 5; x++)
    sprintf(printstr+(x*2), "%02x", data[x]);
printstr[10] = '\0';

Now printstr has "0012b3fff0"

Mike
  • 47,263
  • 29
  • 113
  • 177
1

you can use sprintf (note if you sprintf past the end of the 'str' array, you will have a buffer overflow):

//malloc 2 chars for each byte of data (for 2 hex digits)
char *str = malloc(sizeof(char) * 3 * (sizeof(data) + 1));

//this var will point to the current location in the output string
char *strp = str;

if (NULL == str)
{
    return false; //memory error
}

for (size_t i= 0; i < sizeof(data); i++) {
    snprintf(strp, 2, "%02X ", data[i]);
    strp++;
}

// now the memory in *str holds your hex string!
harper
  • 13,345
  • 8
  • 56
  • 105
problemPotato
  • 589
  • 3
  • 8
  • Almost. Add one more space for terminating `'\0'`... `char *str = malloc(sizeof(char) * (2 * sizeof(data) + 1));` – Floris Mar 07 '14 at 20:44
-1

this it work easy variable test is data type char or unsigned char

std::string User_re((char*)test); 
Pang
  • 9,564
  • 146
  • 81
  • 122
Thanawat test
  • 76
  • 1
  • 1
  • 9