Reputation: 49
I am going read codes(binary) from a text file which will have data like "000101" or "100010". Then I am concatenating 2 bits with this binary strings further. I am having trouble with leading zeros,which i cant skip. I have been trying to do it by first converting the binary string to int (using atoi()) and then to hexadecimal string. But with leading zeros if i use that function,it truncates the leading zeros. I have searched here but the solutions given are not in C language. Is there any direct method to do this or I will have to keep track of everything? thanks in advance. Here is my function which takes int as input and then converts it to hexadecimal string
void toHex(int n, char str[]) /* Function to convert binary to hexadecimal. */
{
int i=0,decimal=0, rem;
char temp[2];
while (n!=0)
{
decimal += (n%10)*pow(2,i);
n/=10;
++i;
}
/* At this point, variable decimal contains binary number in decimal format. */
i=0;
while (decimal!=0)
{
rem=decimal%16;
switch(rem)
{
case 10:
str[i]='A';
break;
case 11:
str[i]='B';
break;
case 12:
str[i]='C';
break;
case 13:
str[i]='D';
break;
case 14:
str[i]='E';
break;
case 15:
str[i]='F';
break;
default:
str[i]=rem+'0';
break;
}
++i;
decimal/=16;
}
str[i]='\0';
strrev(str); // Function to reverse string.
if(strlen(str)==1)
{
temp[0] = '0';
temp[1] = str[0];
temp[2] = '\0';
strcpy(str,temp);
}
else if(strlen(str)==0)
{
temp[0] = '0';
temp[1] = '0';
temp[2] = '\0';
strcpy(str,temp);
}
}
Upvotes: 0
Views: 1512
Reputation: 67723
get the number of binary digits (before calling atoi)
size_t binlen = strlen(input);
get the number of hex digits to output (it's just binlen/4
rounded up)
size_t hexlen = ((binlen % 4 ? binlen+4 : binlen) / 4);
print the integer as hex, with the correct number of leading zeroes
sprintf(str, "%0.*x", hexlen, n);
NB. you should really pass the length of your output buffer, and use snprintf
instead ...
Upvotes: 1
Reputation: 726509
You should not start by converting the number to int
(the loop where you construct decimal
).
Instead, you should write a separate function that takes four characters representing a binary number, and produces a single hex character. Then you could implement your target function as a sequence of calls to this function, going four characters at a time.
The only trick to this approach is that you need to deal with situations when the length of the binary string is not divisible by 4. In this case you need to pad the initial portion of the string with zeros.
Here is how you can implement a function that converts four characters to a single hex digit:
char binToHexDigit(const char bin[4]) {
int digit = (bin[0] == '1' ? 8 : 0)
| (bin[1] == '1' ? 4 : 0)
| (bin[2] == '1' ? 2 : 0)
| (bin[3] == '1' ? 1 : 0);
return (digit < 10 ? '0' : ('A'-10)) + digit;
}
Demo.
Note how this function avoids the switch
statement by exploiting the fact that digits 0..9 and letters A..F are arranged sequentially in UNICODE, ASCII, and EBCDIC, so you can convert a number to a digit character using a simple addition.
Upvotes: 0