473,703 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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: "95670813547944 32" should be converted to
the seven bytes: 0x21FD35B5B095C 0

Thanks in advance
Sam
Jul 22 '05 #1
5 7061
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: "95670813547944 32" should be converted to
the seven bytes: 0x21FD35B5B095C 0


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[] = "95670813547944 32";
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: "95670813547944 32" should be converted
to
the seven bytes: 0x21FD35B5B095C 0

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[] = "95670813547944 32";
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::o 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<uns igned short>(buff[i]);
}
}

Jul 22 '05 #4

"JH Trauntvein" <j.**********@c omcast.net> wrote in message
news:11******** *************@c 13g2000cwb.goog legroups.com...
void dump_hex(std::o 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<uns igned 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************ *****@nntpserve r.swip.net>,
Sam Smith <sa*@smith.co m> 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: "95670813547944 32" should be converted to
the seven bytes: 0x21FD35B5B095C 0
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
41363
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 nmea checksum for gps data. e.g. everything between $ and * needs to be xor: $GPGSV,3,1,10,06,79,187,39,30,59,098,40,25,51,287,00,05,25,103,44* to get the checksum.
13
41813
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
2844
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. I have the calling side working and am capturing the return value into a MemoryStream. Now I'm breaking up the MemoryStream into structures. Some of the values coming back are character arrays which I want to turn into strings. They are not...
7
7112
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 without strtol or s/printf function. Thanks, whatluo.
3
32089
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 to do is convert the input to the associated decimal. I.E ASCII Char 'NUL' will become 0 ASCII Char 'SOH' will become 1
13
22109
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 code so that i can process it. i googled many times and tried many of the answers out there, no luck so far. is there a way to convert a string into its binary representation of the
6
28078
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
12555
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 great. example: i want to save the char of 48 as 0 and 49 as 1, 50 as 2... and so...
4
25066
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. Anybody please help in c language. Thanks in Advance.
4
22249
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 = open("test2.pc0", "rb") In : tagData = f.read(4) In : tagData
0
9251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9117
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9014
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7861
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6585
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2063
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.