Solved

convert a string to 2 unsigned long

Posted on 1998-08-04
5
299 Views
Last Modified: 2010-05-18
How do I convert a character string to 2 unsigned longs if my system doesn't support 64bits data type such as double ? (which means I cannot use atof() to
convert a string to double number and then convert to 2 unsigned long).

For instance:

   char DOUBLE[30] = "50393836554747838837636355443";  // string format of a  
                                                                                                   // double number
   
   I cannot use:   double d = atof(DOUBLE); since my system doesn't have
   the primitive type "double".

   unsigned long LO = first 32 bits of the DOUBLE
   unsigned long HI =  second 32 bits of the DOUBLE
0
Comment
Question by:bsmoam1
  • 3
5 Comments
 
LVL 3

Accepted Solution

by:
sailwind earned 100 total points
ID: 1251945
Why don't you divide the string DOUBLE into 2?

For example, do something like:

int i;
char LoDouble[15], HiDouble[15];

for(i=0;i<15;i++)
  LoDouble[i]=DOUBLE[i];
for(i=0;i<15;i++)
  HiDouble[i]=DOUBLE[i+15];

lo = atof(LoDouble);
hi = atof(HiDouble);

and use that to get the long values.
0
 

Author Comment

by:bsmoam1
ID: 1251946
I cannot use following declaration:

   char LoDouble[15], HiDouble[15];

since I the length of the DOUBLE string is not fix.
The DOUBLE string is obtained from user's input, the can
be as small as "1" or as big as "992885...."(more than 30 digits)
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1251947
Okay, that's fine. I'm assuming that you'll have a DOUBLE string that's long enough
to hold the char value.

Then what you do is get the size of the string, with

int size = strlen(DOUBLE);

float midpoint = size/2;

int losize = floor(midpoint);
int hisize = ceil(midpoint);

Declare the 2 strings with:

char LoDouble[losize], HiDouble[hisize];

This divides it up into 2 strings which you can use atof on.






0
 
LVL 84

Expert Comment

by:ozo
ID: 1251948
30 digits won't fit in 64 bits
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1251949
ozo's right, it wouldn't

a 64bit unsigned double would be able to hold 2^64 number combinations
which means it can hold numbers 0 -  1.84467440737e+19   20 digits

a 32bit unsigned long would be able to hold 2^32 number combinations
which means it can hold numbers 0 -  4,294,967,295  10 digits

However, it is posible to store a 20 digit string into 2 unsigned long, since
each long can hold ~ 10 digits. But you've got to watch the 4.29 billion limit

If I'm correct, if you want to store a 30 digit string, you would have to save
it in 3 or more unsigned longs.

Hope this helps your cause.




0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now