473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Large number conversion, ASCII to binary

I am trying to write a function which will convert a large ascii
number, say 100 ascii digits, to its binary representation. It seems
that evey algorithm I am trying to think of is backwards. Normaly in
pen and paper ascii to binary conversion one would start by
subtracting largest power of 2 that can be subtracted from the given
number and work from left to right filling memory up with 1s and 0s.
In my case I cant really do that b/c the largest power of 2 which
would fit into a number i am trying to convert is too large even for
64 bit int.

Any help, suggestions, sample code would be greatly apreciated.

ets assume I have an array of char type, evey element of which has a
value equal to the value of next digit in ascii number.

Thanks ahead
Jul 14 '08 #1
12 3670
On Sun, 13 Jul 2008 17:52:13 -0700 (PDT), fermineutron
<fr**********@yahoo.comwrote:
>I am trying to write a function which will convert a large ascii
number, say 100 ascii digits, to its binary representation. It seems
that evey algorithm I am trying to think of is backwards. Normaly in
pen and paper ascii to binary conversion one would start by
subtracting largest power of 2 that can be subtracted from the given
number and work from left to right filling memory up with 1s and 0s.
In my case I cant really do that b/c the largest power of 2 which
would fit into a number i am trying to convert is too large even for
64 bit int.

Any help, suggestions, sample code would be greatly apreciated.

ets assume I have an array of char type, evey element of which has a
value equal to the value of next digit in ascii number.
Any of the "big number" libraries should provide the tools you need. I
happen to like the bignum part of clint by Richard Heathfield but that
may be because it's the only one I've studied.
Remove del for email
Jul 14 '08 #2
fermineutron <fr**********@yahoo.comwrites:
I am trying to write a function which will convert a large ascii
number, say 100 ascii digits, to its binary representation.
Homework? If not, use a bignum library (it will pay eventually). If
it is homework, I'll show you mine if you show me yours...

--
Ben.
Jul 14 '08 #3
On Jul 14, 5:52*am, fermineutron <free4tram...@yahoo.comwrote:

If your problem is regarding some production code, then it's better
not to re-invent the wheel and use some existing library. If it is for
pedagogical purpose, you ought to post your approach.
Jul 14 '08 #4
Ben Bacarisse wrote:
fermineutron <fr**********@yahoo.comwrites:
>I am trying to write a function which will convert a large ascii
number, say 100 ascii digits, to its binary representation.

Homework? If not, use a bignum library (it will pay eventually).
If it is homework, I'll show you mine if you show me yours...
All he needs is a mechanism for multiplying some value by 10 and
adding another value to it. The 'other value' need not exceed 10.
Don't drop any digits.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

Jul 14 '08 #5
CBFalconer wrote:
Ben Bacarisse wrote:
>fermineutron <fr**********@yahoo.comwrites:
>>I am trying to write a function which will convert a large ascii
number, say 100 ascii digits, to its binary representation.

Homework? If not, use a bignum library (it will pay eventually).
If it is homework, I'll show you mine if you show me yours...

All he needs is a mechanism for multiplying some value by 10 and
adding another value to it. The 'other value' need not exceed 10.
Don't drop any digits.
The OP says he is using a 100 digit number. None of the Standard
integral types of C can hold such a large value. He either needs to
deal with it piece by piece (as bignum routines do) or manipulate it in
it's string form, or use an available bignum library like GMP.

Jul 14 '08 #6
CBFalconer <cb********@yahoo.comwrites:
Ben Bacarisse wrote:
>fermineutron <fr**********@yahoo.comwrites:
>>I am trying to write a function which will convert a large ascii
number, say 100 ascii digits, to its binary representation.

Homework? If not, use a bignum library (it will pay eventually).
If it is homework, I'll show you mine if you show me yours...

All he needs is a mechanism for multiplying some value by 10 and
adding another value to it. The 'other value' need not exceed 10.
Don't drop any digits.
Another way is to have a method to halve a number (easy even when the
number is a digit string) and a test for parity and zero.

--
Ben.
Jul 14 '08 #7
"fermineutron" <fr**********@yahoo.comwrote in message
news:ad**********************************@m45g2000 hsb.googlegroups.com...
>I am trying to write a function which will convert a large ascii
number, say 100 ascii digits, to its binary representation. It seems
that evey algorithm I am trying to think of is backwards. Normaly in
pen and paper ascii to binary conversion one would start by
subtracting largest power of 2 that can be subtracted from the given
number and work from left to right filling memory up with 1s and 0s.
In my case I cant really do that b/c the largest power of 2 which
would fit into a number i am trying to convert is too large even for
64 bit int.

Any help, suggestions, sample code would be greatly apreciated.
Your 100-digit number would need over 300 binary digits. You need to decide
exactly what data structure to use.

Different numbers will all have different lengths so you also have to decide
whether to have a variable length structure for each number, or have the
same maximum size for all. And you have to decide how to represent negative
values and so on.

Then, you might look at the best ways of converting the text to binary,
which usually involves multiplying by ten then adding the next digit.

But if you use an existing library for this stuff, that will specify what
data structure to use.

--
Bartc
Jul 14 '08 #8
santosh wrote:
CBFalconer wrote:
>Ben Bacarisse wrote:
>>fermineutron <fr**********@yahoo.comwrites:

I am trying to write a function which will convert a large ascii
number, say 100 ascii digits, to its binary representation.

