如何解决如何在C中将十六进制字符串转换为二进制字符串?
我的范围是将十六进制字符串转换为二进制字符串。在我制作的程序中,我只打印。如何将每个单个十六进制字符(在 hexa 数组中)的转换保存为 4 位(在 binarynum 数组中)?
示例
hexa[100] = "ff" -> binarynum[100] = "11111111"
这是我的代码:
#include <stdio.h>
#include <string.h>
int main()
{
char binarynum[100];
long int i = 0;
char hexa[100] = "fff";
while (hexa[i])
{
switch (hexa[i])
{
case '0':
printf("0000"); break;
case '1':
printf("0001"); break;
case '2':
printf("0010"); break;
case '3':
printf("0011"); break;
case '4':
printf("0100"); break;
case '5':
printf("0101"); break;
case '6':
printf("0110"); break;
case '7':
printf("0111"); break;
case '8':
printf("1000"); break;
case '9':
printf("1001"); break;
case 'A':
printf("1010"); break;
case 'B':
printf("1011"); break;
case 'C':
printf("1100"); break;
case 'D':
printf("1101"); break;
case 'E':
printf("1110"); break;
case 'F':
printf("1111"); break;
case 'a':
printf("1010"); break;
case 'b':
printf("1011"); break;
case 'c':
printf("1100"); break;
case 'd':
printf("1101"); break;
case 'e':
printf("1110"); break;
case 'f':
printf("1111"); break;
default:
printf("\n Invalid hexa digit %c ",hexa[i]);
return 0;
}
i++;
}
return 0;
}
解决方法
这是一个使用查找表的示例;它假设您将处理 32 位 int
,因此结果缓冲区的大小会相应调整。不过,您应该可以根据自己的需要进行调整。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/**
* Maps our input character onto an array index. '0' maps to 0,* '1' maps to 1,'a' maps to 10,etc. C guarantees that decimal
* digit encodings are sequential,so subtracting `0` from a digit
* character should map the value correctly. This code assumes that
* the encodings for `a` through `f` are sequential (as they are in
* ASCII and UTF-8 - that *may* not be a valid assumption for
* other encodings.
*
* If the input character is neither a decimal nor hexadecimal
* digit,this will map to index 16 which holds an empty string.
*/
#define MAP(c) (isdigit(c) ? c - '0' : (isxdigit(c) ? tolower(c) - 'a' + 10 : 16 ) )
/**
* Takes an input value off the command line.
*/
int main( int argc,char **argv )
{
if ( argc < 2 )
{
fprintf( stderr,"USAGE: %s number\n",argv[0] );
exit( EXIT_FAILURE );
}
/**
* Convert the command line argument to an integer value.
*/
int arg = atoi( argv[1] );
/**
* Stores the hexadecimal string representation of the input
* value.
*/
char buf[9] = { 0 };
/**
* This will hold our final binary string
*/
char result[33] = { 0 };
/**
* Our lookup table
*/
char bits[17][5] = { "0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111","" };
sprintf( buf,"%08x",arg );
for ( char *b = buf; *b != 0; b++ )
strcat( result,bits[ MAP(*b) ] );
printf( "Binary version of %10d (0x%s) = %s\n",arg,buf,result );
return EXIT_SUCCESS;
}
还有一些输出:
$ ./map 1
Binary version of 1 (0x00000001) = 00000000000000000000000000000001
$ ./map 1234
Binary version of 1234 (0x000004d2) = 00000000000000000000010011010010
$ ./map 12345678
Binary version of 12345678 (0x00bc614e) = 00000000101111000110000101001110
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。