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
|
|
0
|
|
|
|
Reply
|
sam2576 (10)
|
12/10/2004 9:53:34 AM |
|
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
kbuchegg@gascad.at
|
|
0
|
|
|
|
Reply
|
kbuchegg (2095)
|
12/10/2004 12:50:50 PM
|
|
"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++;
> }
|
|
0
|
|
|
|
Reply
|
r124c4u1022 (2251)
|
12/10/2004 1:25:01 PM
|
|
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]);
}
}
|
|
0
|
|
|
|
Reply
|
j.trauntvein (41)
|
12/10/2004 2:46:38 PM
|
|
"JH Trauntvein" <j.trauntvein@comcast.net> wrote in message
news:1102689998.869295.99000@c13g2000cwb.googlegroups.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
|
|
0
|
|
|
|
Reply
|
alicebt (1862)
|
12/10/2004 5:39:53 PM
|
|
In article <xmeud.3106$Of5.2294@nntpserver.swip.net>,
Sam Smith <sam@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.
|
|
0
|
|
|
|
Reply
|
albert37 (2988)
|
12/21/2004 2:48:02 PM
|
|
|
5 Replies
24 Views
(page loaded in 0.115 seconds)
Similiar Articles: conversion decimal to hex,octal,binary - comp.lang.asm.x86 ...How to Convert Binary to Octal Number | eHow.com How to Convert Binary to Octal Number. Binary, octal and our familiar decimal system are all place ... varchar2 to hex/octal ... convert string - comp.databases.oracle ...How to convert decimal to binary, octal and hex String in Java ... of permissions - comp.unix ... Converting number to std ... Hex to ascii - comp.lang.asm.x86 varchar2 to ... integer BCD converter in VHDL - comp.lang.vhdlThis code should convert binary number to BCD ... Binary To BCD Conversion ... binary number ... lang.asm.x86 I thought BCD BigInt might be easier, but ... bigint to ascii ... EBCDIC packed fields conversion - comp.unix.programmer... Conversions: ascii Convert ... VEDIT Hex, EBCDIC, ASCII, Binary, Text Editor, Conversion ... Convert a simple ... EBCDIC, ASCII and Numbers Conversion ... problem to convert integer to ascii chars for LCD in vhdl - comp ...... comp.lang.fortran problem to convert integer to ascii chars for LCD in vhdl - comp ... The problem is that the LCD only takes one ASCII ... the 8-bit binary number and ... unsigned long -> ascii conversion - comp.lang.asm.x86Hi Guys, I have written a function that will convert a number in the EAX register into a hex value in ascii ... Note, also, that the choice to use binary vs. ascii ... Then ... Convert decimal to 24-bit binary - comp.soft-sys.matlabHow can i convert a decimal number to binary number LIMITED to 24-bits? I especially need to know how to do this in Simulink, but knowing how to do it... Convert large numbers to binary in matlab - comp.soft-sys.matlab ...Hello everyone, am looking for the matlab code for converting NEGATIVE FRACTIONS TO BINARY. ... How to Convert a Negative Binary Number; How to Convert Signed Binary to ... Convert decimal number in binary number - comp.lang.vhdl ...Convert decimal number in binary number - comp.lang.vhdl ... Convert decimal number in binary number - comp.lang.vhdl ... How to get a number with 2 decimal, whatever this ... Binary to BCD in VHDL - comp.lang.vhdlThis code should convert binary number to BCD number using shift and add= 3 ... Conversion: double IEEE 754 => decimal ASCII-String - comp.lang ... Binary to BCD in VHDL ... How to Convert FTP Binary to ASCII | eHow.comHow to Convert FTP Binary to ASCII. When you create a website or need to upload files to ... File transfer protocol software is free to users and any number of them do ... www.6502.org: Source: Convert ASCII Number String to 32-bit BinaryConvert ASCII Number String to 32-bit Binary. ... Convert ASCII Number String to 32-bit Binary. This 6502 assembly language program converts a null-terminated ASCII ... 7/27/2012 10:37:19 AM
|