Solved

convert a string to 2 unsigned long

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

730 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