Converting string of 0 and 1 characters to hexadecimal string

rohith p

I am converting all binary numbers stored in a file to hexadecimal. The file is parsed through command line argument, but to convert it to hexadecimal I want it to be converted to an integer, i.e. literal integer of the string received and atoi() doesn't seem to work.

#include<stdlib.h>
#include<stdio.h>
#include<string.h>

int main ( int argc, char *argv[] )
{
    FILE *fp,*op;
    fp = fopen(argv[1],"r");
    op= fopen(argv[2],"w+");
    if( fp == NULL) 
    {
       perror("An error has occurred\n");
       exit(1);
    }

    char buff[1026];
    unsigned char hexstr[1024];
    unsigned long int hexnum,hexint;
    unsigned char four[4];
    int i, j,len;
    while(fgets(buff, sizeof(buff), fp))
    {
        printf("\nString value = %s", buff);
        hexint=atoi(buff);
        printf("%ld",hexint);
    }

    fclose(fp); 
    fclose(op);
    return 0;
}

input file contains:

0000000000001010
1010000000000000
101010111100110111101111
000100100011010001010110
000100010001000100010001
011101110111011101110111
000000000000000000000000

output file should contain:

000a
a000
abcdef
123456
111111
777777
000000
dbush

The atoi function expects the input text to be in decimal format, but you're feeding it binary. Also, you're using the %d format specifier to print, which output decimal format.

You need to use the strtoul function for the conversion instead and specify base 2 for the input. Then for the output, use the %x format specifier to print in hex.

while(fgets(buff, sizeof(buff), fp))
{      printf("\nString value = %s", buff);
       hexint=strtoul(buff, NULL, 2);
       printf("%lx",hexint);
}

If you want the number of digits printed to vary based on the length of the input text, you can specify a field width of * and pass the number of digits as an extra parameter, and also add the 0 flag to pad on the left with zeros.

Since you have the binary string, and there are 4 binary digits to 1 hex digits, divide the length of the binary string by 4 and round up to get the number of digits:

       hexint=strtoul(buff, NULL, 2);
       int bindigits = strlen(buff);
       int hexdigits = (bindigits % 4 == 0) ? bindigits / 4 : bindigits / 4 + 1;
       printf("%0*lx", hexdigits, hexint);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

converting hexadecimal string to byte array

Converting binary string to a hexadecimal string JAVA

Converting ArrayList of Characters to a String?

Converting Hexadecimal String to Decimal Integer

Converting a string with Arabic characters to a date

NumberFormatException while converting Hexadecimal String to Long

Converting an integer to a hexadecimal string in Ruby

Converting a hexadecimal string to a decimal integer

Converting hexadecimal string to integer

Converting unicode string to hexadecimal representation

Hexadecimal string representation to hexadecimal

Converting a hexadecimal string to a decimal number

Converting all characters in a string to it's hexadecimal format for values less than 256

Converting String in Array with the same Characters

How to convert a string with hexadecimal characters in a string to a full unicode in Python 2.7

Converting a hexadecimal String to byte array

converting a list of string to integers starting at 0 to 1 to

JAVA Converting Hexadecimal String

Python to convert special unicode characters (like ♡) to their hexadecimal literals in a string (like 0x2661)

Converting string to Unicode characters

Converting a string into an array of characters

How to replace characters in a string of 0's and 1's by other characters

Byte hexadecimal object converting to string

Converting bytes string with escape characters into a normal string

Converting a large hexadecimal string to decimal

Converting hexadecimal to string in snowflake

Ruby: Converting Hexadecimal Strings to Hexadecimal NON-string (edited)

Delphi : Converting long hexadecimal string to decimal string

String should only contain characters '?', '0' and '1'