• C

# convert a string to 2 unsigned long

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
###### Who is Participating?

Commented:
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 Commented:
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

Commented:
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

Commented:
30 digits won't fit in 64 bits
0

Commented:
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.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.