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? 7 19170 el*********@gmail.com wrote:
[snip]
How do I convert 32-bit binary string into hexadecimal?
In snippets, look at bascnvrt.c 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.)
Use strtoul().
--
Eric Sosman es*****@acm-dot-org.invalid 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?
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
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
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 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().
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>
On Tue, 26 Dec 2006 15:13:55 -0500, Martin Ambuhl
<ma*****@earthlink.netwrote:
>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;
Undefined behavior. See FAQs 8.5 and 1.32.
--
jay
jaysome wrote:
On Tue, 26 Dec 2006 15:13:55 -0500, Martin Ambuhl
<ma*****@earthlink.netwrote:
>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;
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David Lawson |
last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an
array of integers (really unsigned chars). Eg,
$str ="ABC";
needs to convert to something...
|
by: Mark S Pryor |
last post by:
Hello,
Using MySQL 4.017 on Win2k sp3:
using MySQL built-ins: how can I convert a
value stored as a hexadecimal string back to
a binary string while logged into the shell?
>set @t1=616263;...
|
by: akash deep batra |
last post by:
hi
i want to convert a 96 bit binary number into a hexadecimal number.
e.g binary number= 001100010001010000100101011110111111010101110100010110000101011000101010000000000000000000000000
how...
|
by: akk003 |
last post by:
Hi,
I have to convert to a hexadecimal file. I'am new to file handling so kindly review my code and tell me why does it crash!
//As inputs, I use:
// hexdump_c.cpp
// gif.txt /* after this,...
|
by: bilbo2000 |
last post by:
I am trying to read in a binary file which has the data stored as 32
bit float. I have been trying to figure out how to read this in as 32
bit but everytime I try to read it in the best I can do...
|
by: supercooper |
last post by:
I have this string that is being returned from a query on a DBISAM
database. The field must be some sort of blob, and when I connect to
the database thru ODBC in MS Access, the field type comes...
|
by: sweeet_addiction16 |
last post by:
hello
Im writin a code in c...
can sum1 pls help me out in writing a c code to convert decimalnumber
to hexadecimal number.The hexadecimal number generated has to be an
unsigned long.
|
by: Terry Reedy |
last post by:
A. Joseph wrote:
These are number representation systems that can be applied to or used
with integral, rational (numberator,denominator), and 'point' numbers.
Try Wikipedia or any search...
|
by: napster161 |
last post by:
1st Question:
Correction: BINARY to HEXADECIMAL
I have write some simple code for this matter
public class DecimalToHexadecimal{
public static void main(String args) throws IOException{
...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |