如何在c中将for循环中的每个数组元素的值连接在一起


#include <stdio.h>
#include <stdint.h> 
#include <string.h>
int main ()
{
char *hexstring = "deadbeef"; //storing the hexadecimal value
int i;
unsigned int bytearray[12]; //variable to store the new output.
uint8_t str_len = strlen(hexstring); //calculates length of the hexstring
for (i = 0; i < (str_len / 2); i++)
{
sscanf(hexstring + 2*i, "%02x", &bytearray[i]);
printf("bytearray %d: %02xn", i, bytearray[i]);
}
return 0;
}

当前显示的输出:

bytearray 0: de
bytearray 1: ad
bytearray 2: be
bytearray 3: ef

要做的是:我想合并/组合/放在一起的数组,这样byterarray[0]byterarray[1]byterarray[2]byterarray[3],即死牛肉输出可以在for循环后再次看到?

您可能想要这个:

int db = 0;
for (i = 0; i < (str_len / 2); i++)
{
db <<= 8;            // shift 8 bits to the left
db |= bytearray[i];  // copy bytearray[i] into 8 lower bits of db
}
printf("%08xn", db);

最新更新