Solved

convert a string to 2 unsigned long

Posted on 1998-08-04
5
296 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

706 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

19 Experts available now in Live!

Get 1:1 Help Now