In C, what is the most efficient way to convert a string of hex digits into a binary unsigned int
or unsigned long
?
For example, if I have
Hexadecimal to decimal. Don't run it on online compilers, because it won't work.
#include<stdio.h>
void main()
{
unsigned int i;
scanf("%x",&i);
printf("%d",i);
}
Try this to Convert from Decimal to Hex
#include<stdio.h>
#include<conio.h>
int main(void)
{
int count=0,digit,n,i=0;
int hex[5];
clrscr();
printf("enter a number ");
scanf("%d",&n);
if(n<10)
{
printf("%d",n);
}
switch(n)
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("B");
break;
case 13:
printf("C");
break;
case 14:
printf("D");
break;
case 15:
printf("E");
break;
case 16:
printf("F");
break;
default:;
}
while(n>16)
{
digit=n%16;
hex[i]=digit;
i++;
count++;
n=n/16;
}
hex[i]=n;
for(i=count;i>=0;i--)
{
switch(hex[i])
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
default:
printf("%d",hex[i]);
}
}
getch();
return 0;
}
Why is a code solution that works getting voted down? Sure, it's ugly ...
Perhaps because as well as being ugly it isn't educational and doesn't work. Also, I suspect that like me, most people don't have the power to edit at present (and judging by the rank needed - never will).
The use of an array can be good for efficiency, but that's not mentioned in this code. It also takes no account of upper and lower case so it does not work for the example supplied in the question. FFFFFFFE
As written before, the efficiency basically depends on what one is optimizing for.
When optiming for lines of code, or just working in environment without fully-equipped standard library one quick and dirty option could be:
// makes a number from two ascii hexa characters
int ahex2int(char a, char b){
a = (a <= '9') ? a - '0' : (a & 0x7) + 9;
b = (b <= '9') ? b - '0' : (b & 0x7) + 9;
return (a << 4) + b;
}
... more in similar thread here: https://stackoverflow.com/a/58253380/5951263
This currently only works with lower case but its super easy to make it work with both.
cout << "\nEnter a hexadecimal number: ";
cin >> hexNumber;
orighex = hexNumber;
strlength = hexNumber.length();
for (i=0;i<strlength;i++)
{
hexa = hexNumber.substr(i,1);
if ((hexa>="0") && (hexa<="9"))
{
//cout << "This is a numerical value.\n";
}
else
{
//cout << "This is a alpabetical value.\n";
if (hexa=="a"){hexa="10";}
else if (hexa=="b"){hexa="11";}
else if (hexa=="c"){hexa="12";}
else if (hexa=="d"){hexa="13";}
else if (hexa=="e"){hexa="14";}
else if (hexa=="f"){hexa="15";}
else{cout << "INVALID ENTRY! ANSWER WONT BE CORRECT\n";}
}
//convert from string to integer
hx = atoi(hexa.c_str());
finalhex = finalhex + (hx*pow(16.0,strlength-i-1));
}
cout << "The hexadecimal number: " << orighex << " is " << finalhex << " in decimal.\n";
#include "math.h"
#include "stdio.h"
///////////////////////////////////////////////////////////////
// The bits arg represents the bit say:8,16,32...
/////////////////////////////////////////////////////////////
volatile long Hex_To_Int(long Hex,char bits)
{
long Hex_2_Int;
char byte;
Hex_2_Int=0;
for(byte=0;byte<bits;byte++)
{
if(Hex&(0x0001<<byte))
Hex_2_Int+=1*(pow(2,byte));
else
Hex_2_Int+=0*(pow(2,byte));
}
return Hex_2_Int;
}
///////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////
void main (void)
{
int Dec;
char Hex=0xFA;
Dec= Hex_To_Int(Hex,8); //convert an 8-bis hexadecimal value to a number in base 10
printf("the number is %d",Dec);
}