Binary to Decimal Function
Author Message
Binary to Decimal Function

Is there a function in the math.h or any other that will convert a number in
binary to decimal?  If so, what header must I import and what is the syntax
for its use?

Thanks.

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function

Quote:
> Is there a function in the math.h or any other that will convert a number in
> binary to decimal?  If so, what header must I import and what is the syntax
> for its use?

> Thanks.

I'm confused!

Z

--
LISP is worth learning for the profound enlightenment experience you
will have when you finally get it; that experience will make you a
better programmer for the rest of your days.         Eric S. Raymond

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function
I'm writing a program in C that will read in a .bin file and decode the
records so that it can be displayed on the screen.  The first field is
EBCDIC, the second field is 2's complement, the third is IEEE 574 normalized
floating point, the fourth field is packed decimal and the fifth field is
zoned decimal.  I've basically written the program and I've got everything I
need except a shortcut (ie math function) that will make it easier to
convert the 2's complement and IEEE 574 fields from binary to decimal.
Is there a function that does this?

Thanks.

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function

Quote:
> I'm writing a program in C that will read in a .bin file and decode the
> records so that it can be displayed on the screen.  The first field is
> EBCDIC, the second field is 2's complement, the third is IEEE 574 normalized
> floating point, the fourth field is packed decimal and the fifth field is
> zoned decimal.  I've basically written the program and I've got everything I
> need except a shortcut (ie math function) that will make it easier to
> convert the 2's complement and IEEE 574 fields from binary to decimal.
> Is there a function that does this?

> Thanks.

So you want to convert a string that represents a number in binary
format to some integer type?

strtol(binarystring, NULL, 2);

will do this for you!

and maybe strtof() is what you need to convert your floating point
numbers.

Z

--
LISP is worth learning for the profound enlightenment experience you
will have when you finally get it; that experience will make you a
better programmer for the rest of your days.         Eric S. Raymond

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function
I'm not quite sure.  I think the number IS a binary number represented in 32
bits (4 bytes) and I want to convert it to decimal.  In memory it is just
bits.

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function

Quote:

> I'm not quite sure.  I think the number IS a binary number represented in 32
> bits (4 bytes) and I want to convert it to decimal.

Then you misunderstand how numbers are represented in C. What you've got
is a _number_. Not a binary representation of it, not a decimal
representation of it; a number. There is no way of "converting it to
decimal" in C, because there is no such thing as a "decimal number" as
opposed to any other kind. If you've got a number, you can print its
decimal representation using printf("%d", number) and its hex

If, instead, you have four bytes that hold the number, you can do a bit
of juggling. First, you'll have to realise that bytes need not be 8
bits. They can be anything from 8 bits upwards. Then, you can do
something like this:

unsigned long so_called_decimal;
char so_called_binary[4];

fill_in_eight_bits_of_each_char_from_file(so_called_binary);

// but you still need this:
so_called_decimal=so_called_binary[3]<<24 + so_called_binary[2]<<16 +
so_called_binary[1]<< 8 + so_called_binary[0];

Of course, this does presume that you've only filled the smallest eight
bits of each char, and that you've put the least significant machine
byte in the so_called_binary[0], but I think we can safely presume that,

Richard

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function

Quote:
> I'm not quite sure.  I think the number IS a binary number represented in 32
> bits (4 bytes) and I want to convert it to decimal.  In memory it is just
> bits.

So you have a binary file mixed with strings in EBCDIC format?

Hmm..., You will have to know where to find the integer numbers
and floats exactly. Than you simply load some data from the file
You state that you have sort of fields in your file. So you
exactly know where you need to read the data you expect.
Now, do a fread() for the first field, put it to your
string buffer, do a fread() for the second field and put
it into the integer variable with something like

char buf[1024];  /* buffer long enough to hold data to be read */
long myint;
size_t itms;

itms = fread(buf, sizeof(long),1, fp); /* where fp is a
FILE-pointer retunred from fopen() */

if(itms != 1)
return FUNCERROR;  /* FUNCERROR should be defined somewhere */

myint = buf[0];
myint |= (unsigned long) buf[1] << 8;
myint |= (unsigned long) buf[2] << 16;
myint |= (unsigned long) buf[3] << 24;

if you have a little endian system.

Take care, the above code has not been tasted!

Z
--
LISP is worth learning for the profound enlightenment experience you
will have when you finally get it; that experience will make you a
better programmer for the rest of your days.         Eric S. Raymond

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function

