Nu știam ce este acela un hex string"".
Acuma am înțeles. (cred că pentru majoritatea este ceva banal, dar nu a fost și pt mine).
citez:
"
I think for the most part you are just doing unnecessary calculations and using unnecessary temporary space.
One of the reasons hexadecimal is so nice is because 1 hexa digit is
equal to a nibble or 4 bits, so if I were supposed to convert your
string into a byte string it would have to be like:
Code:
A4 50 5D 0B 0F 6A ED AA
10100100 0101000 01011101 00001011 00001111 01101010 11101101 10101010
Each
column is one byte of string. BTW this is for illustration. Just don't
mind the numbers too much, I tried to convert to binary and I think it's
right, but I might have transposed some digits or something.
The good news is that sscanf can indeed do this work, but you need to
handle it two characters at a time, because it is the same as storing
the result one byte at a time.
Additionally, if you actually look up the %X conversion for sscanf
(check your syntax!) you will find that the result is expected to be a
unsigned int. Just use a temporary variable with sscanf, and cast over
to unsigned char or char, whichever is right, when you put it into the
byte string. I am fairly sure this was completely ignored in your
attempt and contributed to things being very wrong."
#include <stdio.h>
int main() {
int i, n;
char *HEXStr = "A4505D0B0F6AEDAA";
unsigned char tmpByte[9];
char backHex[17];
for(i = 0; i < 16; i++) {
sscanf(HEXStr+2*i, "%2X", &n);
tmpByte[i] = (char)n;
}
for(i = 0; i < 8; i++)
sprintf(backHex+2*i, "%02X", tmpByte[i]);
backHex[16] = '\0';
printf("%s\n", backHex);
return 0;
}
Am citat din linkul de mai jos.
http://cboard.cprogramming.com/c-programming/150170-how-change-hex-string-byte-array.html