Click here to Skip to main content
14,691,238 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can I convert a hex char array to a byte array?


for eg
data[]="600ffe422b4e00731a59557a5cca46cc183944191006324a447bdb2d98d4b408
"

how can I convert this array to a byte array strictly in c language

What I have tried:

.....................................................................................
Posted
Updated 23-Mar-20 22:05pm

Here you can find an example implementation for C:
c - How to turn a hex string into an unsigned char array? - Stack Overflow[^]
   
Comments
Rick York 24-Mar-20 2:54am
   
Hopefully he is able to figure out how to peel off one character at a time into a temporary string and convert that.
CPallini 24-Mar-20 3:01am
   
5.
Asish6542165 26-Mar-20 9:06am
   
thanks
Oh come on - is there anything you do know how to do for yourself? OK, Hashes can get a little complex, but this is a trivial task that shouldn't be beyond the average beginner after the second week of his course...

Allocate a new array of bytes: in the char array is n chars, then the length should be (n - 1) / 2 + 1 bytes.
Write a function that accepts a hex char, and returns a byte. That's pretty trivial, the simplest solution (though not the best) is a basic switch :
unsigned char FromHex(char c)
   {
   switch(c)
      {
      case '0': return 0;
      case '1': return 1;
      case '2': return 2;
      case '3': return 3;
      case '4': return 4;
      case '5': return 5;
      case '6': return 6;
      case '7': return 7;
      case '8': return 8;
      case '9': return 9;
      case 'a': return 10;
      case 'b': return 11;
      case 'c': return 12;
      case 'd': return 13;
      case 'e': return 14;
      case 'f': return 15;
      }
   // Report a problem here!
   ...
   return -1;
   }
Then loop through your char array in pairs.
Use the FromHex function to convert each character to binary, and use the Left Shift operator << to move the first result to the high byte, and binary OR iot with teh low byte:
b1 = FromHex(InputData[i++];
b2 = FromHex(INputData[i++};
b = (b1 << 4) | b2;
Then insert that byte into your output array.

30 seconds of actual thinking would have got you that: so why did you ask us?
   
Comments
k5054 24-Mar-20 9:18am
   
5ed for the first sentence alone.
Asish6542165 24-Mar-20 15:59pm
   
is this code correct

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <openssl sha.h="">
#include <openssl ripemd.h="">
unsigned char FromHex(char c)
{
switch(c)
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'B': return 11;
case 'C': return 12;
case 'D': return 13;
case 'E': return 14;
case 'F': return 15;
}

return -1;
}

int main(){

unsigned char rawdata[] = "046EAF0968AA895ADDFEE599566F0B880242461D1377F4887C9B84631E13067B96DB18C41E0C208F8D12EBCC3F99F2522903AF6105833E4CBADE9D6A1D0F039187";


int i,a,c=0;
unsigned char output[]="\0";
unsigned int b,b1,b2;


putchar('\n');
putchar('\n');

for(i=0;i<65;i++){

b1 = FromHex(rawdata[c]);
printf("%c",rawdata[c]);

c++;
b2 = FromHex(rawdata[c]);
printf("%c",rawdata[c]);

c++;
b = (b1 << 4) | b2;

output[a]=b;
a++;

}

putchar('\n');
putchar('\n');
printf("%s",output);
putchar('\n');

i=strlen(output);
printf("%d",i);

unsigned char *d = SHA256(output, 130, 0);

for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
printf("%02x", d[i]);
putchar('\n');


}
OriginalGriff 24-Mar-20 17:26pm
   
What does it do when you test it?
You do test your code, I assume?
Asish6542165 26-Mar-20 3:55am
   
gotcha!!! It took me a while to understand this concept. Thanks for answering my questions

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900