[英]How to convert from hexadecimal to binary using pointers in C
我需要編寫一個程序來解析十六進制之間的運算,在該程序中,我詢問要操作的十六進制數,詢問它們,然后顯示結果。
可以完成的運算是or,and,xor(|,^,&)。 我用存儲十六進制數的數組制作了轉換部分,然后將其轉換成二進制數並將其保存在另一個數組中,但是當我意識到自己知道要操作多少個十六進制整數時,我就想出了如何創建足夠的整數數組來存儲那些有人告訴我的信息,我必須使用指針。 我對此並不陌生,並對此進行了一些研究仍然不知道指針如何解決這個問題。
到目前為止,我的代碼的一部分:
#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()
使其增長。
我們事先知道數組的大小是因為我們需要它,因此我們分配了確切的數量。 但這僅僅是數組,內容需要它自己的內存。
讓我嘗試一個比喻(一個比喻嗎?我永遠不會知道):將數組想象成帶有多個鈎子的軌道; 你可能會在廚房里發現這樣的東西。 如果要將香料掛在這些鈎子上,則不能直接做,需要將其放入足夠大的容器中,以保持要填充的香料量。如果容器太小,則會流淌如果太大,那就太浪費了。
因此,您需要知道最小數目的鈎子(對於第一個malloc
)和容器的最小大小(對於第二個malloc
)。 將此隱喻擴展到其極限:我用hex
作為湯匙填充了容器bin[i]
。
如果您想要任意大的輸入:嘗試使用fgets()
和company。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.