C:将uint8数组转换为包含其字节的十六进制表示的字符串

时间:2021-03-05 15:47:06

I have a uint8 array:

我有一个uint8数组:

int i = 0;
uint8_t x[32];

for (i = 0; i < 32; i++) {
    x[i] = i*i;
    printf("x[%d]: %02X\n", i, x[i]);
}

Here are the contents of the array:

以下是数组的内容:

x[0]: 0 00
x[1]: 1 01
x[2]: 4 04
...
x[14]: 196 C4
x[15]: 225 E1
...etc

I want to convert the x array to a char array that is equivalent to the full string representation of the byte array stored in x, which is:

我想将x数组转换为char数组,该数组等效于存储在x中的字节数组的完整字符串表示形式,即:

00010409101924314051647990A9C4E10021446990B9E4114071A4D9104984C1

Basically, I want to do the programatic equivalent of

基本上,我想做程序化的等价物

char hex[64] = "00010409101924314051647990A9C4E10021446990B9E4114071A4D9104984C1"

How can this be done in C programming language?

如何用C编程语言完成?

2 个解决方案

#1


2  

You can use sprintf and pass &hex[2*i] for the printing position:

您可以使用sprintf并传递&hex [2 * i]作为打印位置:

char hex[65];
for (int i = 0 ; i != 32 ; i++) {
    sprintf(&hex[2*i], "%02X", x[i]);
}
hex[64] = '\0';

Since you know that each sprintf will use exactly two positions, you can be sure that 32 sprintf calls will fill all 64 characters in hex[64].

既然你知道每个sprintf将使用正好两个位置,你可以确定32个sprintf调用将填充十六进制[64]中的所有64个字符。

Note: your example tries to store 65 characters in hex[64]. The 65-th one is coming from null terminator of the string literal.

注意:您的示例尝试以十六进制[64]存储65个字符。第65个来自字符串文字的空终止符。

#2


1  

First create a function that converts a nibble (4 bits) to a hex character, e.g.

首先创建一个将半字节(4位)转换为十六进制字符的函数,例如

char to_hex(uint8_t nibble)
{
   static const char hex[] = "0123456789ABCDEF";
   return hex[nibble];
}

Then make a sufficiently large array to hold the result (you need one extra character for the nul terminator, call the to_hex function for each 4 bits.

然后创建一个足够大的数组来保存结果(nul终结符需要一个额外的字符,每4位调用一次to_hex函数。

char result[65];
int i = 0;
uint8_t x[32];

for (i = 0; i < 32; i++) {
    x[i] = i*i;
    result[i*2] = to_hex(x[i] >> 4);  //upper 4 bits
    result[i*2+1] = to_hex(x[i] & 0xf); //lower 4 bits
}
result[64] = 0; //make sure the string is nul terminated
printf("Result is : %s\n", result);

#1


2  

You can use sprintf and pass &hex[2*i] for the printing position:

您可以使用sprintf并传递&hex [2 * i]作为打印位置:

char hex[65];
for (int i = 0 ; i != 32 ; i++) {
    sprintf(&hex[2*i], "%02X", x[i]);
}
hex[64] = '\0';

Since you know that each sprintf will use exactly two positions, you can be sure that 32 sprintf calls will fill all 64 characters in hex[64].

既然你知道每个sprintf将使用正好两个位置,你可以确定32个sprintf调用将填充十六进制[64]中的所有64个字符。

Note: your example tries to store 65 characters in hex[64]. The 65-th one is coming from null terminator of the string literal.

注意:您的示例尝试以十六进制[64]存储65个字符。第65个来自字符串文字的空终止符。

#2


1  

First create a function that converts a nibble (4 bits) to a hex character, e.g.

首先创建一个将半字节(4位)转换为十六进制字符的函数,例如

char to_hex(uint8_t nibble)
{
   static const char hex[] = "0123456789ABCDEF";
   return hex[nibble];
}

Then make a sufficiently large array to hold the result (you need one extra character for the nul terminator, call the to_hex function for each 4 bits.

然后创建一个足够大的数组来保存结果(nul终结符需要一个额外的字符,每4位调用一次to_hex函数。

char result[65];
int i = 0;
uint8_t x[32];

for (i = 0; i < 32; i++) {
    x[i] = i*i;
    result[i*2] = to_hex(x[i] >> 4);  //upper 4 bits
    result[i*2+1] = to_hex(x[i] & 0xf); //lower 4 bits
}
result[64] = 0; //make sure the string is nul terminated
printf("Result is : %s\n", result);