#include <stdlib.h>
#include <stdio.h>
#define STR_SIZE 8
long hex_value = 0XFF; //ef74=1110 1111 0111 0100低14位,10 1111 0111 0100
char bin_str[STR_SIZE];
long hex_tmp;
void HexToBinStr(int hex, char *bin_str, int str_size)
{
int i;
for (i = 0; i != str_size; ++i)
{
bin_str[str_size - 1 - i] = hex % 2 + '0';
hex /= 2;
}
}
int main(void)
{
hex_tmp = hex_value & 0x3fff;
HexToBinStr(hex_value, bin_str, STR_SIZE);
printf("hex_value: %x, hex_tmp: %x, binstr: %s \n", hex_value, hex_tmp, bin_str);
for (int i = 0; i < STR_SIZE; i++)
{
printf("%c\n", bin_str[i]);
}
return 0;
}
十六进制转字符串,并存到数组中
最新推荐文章于 2022-07-20 11:23:08 发布