如何将十六进制字符串转换成比特数组
1个回答
展开全部
#include <stdio.h>
int Change(char s[],char bits[]) {
int i,n = 0;
for(i = 0; s[i]; i += 2) {
if(s[i] >= 'A' && s[i] <= 'F')
bits[n] = s[i] - 'A' + 10;
else bits[n] = s[i] - '0';
if(s[i + 1] >= 'A' && s[i + 1] <= 'F')
bits[n] = (bits[n] << 4) | (s[i + 1] - 'A' + 10);
else bits[n] = (bits[n] << 4) | (s[i + 1] - '0');
++n;
}
return n;
}
int main(void) {
char s[] = "E4F1C3A81F";
char bits[10];
int i,n = Change(s,bits);
printf ("%s\n",s);
for(i = 0;i < n;i++)
printf ("%X ",0XFF & bits[i]);
printf("\n");
return 0;
}
int Change(char s[],char bits[]) {
int i,n = 0;
for(i = 0; s[i]; i += 2) {
if(s[i] >= 'A' && s[i] <= 'F')
bits[n] = s[i] - 'A' + 10;
else bits[n] = s[i] - '0';
if(s[i + 1] >= 'A' && s[i + 1] <= 'F')
bits[n] = (bits[n] << 4) | (s[i + 1] - 'A' + 10);
else bits[n] = (bits[n] << 4) | (s[i + 1] - '0');
++n;
}
return n;
}
int main(void) {
char s[] = "E4F1C3A81F";
char bits[10];
int i,n = Change(s,bits);
printf ("%s\n",s);
for(i = 0;i < n;i++)
printf ("%X ",0XFF & bits[i]);
printf("\n");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询