Char Array to Hex String C++

How do you convert a byte array to a hexadecimal string in C?

printf("%02X:%02X:%02X:%02X", buf[0], buf[1], buf[2], buf[3]);

For a more generic way:

int i;
for (i = 0; i < x; i++)
{
if (i > 0) printf(":");
printf("%02X", buf[i]);
}
printf("\n");

To concatenate to a string, there are a few ways you can do this. I'd probably keep a pointer to the end of the string and use sprintf. You should also keep track of the size of the array to make sure it doesn't get larger than the space allocated:

int i;
char* buf2 = stringbuf;
char* endofbuf = stringbuf + sizeof(stringbuf);
for (i = 0; i < x; i++)
{
/* i use 5 here since we are going to add at most
3 chars, need a space for the end '\n' and need
a null terminator */
if (buf2 + 5 < endofbuf)
{
if (i > 0)
{
buf2 += sprintf(buf2, ":");
}
buf2 += sprintf(buf2, "%02X", buf[i]);
}
}
buf2 += sprintf(buf2, "\n");

How to convert an Unsigned Character array into a hexadecimal string in C

So, based on your update, are you talking about trying to convert a unsigned char buffer into a hexadecimal interpretation, something like this:

#define bufferSize 10
int main() {
unsigned char buffer[bufferSize]={1,2,3,4,5,6,7,8,9,10};
char converted[bufferSize*2 + 1];
int i;

for(i=0;i<bufferSize;i++) {
sprintf(&converted[i*2], "%02X", buffer[i]);

/* equivalent using snprintf, notice len field keeps reducing
with each pass, to prevent overruns

snprintf(&converted[i*2], sizeof(converted)-(i*2),"%02X", buffer[i]);
*/

}
printf("%s\n", converted);

return 0;
}

Which outputs:

0102030405060708090A

C++ Convert Char Array To Hex String

Something very similar:

const char* string_to_hex(const char *str, char *hex, size_t maxlen)
{
static const char* const lut = "0123456789ABCDEF";

if (str == NULL) return NULL;
if (hex == NULL) return NULL;
if (maxlen == 0) return NULL;

size_t len = strlen(str);

char *p = hex;

for (size_t i = 0; (i < len) && (i < (maxlen-1)); ++i)
{
const unsigned char c = str[i];
*p++ = lut[c >> 4];
*p++ = lut[c & 15];
}

*p++ = 0;

return hex;
}

int main()
{
char hex[20];
const char *result = string_to_hex("0123", hex, sizeof(hex));
return 0;
}

How to turn a hex string into an unsigned char array?

You'll never convince me that this operation is a performance bottleneck.
The efficient way is to make good use of your time by using the standard C library:

static unsigned char gethex(const char *s, char **endptr) {
assert(s);
while (isspace(*s)) s++;
assert(*s);
return strtoul(s, endptr, 16);
}

unsigned char *convert(const char *s, int *length) {
unsigned char *answer = malloc((strlen(s) + 1) / 3);
unsigned char *p;
for (p = answer; *s; p++)
*p = gethex(s, (char **)&s);
*length = p - answer;
return answer;
}

Compiled and tested. Works on your example.

Print character array as hex in C

You are confused about the fuctionality of strtol. If you have a string that represents a number in hex, you can use strtol like you have:

char s[] = "ff2d";
int n = strtol(s, NULL, 16);
printf("Number: %d\n", n);

When you want to print the characters of a string in hex, use %x format specifier for each character of the string.

char s[] = "Hello";
char* cp = s;
for ( ; *cp != '\0'; ++cp )
{
printf("%02x", *cp);
}

Convert char array elements into Hexadecimal equivalent in C code

Iterate over the string, and with each iteration shift the result to the left and set the right-most bit to the appropriate value:

#include <stdio.h>
#include <string.h>

int main(void)
{
char const array[] = "11000000111111111000000010000000";
size_t const length = strlen(array);

unsigned long long n = 0ULL;
for (size_t idx = 0U; idx < length; ++idx)
{
n <<= 1U;
n |= array[idx] == '0' ? 0U : 1U;
// n |= array[idx] - '0'; // An alternative to the previous line
}

printf("%#llx\n", n);
}

This uses a (signed) char array, but the method is the same.

Storing the result back into the char array:

#include <stdio.h>
#include <string.h>

int main(void)
{
char array[] = "11000000111111111000000010000000";
size_t length = strlen(array);

for (size_t idx = 0U; idx < length; ++idx)
array[idx] = array[idx] - '0';

for (size_t idx = 0U; idx < length; ++idx)
printf("%d", array[idx]);
putchar('\n');
}

Note that here the char types will hold the decimal values 0 and 1.

way to convert char array to a hex string and print it in one line

Substitute your log function for printf in the printHex function. You may be able to get away with not passing the length of the array as long as it is 0 terminated and there are no 0 bytes in the middle of it so you could use strlen to find the length.

#include <stdio.h>
#include <string.h>

void printHex(char* digits, int len)
{
int i;
char* str;

str = malloc(len * 2 + 1);
memset(str, 0, len * 2 + 1);
for(i = 0; i < len; ++i)
{
char temp[3];
sprintf(temp, "%02x", digits[i]);
strcat(str, temp);
}
printf("%s\n", str);
free(str);
}

int main(void)
{
char temp[] ={0x1f,0x2d,0x3c,4,0,5,0,6,7,8};
printHex(temp, sizeof(temp));
return 0;
}


Related Topics



Leave a reply



Submit