gpt4 book ai didi

c - 如何使用C中的指针将十六进制转换为二进制

转载 作者:行者123 更新时间:2023-11-30 15:07:11 25 4
gpt4 key购买 nike

我需要制作一个程序来解析十六进制之间的运算,其中我询问要运算的十六进制数,询问它们,然后显示结果。

可以进行的运算有或、与、异或(|、^、&)。我用数组制作了转换部分,其中存储了十六进制数,然后将它们转换为二进制并将其保存在另一个数组中,但是当我意识到我不知道要操作多少个十六进制整数时,我不知道如何创建足够的整数有人告诉我我必须使用指针来存储数组。我对此很陌生,并且对此进行了一些研究,但仍然不知道指针如何解决这个问题......

到目前为止我的部分代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <proyecto.h>
#include <string.h>
int main () {
char op,bin1[31]="",bin2[31]="",hex1[100],hex2[100];
int sizeh,repeat1,repeat2,n,z,i;
printf("Write Hexadecimal:);
scanf("%s",hex1);
convert(hex1,bin1,n);

函数转换:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <proyecto.h>
#include <string.h>
void convert(char hex1[],char bin1[],int n){
int i,b;
printf("\nEquivalent binary value: ");
for(i=0;i<4;i++){
switch(hex1[i]){
case '0': strcat(bin,"0000"); break;
case '1': strcat(bin,"0001"); break;
case '2': strcat(bin,"0010"); break;
case '3': strcat(bin,"0011"); break;
case '4': strcat(bin,"0100"); break;
case '5': strcat(bin,"0101"); break;
case '6': strcat(bin,"0110"); break;
case '7': strcat(bin,"0111"); break;
case '8': strcat(bin,"1000"); break;
case '9': strcat(bin,"1001"); break;
case 'A': strcat(bin,"1010"); break;
case 'B': strcat(bin,"1011"); break;
case 'C': strcat(bin,"1100"); break;
case 'D': strcat(bin,"1101"); break;
case 'E': strcat(bin,"1110"); break;
case 'F': strcat(bin,"1111"); break;
case 'a': strcat(bin,"1010"); break;
case 'b': strcat(bin,"1011"); break;
case 'c': strcat(bin,"1100"); break;
case 'd': strcat(bin,"1101"); break;
case 'e': strcat(bin,"1110"); break;
case 'f': strcat(bin,"1111"); break;
default: printf("\nInvalid hexadecimal digit %c ",hex[i]);
}
}
printf("%s",bin);
}

最佳答案

我仍然不确定,如果你想用字符串来完成这一切(如果你想在不使用大整数的情况下变得非常大,则需要这样做)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// for tolower()
#include <ctype.h>

int convert(char *hex, size_t len, char **bin)
{
size_t i;
char c;

for (i = 0; i < len; i++) {
// work on lower-case to safe us some work
c = tolower(hex[i]);
// TODO: use a table instead of a large switch,
// or a union
switch (c) {
case '0':
strcat(*bin, "0000");
break;
case '1':
strcat(*bin, "0001");
break;
case '2':
strcat(*bin, "0010");
break;
case '3':
strcat(*bin, "0011");
break;
case '4':
strcat(*bin, "0100");
break;
case '5':
strcat(*bin, "0101");
break;
case '6':
strcat(*bin, "0110");
break;
case '7':
strcat(*bin, "0111");
break;
case '8':
strcat(*bin, "1000");
break;
case '9':
strcat(*bin, "1001");
break;
case 'a':
strcat(*bin, "1010");
break;
case 'b':
strcat(*bin, "1011");
break;
case 'c':
strcat(*bin, "1100");
break;
case 'd':
strcat(*bin, "1101");
break;
case 'e':
strcat(*bin, "1110");
break;
case 'f':
strcat(*bin, "1111");
break;
default:
// should not happen, an error
return -1;
break;
}
}
return len;
}

int main()
{
// number of inputs
int count;
// length of input
int len;
// iterator
int i;
// first hexadecimal string (100 characters plus '\0')
// fixed size memory because the modifier 'm' is in Posix not standard-C
// and scanning input manually is more complicated.
char hex[101];
// binary strings
// gets allocated dynamically
char **bin;

puts("Number of Hexadecimal:");
scanf(" %d", &count);
// allocate an array for the strings
bin = malloc(count* sizeof(char *));
// gather input
for(i = 0;i < count;i++){
printf("Hexadecimal #%d:",i+1);
scanf(" %100s", hex);
len = strlen(hex);
//TODO: check input for correctness
// Hint: try isxdigit() (also in ctype.h)

// allocate memory for the individual string in the array (8 bits in a byte)
bin[i] = malloc(len * 8 + 1);
bin[i][0] = '\0';
convert(hex, len, &bin[i]);
printf("Binary #%d: %s\n",i+1,bin[i]);
}

// if you want to store the individual hextrings, too, just make an
// array "hexarray" and handle it just like the "bin" array.

// Now all hexstrings are converted and in the array "bin"
// Do whatever you want to do with them here

// free memory (although not needed at the end of main, just good style)
for(i = 0;i < count;i++){
free(bin[i]);
}
free(bin);

/*
Testvalues:
1234abdf -> 00010010001101001010101111011111
abdf1234 -> 10101011110111110001001000110100
*/

// exit and return the correct value the operating system assigned to
// "program ended without error"
exit(EXIT_SUCCESS);
}

编辑:

C 不假设任何事情,你需要明确地告诉它。因此,如果您想要一个数组,您需要提前告诉编译器成员的数量,或者分配几个成员,并在必要时使用 realloc() 稍后增加它。

我们提前知道数组的大小,因为我们要求它,因此我们分配了确切的数量。但这只是数组,内容需要自己的内存。

让我尝试一个比喻(明喻?我永远不会知道):将数组想象成带有许多钩子(Hook)的导轨;你可能会在厨房里发现这样的东西。如果你想把香料卡在这些 Hook 上,你不能直接这样做,你需要把它放在一个足够大的容器里,以容纳你想要填充的香料的数量。如果容器太小,它会流动超过[原文如此!],如果太大那就是浪费。

因此,您需要知道 Hook 的最小数量(对于第一个 malloc)和容器的最小大小(对于第二个 malloc)。为了将这个比喻发挥到极致:我使用hex作为勺子来填充容器bin[i]

如果你想要任意大的输入:尝试 fgets()和公司。

关于c - 如何使用C中的指针将十六进制转换为二进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38416695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com