Convert Binary String to Hexadecimal

E

elliotng.ee

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:

#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?
 
E

Eric Sosman

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.)

Use strtoul().
 
M

Martin Ambuhl

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?
 
B

Barry Schwarz

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

You are using terms somewhat inconsistently. Hexadecimal, binary, and
decimal are simply ways to REPRESENT integer values. If you really
have a text file, you don't have a 32 bit binary. You have a 32
character representation of an integer value using binary notation.
This line of data is the binary representation of the value 65535
(when represented in decimal) or the value FFFF when represented in
hexadecimal.
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

Since you state below that you really don't want a string of F's but
the "exact value", you really want the integer value represented by
the characters.
will be utilized in another function. For example, when the code
reaches the first 32-bit binary string, it will send 0xFFFF to another

You want this function to return the integer value represented by the
characters.
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.)

You mean the exact integer value.
This is what I have so far:

#include <stdio.h>

int main(){
char c[33];

You want the call to fgets below to read in the entire line of data. c
must have room for the 32 characters, the '\n' that terminates the
line, and the '\0' that terminates the string (you want a string).
This adds up to 34, not 33.
FILE *file;

file = fopen("test.txt", "r");

if (file == NULL){
printf("Error: File does not exist!\n");
return 1;
}

You don't want your conversion function to open the file and read the
same line repeatedly. You should open the file in main or some other
calling function, test for success, process or skip the three header
lines, and pass the FILE* to your conversion routine so it reads a new
line each time.
else{

while (fgets(c, 33, file) != NULL) {

Replace the "33" with "sizeof c" and never have to worry about it
changing again.
if (((c[0]== '0') || (c[0]=='1')) && ((c[1] == '0') ||(c[1] == '1'))
&& ((c[2] == '0') || (c[2]=='1'))){

// Hexadecimal conversion occurs here

Look up the strto... conversion functions in the standard library.
// Send the hexadecimal convertion to another function

A simple "return converted_value" should work.
}
}

fclose(file);
return 0;
}
}



How do I convert 32-bit binary string into hexadecimal?

If you really had a 32 bit binary value, you could simply memcpy it to
a 32 bit integer type and you would have the value you wanted.


Remove del for email
 
C

CBFalconer

Eric said:
Use strtoul().

Alternatively, if you want to simultaneously insist on exactly 32
chars and detect errors, you can write your own. Such as
(untested):

/* input exactly lgh chars, file positioned to read 1st digit */
int binstr2ul(FILE *f, int lgh, unsigned long *value) {
int ch;
int i

*value = 0; i = lgh;
for (; i; i--) {
if (('0' == (ch = getc(f)) || ('1' == ch))
*value = 2 * value + ('1' == ch);
else /* bad character in input */
break;
}
if (i) return ch; /* actual bad character */
return 0; /* all is well */
}
 
J

jaysome

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;

Undefined behavior. See FAQs 8.5 and 1.32.
 
M

Martin Ambuhl

jaysome said:
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;

Undefined behavior. See FAQs 8.5 and 1.32.

You are quite right. In my earlier version I had copied each line into
an array. In a foolish attempt to make the code smaller and 'cleaner' I
introduced this error. Since, as you know if you followed this
newsgroup at all, I am well aware of the problem, your cryptic note,
even without the reference to the FAQ, was obviously sufficient for me,
but the OP would no doubt find it less than useful. He may not even
know how to find the FAQ. You should, for the sake of the poor OP,
specify the problem rather just sticking out your tongue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,103
Latest member
VinaykumarnNevatia
Top