convert number in ascii to binary...

S

Sam Smith

Hi,

is there a function or a "well-known" algorithm which converts a number of
random length represented as an array of bytes to its binary format?

For example: a 16 byte long array: "9567081354794432" should be converted to
the seven bytes: 0x21FD35B5B095C0

Thanks in advance
Sam
 
K

Karl Heinz Buchegger

Sam said:
Hi,

is there a function or a "well-known" algorithm which converts a number of
random length represented as an array of bytes to its binary format?

For example: a 16 byte long array: "9567081354794432" should be converted to
the seven bytes: 0x21FD35B5B095C0

If you need '7-byte' numbers, you might check if your compiler
supports data type 'long long'. If it does, check the sizeof
of that data type. If it is more then 6 (usually it is 8) then
you are on your way.

long long Number = 0;
char Characters[] = "9567081354794432";
int i;

while( Characters != '\0' ) {
Number = Number * 10 + ( Characters - '0' );
i++;
}
 
O

osmium

Karl Heinz Buchegger said:
is there a function or a "well-known" algorithm which converts a number
of
random length represented as an array of bytes to its binary format?

For example: a 16 byte long array: "9567081354794432" should be converted
to
the seven bytes: 0x21FD35B5B095C0

If you need '7-byte' numbers, you might check if your compiler
supports data type 'long long'. If it does, check the sizeof
of that data type. If it is more then 6 (usually it is 8) then
you are on your way.

long long Number = 0;
char Characters[] = "9567081354794432";
int i;

Nitpick?

int i = 0;
while( Characters != '\0' ) {
Number = Number * 10 + ( Characters - '0' );
i++;
}
 
J

JH Trauntvein

void dump_hex(std::eek:stream &out, unsigned char *buff, size_t buff_len)
{
for(size_t i = 0; i < buff_len; ++i)
{
out << std::hex << std::setw(2) << std::fill('0')
<< static_cast<unsigned short>(buff);
}
}
 
H

Howard

JH Trauntvein said:
void dump_hex(std::eek:stream &out, unsigned char *buff, size_t buff_len)
{
for(size_t i = 0; i < buff_len; ++i)
{
out << std::hex << std::setw(2) << std::fill('0')
<< static_cast<unsigned short>(buff);
}
}


Not what he was looking for, if I understand both of you. This code would
give 2-char hex values for each character in the string. But given the
example from the OP, I think he wants to simply store the extra-long string
of decimal digits as if it were an extra-long integer.

(The fact he showed it as hex seems to be a common mistake among posters,
thinking that there's something different in memory when representing
integers as hex. The difference is only in how you present it to the user,
not how it's stored. But the result in the example was something like
0x21..., which indicates to me to be an integer - a hex literal, if it was
in code - not a string, which would have been "21...".)

The ideal would be to convert the original string into an integer. The
major problem with that is he asked about a string of "random length", which
is not directly supported in any built-in integer type. (There is also the
question of byte-ordering to consider!)

I know of no "well-known" algorithm to do it. You need to be able to
compute the base-256 values (not base-16, since a byte holds 0..255, not
0..15), which is easy enough, except for the fact that you haven't specified
an upper limit to the length of the original string. Provided that length
is actually limited, you could maybe get away with using a double to store
the integer values for each decimal digit in the original string in turn,
and loop to decompose that into its base-256 parts, adding the results of
each iteration of that inner loop to the appropriate result digit (with
carry as needed).

But your best bet may be to find a library dedicated to finite precision
integer math. Such a library may already have what you need.

-Howard
 
A

Albert van der Horst

Hi,

is there a function or a "well-known" algorithm which converts a number of
random length represented as an array of bytes to its binary format?

For example: a 16 byte long array: "9567081354794432" should be converted to
the seven bytes: 0x21FD35B5B095C0

Look up synthetic division in Knuth TAOP.
(Not C++ specific.)
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top