Quote:

>> I'm not quite sure.  I think the number IS a binary number represented in 32
>> bits (4 bytes) and I want to convert it to decimal.  In memory it is just
>> bits.

> So you have a binary file mixed with strings in EBCDIC format?

> Hmm..., You will have to know where to find the integer numbers
> and floats exactly. Than you simply load some data from the file
> You state that you have sort of fields in your file. So you
> exactly know where you need to read the data you expect.
> Now, do a fread() for the first field, put it to your
> string buffer, do a fread() for the second field and put
> it into the integer variable with something like

>    char buf[1024];  /* buffer long enough to hold data to be read */
>    long myint;
>    size_t itms;

>    itms = fread(buf, sizeof(long),1, fp); /* where fp is a
>                          FILE-pointer retunred from fopen() */

>    if(itms != 1)
>            return FUNCERROR;  /* FUNCERROR should be defined somewhere */

>    myint = buf[0];
>    myint |= (unsigned long) buf[1] << 8;
>    myint |= (unsigned long) buf[2] << 16;
>    myint |= (unsigned long) buf[3] << 24;

> if you have a little endian system.

> Take care, the above code has not been tasted!

^^^^^^^

for sure I ment tested!
Even though it might taste bad too!:-)

Quote:

>    Z

--
LISP is worth learning for the profound enlightenment experience you
will have when you finally get it; that experience will make you a
better programmer for the rest of your days.         Eric S. Raymond

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function
Thanks guys!  That'll give me enough to chew on for a while.

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function

Quote:

> > I'm not quite sure.  I think the number IS a binary number
> > represented in 32 bits (4 bytes) and I want to convert it to
> > decimal.  In memory it is just bits.

> So you have a binary file mixed with strings in EBCDIC format?

> Hmm..., You will have to know where to find the integer numbers
> and floats exactly. Than you simply load some data from the file
> You state that you have sort of fields in your file. So you
> exactly know where you need to read the data you expect.
> Now, do a fread() for the first field, put it to your
> string buffer, do a fread() for the second field and put
> it into the integer variable with something like

<snip code>

Basically the OP is trying to read a binary file. If the C compiler's
internal representation of ints, floats etc. matches the internal
format of the file then fread() will be fine. If they don't he's got
problems, and C doesn't provide any support for apparently simple
things like reading a 32-bit binary number. This is not C's fault
there is no standard for a binary integer in a file.

Try fread(). If it goes horribly wrong try hex dumping the fields you
can't read and reverse engineer their format. ints are fairly easy,
floats could be real nightmare. In general avoid storing things in
binary files, you end up paying later!

--
Abuse of casting leads to abuse of the type system
leads to unreliably, even undefined, behaviour.
And that is the path to the dark side....
Richard Bos/John Hascall

Sent via Deja.com http://www.deja.com/

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function

Quote:

> > > I'm not quite sure.  I think the number IS a binary number
> > > represented in 32 bits (4 bytes) and I want to convert it to
> > > decimal.  In memory it is just bits.

> > So you have a binary file mixed with strings in EBCDIC format?

> > Hmm..., You will have to know where to find the integer numbers
> > and floats exactly. Than you simply load some data from the file
> > You state that you have sort of fields in your file. So you
> > exactly know where you need to read the data you expect.
> > Now, do a fread() for the first field, put it to your
> > string buffer, do a fread() for the second field and put
> > it into the integer variable with something like

> <snip code>

> Basically the OP is trying to read a binary file. If the C compiler's
> internal representation of ints, floats etc. matches the internal
> format of the file then fread() will be fine. If they don't he's got
> problems, and C doesn't provide any support for apparently simple
> things like reading a 32-bit binary number. This is not C's fault
> there is no standard for a binary integer in a file.

> Try fread(). If it goes horribly wrong try hex dumping the fields you
> can't read and reverse engineer their format. ints are fairly easy,
> floats could be real nightmare. In general avoid storing things in
> binary files, you end up paying later!

Bad advice, IMNSHO.  The thing you KNOW is the endianess of the file
to be read, and there things are KNOWN to be in 8 bit bytes.  Thus the
solution given earlier, such as:

long val;

val = hiorderbyte;
val = (val << 8) + order3byte;
val = (val << 8) + order2byte;
val = (val << 8) + loorderbyte;

will always assemble a suitable value, regardless of the endianess of
the executing system.  All you have to do is pick the bytes in the
right order.  The size of a byte in your system has nothing to do with
it, the 8 represents the size of a byte on the input file.

