Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# convert a string to 2 unsigned long

Posted on 1998-08-04
Medium Priority
305 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
Question by:bsmoam1
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 3
5 Comments

LVL 3

Accepted Solution

sailwind earned 200 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

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

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

ID: 1251948
30 digits won't fit in 64 bits
0

LVL 3

Expert Comment

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

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
###### Suggested Courses
Course of the Month9 days, 4 hours left to enroll

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

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