472,374 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

convert number in ascii to binary...

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
Jul 22 '05 #1
5 6951
Sam Smith wrote:

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[i] != '\0' ) {
Number = Number * 10 + ( Characters[i] - '0' );
i++;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
"Karl Heinz Buchegger" writes:
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[i] != '\0' ) {
Number = Number * 10 + ( Characters[i] - '0' );
i++;
}

Jul 22 '05 #3
void dump_hex(std::ostream &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[i]);
}
}

Jul 22 '05 #4

"JH Trauntvein" <j.**********@comcast.net> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
void dump_hex(std::ostream &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[i]);
}
}


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


Jul 22 '05 #5
In article <xm*****************@nntpserver.swip.net>,
Sam Smith <sa*@smith.com> wrote:
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.)

Thanks in advance
Sam

--

--
Albert van der Horst,Oranjestr 8,3511 RA UTRECHT,THE NETHERLANDS
One man-hour to invent,
One man-week to implement,
One lawyer-year to patent.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Philipp H. Mohr | last post by:
Hello, I am trying to xor the byte representation of every char in a string with its predecessor. But I don't know how to convert a char into its byte representation. This is to calculate the...
13
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
3
by: craig.wagner | last post by:
I've got an application that is calling a command-line executable. The command-line tool uses stdin and stdout as its interface, and it expects a binary stream in and sends a binary stream out. ...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
3
by: Director - Minvent | last post by:
Hi, I am reading from a serial port from a device which sends over an ascii character. It uses the full extended 256 character set and therefore has non-printing characters too. So what i want...
13
by: HNT20 | last post by:
Hello All i am new to python language. i am working on a gnuradio project where it uses python as the primary programming language. i am trying to convert a message, text, or numbers into binary...
6
by: as400tips | last post by:
I have a Binary Data file (Packed Decimal and ASCII mixed) and would like to convert into ASCII (readable) file. How to do it in C#? Thanks.
1
by: ayaniv | last post by:
can anyone tell me how can i convert the ascii code: 48 to the binary number: 00110000 ?? if anyone knows if there any other way to save the char of an ascii code 48 in a variable it would be...
4
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. ...
4
by: Mason | last post by:
I have tried and tried... I'd like to read in a binary file, convert it's 4 byte values into floats, and then save as a .txt file. This works from the command line (import struct); In : f =...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.