469,645 Members | 1,941 Online

# Convert Binary String to Hexadecimal

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
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;
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') || (c=='1')) && ((c == '0') ||(c == '1'))
&& ((c == '0') || (c=='1'))){

// Send the hexadecimal convertion to another function
}
}

fclose(file);
return 0;
}
}

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

Dec 26 '06 #1
7 18479 el*********@gmail.com wrote:
[snip]
How do I convert 32-bit binary string into hexadecimal?
In snippets, look at bascnvrt.c

Dec 26 '06 #2
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
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().

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 26 '06 #3
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
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;
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;
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') || (c=='1')) && ((c == '0') ||(c == '1'))
&& ((c == '0') || (c=='1'))){

// Send the hexadecimal convertion to another function
}
}

fclose(file);
return 0;
}
}

How do I convert 32-bit binary string into hexadecimal?
Dec 26 '06 #4
On 26 Dec 2006 11:25:42 -0800, "el*********@gmail.com"
<el*********@gmail.comwrote:
>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
>11111111111111111111111111111111
00000000000000000000000000000000
11111111111111110000000000000000

I want to be able to take the 32-bit binary and convert it into
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;
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') || (c=='1')) && ((c == '0') ||(c == '1'))
&& ((c == '0') || (c=='1'))){

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
Dec 26 '06 #5
Eric Sosman wrote:
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
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().
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 */
}

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 26 '06 #6
On Tue, 26 Dec 2006 15:13:55 -0500, Martin Ambuhl
>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
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;
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.

--
jay
Dec 27 '06 #7
jaysome wrote:
On Tue, 26 Dec 2006 15:13:55 -0500, Martin Ambuhl
>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
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;
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.
Dec 27 '06 #8

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 4 posts views Thread by David Lawson | last post: by reply views Thread by Mark S Pryor | last post: by 2 posts views Thread by akash deep batra | last post: by 1 post views Thread by akk003 | last post: by 2 posts views Thread by bilbo2000 | last post: by 1 post views Thread by supercooper | last post: by 6 posts views Thread by sweeet_addiction16 | last post: by reply views Thread by Terry Reedy | last post: by 1 post views Thread by napster161 | last post: by reply views Thread by billypeterson | last post: by reply views Thread by Drake Tucker | last post: by reply views Thread by strativab | last post: by reply views Thread by eddparker01 | last post: by 1 post views Thread by Simmer | last post: by reply views Thread by gheharukoh7 | last post: by reply views Thread by aprens | last post: by reply views Thread by isladogs | last post: by reply views Thread by isladogs | last post: by