el*********@gmail.com wrote:
I have a text file that contains a header 32-bit binary. For example,
the text file could be:
%%This is the input text
%%test.txt
Date: Tue Dec 26 14:03:35 2006
00000000000000001111111111111111
11111111111111111111111111111111
00000000000000000000000000000000
11111111111111110000000000000000
I want to be able to take the 32-bit binary and convert it into
hexadecimal. The hexadecimal equivalent of the 32-bit binary string
will be utilized in another function. For example, when the code
reaches the first 32-bit binary string, it will send 0xFFFF to another
function. For the second 32-bit binary string, it will send 0xFFFFFFFF
to another function. The hexadecimal equivalent cannot be a string
since the function will need to be the exact hexadecimal number
(0xFFFF, 0xFFFFFFFF, etc.)
This is what I have so far:
You're reinventing the wheel. The strto* family provides what you want.
For example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *inputfile[] = {
"%%This is the input text\n",
"%%test.txt\n",
"Date: Tue Dec 26 14:03:35 2006\n",
"00000000000000001111111111111111\n",
"11111111111111111111111111111111\n",
"00000000000000000000000000000000\n",
"11111111111111110000000000000000\n"
};
char *nl, *endp;
unsigned long value;
char hexrep[18];
size_t linenum, nlines = sizeof inputfile / sizeof *inputfile;
/* ignore three lines (fgets works for an input file) */
for (linenum = 0; linenum < nlines && linenum < 3; linenum++) ;
/* in turn get, each line (again fgets works for both input and loop
control for an input file), strip off the newline, store the
value, create a text representation of that value, print the value
and the text representation of it. */
for (; linenum < nlines; linenum++) {
if ((nl = strchr(inputfile[linenum], '\n')))
*nl = 0;
value = strtoul(inputfile[linenum], &endp, 2);
/* insert error checking (including the use of *endp) here */
sprintf(hexrep, "%#lx", value);
printf("The string representing the binary number:\n"
" \"%s\"\n"
"The value of that number: %lu (decimal), %#lo"
" (octal),\n"
" %#lx (hex)\n"
"A string created to hold the hex representation:"
" \"%s\"\n\n",
inputfile[linenum], value, value, value, hexrep);
}
return 0;
}
The string representing the binary number:
"00000000000000001111111111111111"
The value of that number: 65535 (decimal), 0177777 (octal),
0xffff (hex)
A string created to hold the hex representation: "0xffff"
The string representing the binary number:
"11111111111111111111111111111111"
The value of that number: 4294967295 (decimal), 037777777777 (octal),
0xffffffff (hex)
A string created to hold the hex representation: "0xffffffff"
The string representing the binary number:
"00000000000000000000000000000000"
The value of that number: 0 (decimal), 0 (octal),
0 (hex)
A string created to hold the hex representation: "0"
The string representing the binary number:
"11111111111111110000000000000000"
The value of that number: 4294901760 (decimal), 037777600000 (octal),
0xffff0000 (hex)
A string created to hold the hex representation: "0xffff0000"
>
#include <stdio.h>
int main(){
char c[33];
FILE *file;
file = fopen("test.txt", "r");
if (file == NULL){
printf("Error: File does not exist!\n");
return 1;
}
else{
while (fgets(c, 33, file) != NULL) {
if (((c[0]== '0') || (c[0]=='1')) && ((c[1] == '0') ||(c[1] == '1'))
&& ((c[2] == '0') || (c[2]=='1'))){
// Hexadecimal conversion occurs here
// Send the hexadecimal convertion to another function
}
}
fclose(file);
return 0;
}
}
How do I convert 32-bit binary string into hexadecimal?