473,801 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3715
On Sun, 13 Jul 2008 17:52:13 -0700 (PDT), fermineutron
<fr**********@y ahoo.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**********@y ahoo.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...@y ahoo.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**********@y ahoo.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**********@y ahoo.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********@yah oo.comwrites:
Ben Bacarisse wrote:
>fermineutron <fr**********@y ahoo.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
"fermineutr on" <fr**********@y ahoo.comwrote in message
news:ad******** *************** ***********@m45 g2000hsb.google groups.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:
>>fermineutro n <fr**********@y ahoo.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

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

Similar topics

6
6564
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 therefore I decide to use the shelve module to store my points and elements on disks. Despite the fact it is slow ... Any hint ? I think I have the same memory problem but I don't understand why since my aPoint should be removed by the gc.
10
4997
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 megabits, or a little over 3 megs of memory for just one variable...) I would also need several smaller variables. This came about as I optimised a prime number generator more and more, until I came with the idea to try to find the largest ever, using...
7
1878
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 itself no problem... each record needs to be converted into std::vector<mystruct> I'm having a helluva time with the binary-->(pod)datatype conversion. Although I've referred to C++ containers, iostreams
4
5901
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 or solution when I search the web. I've seen a couple of suggestions searching Usenet, but neither of them works. One tried to use a union, and the other tried to convert each byte of the float.
15
2442
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: "pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Email=petera_gudzon.net:lang =ru:shp_PaymentNo=20040825205308:shp_UserID=pva:shp_Price=2.95:shp_HostPlan= BU:shp_Term=2"
1
2597
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 unicode string? cutdown example: $ cat ./uc.py #!/usr/bin/env python imported="\304\246\311\231\316\257\316\271\303\222
9
10057
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 may be upto 10000 digits long. These numbers are coming through a device in ascii format, I am creating a text file and saving these numbers in the file.
4
2244
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 this new user tries to login, a new hash will be created for the given password and compared to the stored hash. I guess this is quite common.
1
2605
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 ascii of (00.00.11.00.00.02) .. ........ Likewise I read the mac entries from the text file using a perl script and do some processing
0
9698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9558
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10057
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9106
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
7595
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
6834
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
5492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4267
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
2963
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.