--

http://www.qwikpages.com/backstreets/cbfalconer/

Sent via Deja.com http://www.deja.com/

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function

)I'm not quite sure.  I think the number IS a binary number represented in 32
)bits (4 bytes) and I want to convert it to decimal.  In memory it is just
)bits.

I have read a bit of this thread, and I think you don't have a clue what
you're trying to accomplish, which is why you are having a hard time
doing it.

It sounds like:

You have a binary file.
In that file are stored direct internal representations of integers in
You read these into memory, and want to convert them to strings of ASCII
digits which represent these numbers in decimal format.

sprintf() does this.

If this is not what you are doing, then you have yet to convey what you
are doing.

Mike

--
char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
This message made from 100% recycled bits.
I can explain it for you, but I can't understand it for you.
I don't speak for Alcatel      <- They make me say that.

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function

Quote:
> Is there a function in the math.h or any other that will convert a number in
> binary to decimal?  If so, what header must I import and what is the syntax
> for its use?

What do you mean by "a number in binary"? In C, if you have a *number*,
it's pretty much already in binary; that's how numbers are represented
in most current computers.

If you have a string of binary digits, eg "10011", then you can use
the function `strtol` in urm <stdlib.h> to turn it into an integer
(a long, to be exact).

If you have a number and you want its representation as a decimal string,
use `fprintf` with the appropriate format to print it out, or `sprintf`
ditto to convert it into a string; both of these are in <stdio.h>.

As for the syntax of their use, well, your local documentation should
tell you what the arguments are, and you can pretty much work it out
from there. function calls are function calls, and these functions get
used often enough that you'll have to learn them *sometime*: start now
and write some code, we can critique it if it doesn't work and you
can't udnerstand why.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html

Mon, 24 Mar 2003 03:00:00 GMT
Binary to Decimal Function

Quote:

> > > > I'm not quite sure.  I think the number IS a binary number
> > > > represented in 32 bits (4 bytes) and I want to convert it to
> > > > decimal.  In memory it is just bits.

> > > So you have a binary file mixed with strings in EBCDIC format?

> > > Hmm..., You will have to know where to find the integer numbers
> > > and floats exactly. Than you simply load some data from the file
> > > You state that you have sort of fields in your file. So you
> > > exactly know where you need to read the data you expect.
> > > Now, do a fread() for the first field, put it to your
> > > string buffer, do a fread() for the second field and put
> > > it into the integer variable with something like

> > <snip code>

> > Basically the OP is trying to read a binary file. If the C
> > compiler's internal representation of ints, floats etc. matches the
> > internal format of the file then fread() will be fine. If they don't
> > he's got problems, and C doesn't provide any support for apparently
> > simple things like reading a 32-bit binary number. This is not C's
> > fault there is no standard for a binary integer in a file.

> > Try fread(). If it goes horribly wrong try hex dumping the fields
> > you can't read and reverse engineer their format. ints are fairly
> > easy, floats could be real nightmare. In general avoid storing
> > things in binary files, you end up paying later!

> Bad advice, IMNSHO.  The thing you KNOW is the endianess of the file
> to be read, and there things are KNOWN to be in 8 bit bytes.  Thus the
> solution given earlier, such as:

>        long val;

>        val = hiorderbyte;
>        val = (val << 8) + order3byte;
>        val = (val << 8) + order2byte;
>        val = (val << 8) + loorderbyte;

> will always assemble a suitable value, regardless of the endianess of
> the executing system.  All you have to do is pick the bytes in the
> right order.  The size of a byte in your system has nothing to do with
> it, the 8 represents the size of a byte on the input file.

ok fair enough on ints.

what are you going to do about floats?

things in binary files?

--
Abuse of casting leads to abuse of the type system
leads to unreliably, even undefined, behaviour.
And that is the path to the dark side....
Richard Bos/John Hascall

Sent via Deja.com http://www.deja.com/

Tue, 25 Mar 2003 03:00:00 GMT
Binary to Decimal Function
The problem I was originally having stemmed from the fact that I was
creating a program to to be run on an Intel CPU  PC that would decode an IBM
mainframe created binary file .  Applying a bit mask and bit shifting solved
the problem.

Thanks to all for their suggestions.

Tue, 25 Mar 2003 03:00:00 GMT

 Page 1 of 2 [ 15 post ]

Relevant Pages