Homework? If not, use a bignum library (it will pay eventually).
If it is homework, I'll show you mine if you show me yours...

All he needs is a mechanism for multiplying some value by 10 and
adding another value to it. The 'other value' need not exceed 10.
Don't drop any digits.

The OP says he is using a 100 digit number. None of the Standard
integral types of C can hold such a large value. He either needs to
deal with it piece by piece (as bignum routines do) or manipulate
it in it's string form, or use an available bignum library like GMP.
Where did I say to use a standard C integral type? Where did I say
to use a standard C type? The 'struct' keyword allows creation of
your own types. :-)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Jul 14 '08 #9
Thanks for all of the replies, this is not a homework problem, let me
assure you of that. The reason why i stayed away from using large int
libraries is b/c I want to manipulate the number as a whole and its
parts mathematically. I know I am not the smartest man in the world,
nether the less I want to try my hand at the classical large number
problem. My approach is as follows:

Given Large number N which is the product of 2 primes.
Let N=(a1*2^n + b1)*(a1*2^n+b2)
here n is the size of b1 and b2 in bits.
Foiling yields:
1) The last 2*n bits of N depending on values of b1 and b2.
2) The last n bits of N depend on ONLY b1 and b2.
The algorithm then becomes:
1) Choose a X0 numbers of 2n bits in length with last n bits equal to
right most n bits of N, such that can be factored.
2) Generate 2 vectors v10, v20, elements of which are the factors of
elements of X0.
3) repeat steps 1 and 2 increasing the length of items in Xj by n bits
at a time. Only include into Xj numbers which have factors with least
significant bits completely defined by numbers from v1j-1 and v2j-1.
Repeat until Xj consists of only 1 number which is N.

Theoretically there is no need to keep track of v2j but having it at
hand might speed up the inner loop.

Any 1 can suggest best approach to implement this algorithm of can
tell me why its the most stupid thing you have ever heard?
Theoretically this algorithm should coverage in P(m) time where m is
length of N in bits. Even though you are potentially increasing the
number of operations by factor of 2 per bit moved left, that is nor
really so b/c with each shift you are imposing new restrictions to the
existing pool of possible factors, reducing its size.

Jul 15 '08 #10
Now that I posted the algorithm I am kind of afraid, what if someone
smarter than me writes the code to do it and it actually works. Bye
Bye: PGP, SSL etc...
Jul 15 '08 #11
fermineutron <fr**********@yahoo.comwrites:
Now that I posted the algorithm I am kind of afraid, what if someone
smarter than me writes the code to do it and it actually works. Bye
Bye: PGP, SSL etc...
You will get fame and fortune for inventing it (if it is new and
interesting). For what it is worth, I could not follow what you
wrote. It would help (since you have some trouble with English) if
you wrote the algorithm in maths rather than using words. However...

You don't (yet) have a C question. You should ask in a cryptographic
group (sci.crypt?) or maybe a general group like comp.programing.

--
Ben.
Jul 15 '08 #12
rio

"CBFalconer" <cb********@yahoo.comha scritto nel messaggio
news:48***************@yahoo.com...
Ben Bacarisse wrote:
>fermineutron <fr**********@yahoo.comwrites:
>>I am trying to write a function which will convert a large ascii
number, say 100 ascii digits, to its binary representation.

Homework? If not, use a bignum library (it will pay eventually).
If it is homework, I'll show you mine if you show me yours...

All he needs is a mechanism for multiplying some value by 10 and
adding another value to it. The 'other value' need not exceed 10.
Don't drop any digits.
i remember for doing above decimal
to binary there is the need of "long" division for 2
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


Jul 18 '08 #13

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

Similar topics

6
by: guillaume | last post by:
I have to read and process a large ASCII file containing a mesh : a list of points and triangles. The file is 100 MBytes. I first tried to do it in memory but I think I am running out of memory...
10
by: Tuvas | last post by:
I've been thinking about writing a program to generate the world's largest prime numbers, just for the fun of it. This would require being able to hold an 8000000 digit number into memory (25...
7
by: Drake | last post by:
Well, I'm stuck in legacy land and I need a helping hand. We're trying to give some modern value-added functionality to a circa-1985 fortran proggie. The program produces a binary file, by...
4
by: oddstray | last post by:
Hi, I have a number which is larger than the max unsigned long int. I don't have 64-bit integers available to me. I need to get the resulting 40-bit hex string. I can't find any algorithm...
15
by: Peter Afonin | last post by:
Hello, I'm struggling with the string conversion to MD5 which I've never user before. I have a string that I need to encode which looks approximately like this: ...
1
by: NevilleDNZ | last post by:
Hi, Apologies first as I am not a unicode expert.... indeed I the details probably totally elude me. Not withstanding: how can I convert a binary string containing UTF-8 binary into a python...
9
by: Aamir Mahmood | last post by:
Hi, I have working on a system in which I have to manipulate *very* big numbers. Like 32368060745625089670148189374568111100874165870871388541651800834565616109380834613212956588769877 They...
4
by: vcnewbie | last post by:
Hi I'm maintaining a VisualC++ project to increase its security regarding stored passwords. I thought about using SHA256Managed to create a hash for the password when creating a user and when...
1
by: Vic | last post by:
I have a c program which writes mac address entries onto a text file. Text file when opened in vim looks like this index mac 1 ^@^@^Q^@^@^A ascii of (00.00.11.00.00.01) 2 ^@^@^Q^@^@^B...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.