473,322 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

converting a char* to long using atol does not work :(

Im not sure if this is a c or c++ question so I apologise if im in the wrong
place.

I want to convert a char* to a long.

However the number that I want to convert appears to be one digit to long to
be converted. Is there any way around this ??

This is what im trying to do.

char *z = "15132770200";

long lShair = atol(z);

The answer it gives is -2047098984

If I reduce the string by one char like so

char *z = "1513277020";

long lShair = atol(z);

I get 1513277020

Is there any way around this ??

Thanks in advance

Gizmo
Jul 19 '05 #1
5 10039
Gizmo <sc***********@hotmail.com> wrote in message
news:bj**********@newsg4.svr.pol.co.uk...
Im not sure if this is a c or c++ question
It's both. :-)
so I apologise if im in the wrong
place.
Your question is certainly topical here.

I want to convert a char* to a long.

However the number that I want to convert appears to be one digit to long to be converted.

The largest value guaranteed to be representable by
type 'long' is 2147483647

Is there any way around this ??
Use a larger type. If one is not available with
your implementation, you'll need a custom 'bignum'
class.

This is what im trying to do.

char *z = "15132770200";

long lShair = atol(z);

The answer it gives is -2047098984
The answer could have been anything. If 'atol()' tries
to convert an out-of-range value, the resultant behavior
is undefined. This is why one generally should not use 'atol()'
at all.
If I reduce the string by one char like so

char *z = "1513277020";

long lShair = atol(z);

I get 1513277020
That value is within the guaranteed range of type 'long',
so it works.
I recommend you not use 'atol()' at all, but use 'strtol()'
instead, which can tell you for sure, in a defined way, if
the value being converted is out of range.
Is there any way around this ??

Thanks in advance

Gizmo


-Mike

Jul 19 '05 #2

"Gizmo" <sc***********@hotmail.com> wrote in message
news:bj**********@newsg4.svr.pol.co.uk...
Im not sure if this is a c or c++ question so I apologise if im in the wrong place.

I want to convert a char* to a long.

However the number that I want to convert appears to be one digit to long to be converted. Is there any way around this ??

This is what im trying to do.

char *z = "15132770200";

long lShair = atol(z);

The answer it gives is -2047098984

If I reduce the string by one char like so

char *z = "1513277020";

long lShair = atol(z);

I get 1513277020

Is there any way around this ??

Thanks in advance

Gizmo

your computer's long type is 32bit, isn't 64bit!
1513277020 < 32bit
15132770200 > 32bit
Jul 19 '05 #3

"Gizmo" <sc***********@hotmail.com> wrote in message news:bj**********@newsg4.svr.pol.co.uk...
This is what im trying to do.

char *z = "15132770200";

long lShair = atol(z);


The number represented above is obviously too big to fit in a long
on your (presumably 32 bit matchine). What you wrote won't work
and nothing else will. If you exceed the size of long, you're going
to be out of luck.

As a matter of fact, what you wrote above is DANGEROUS. atol
is a ultimately stupidly defined function in the C standard. If you feed
it an overflowing input, there is no requirement for any specific behavior
in the failure.

strtol would be better, as would using the C++ formatted conversions.

There's no portable type lartger than long right now. You might want to switch
everything to doubles (use strtod, etc...)

Jul 19 '05 #4

"Gizmo" <sc***********@hotmail.com> wrote in message
news:bj**********@newsg4.svr.pol.co.uk...
Im not sure if this is a c or c++ question so I apologise if im in the wrong place.

I want to convert a char* to a long.

However the number that I want to convert appears to be one digit to long to be converted. Is there any way around this ??

This is what im trying to do.

char *z = "15132770200";

long lShair = atol(z);

The answer it gives is -2047098984

If I reduce the string by one char like so

char *z = "1513277020";

long lShair = atol(z);

I get 1513277020

Is there any way around this ??

Thanks in advance

Gizmo

your computer's long type is 32bit, isn't 64bit!
1513277020 < 32bit
15132770200 > 32bit
Jul 19 '05 #5
"Gizmo" <sc***********@hotmail.com> writes:
Im not sure if this is a c or c++ question so I apologise if im in the wrong
place. [snip] However the number that I want to convert appears to be one digit to long to
be converted. Is there any way around this ??
This is what im trying to do.

char *z = "15132770200";

long lShair = atol(z);

[snip]

If this is a C question you can use long long:

long long lShair= atoll(z);

However atoll has undefined behavior if there is overflow or
underflow. So you are better off with strtoll:

long long lShair= strtoll(z, NULL, 10);

long long, atoll, and strtoll are new with C99. They are not part of
C89 or C++ .

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
2
by: Exits Funnel | last post by:
Hello, I've inherited a bunch of code which was written on windows and makes frequent calls to _wtol( ) which converts a 2 byte char array to a long integer. I'm pretty sure it is a Microsoft...
5
by: wallacej | last post by:
Does anybody know why unsigned char myImage; works but unsigned char myImage; does not? I think its because the second line is a value too big for unsigned char, is this ...
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
3
by: Golan | last post by:
Hello, I have a hexa file which I need to convert to decimal. I use memcpy into variables (char for one octet, short for 2 octets and int for 4 octets) and then print the value into the file by...
18
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
11
by: TomServo | last post by:
I am writing code that needs to run on a variety of Unix systems. I am calling the statvfs and statfs system calls and I need to to convert some of the integers returned to character strings....
74
by: aruna.mysore | last post by:
Hi all, I have a simple definitioin in a C file something like this. main() { char a; ....... int k; }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.