string or character to integer problem 
July 22nd, 2005, 05:35 PM
| | | |
Can anyone tell me what is wrong with the following code fragment?
char a = x[index];
char b = y[index];
int result = strtol(&a, NULL, 10) + strtol(&b, NULL, 10);
I got the same result using result=atoi(&a)+atoi(&b);
cout << a << '\t' << b << endl; shows the values of a and b to be correct.
cout << atoi(&a) (or strtol(&a, NULL, 10)) is correct but atoi(&b) and
strtol(&b, NULL, 10) are incorrect.
This project is suppose to take integers of "unlimited" size and add them.
Mandrake Linux 10, gcc-c++-3.3.2-6mdk, kernel 2.6.3-15mdk | 
July 22nd, 2005, 05:36 PM
| | | | re: string or character to integer problem
On Sat, 17 Jul 2004 23:30:07 GMT in comp.lang.c++, johnny@n0sq.net
wrote,[color=blue]
>Can anyone tell me what is wrong with the following code fragment?
>
> char a = x[index];
> char b = y[index];
> int result = strtol(&a, NULL, 10) + strtol(&b, NULL, 10);[/color]
strtol does not take a pointer to a single char. Very few things do;
when you see (char *) you can usually assume that it is a pointer to an
array of chars, and most often a zero-terminated string.
Using strtol to convert a single char is silly anyway, just subtract
'\0'. | 
July 22nd, 2005, 05:36 PM
| | | | re: string or character to integer problem
David Harmon wrote:
[color=blue]
> On Sat, 17 Jul 2004 23:30:07 GMT in comp.lang.c++, johnny@n0sq.net
> wrote,[color=green]
>>Can anyone tell me what is wrong with the following code fragment?
>>
>> char a = x[index];
>> char b = y[index];
>> int result = strtol(&a, NULL, 10) + strtol(&b, NULL, 10);[/color]
>
> strtol does not take a pointer to a single char. Very few things do;
> when you see (char *) you can usually assume that it is a pointer to an
> array of chars, and most often a zero-terminated string.
>
> Using strtol to convert a single char is silly anyway, just subtract
> '\0'.[/color]
I'm not sure of what you're trying to say about the subtraction.
Anyway, I normally use atoi() to convert a character (or string) to an
integer. There's probably a better way to do what I'm trying to do but this
is a first draft anyway. I need to be able to add 2 large integers that are
larger than can be stored in an unsigned long.
As it turns out, on my system, any integer type uses 4 bytes (according to
the return from sizeof()). Anyway, I'm entering the values as strings and
storing them in a vector.
Besides, I still need to know why the first call to strtol() returns the
correct value while the second call to strtol() doesn't. | 
July 22nd, 2005, 05:36 PM
| | | | re: string or character to integer problem
On Sun, 18 Jul 2004 01:58:27 GMT in comp.lang.c++, johnny@n0sq.net
wrote,[color=blue]
>I'm not sure of what you're trying to say about the subtraction.[/color]
I made a mistake in that part anyway. What I mean is that if a is char
in the range '0' through '9', then a-'0' is the corresponding value in
the range 0 through 9.
[color=blue]
>Anyway, I normally use atoi() to convert a character (or string) to an
>integer.[/color]
Again, atoi(), strtol(), etc are only for converting zero-terminated c
style array strings, never single characters. If it works for single
char variables it is only by good or bad luck.
[color=blue]
>Besides, I still need to know why the first call to strtol() returns the
>correct value while the second call to strtol() doesn't.[/color]
It depends on what happens by chance to follow the variable in memory.
Both the first and thee second are still equally wrong. | 
July 22nd, 2005, 05:36 PM
| | | | re: string or character to integer problem johnny@n0sq.net wrote:
[color=blue]
> David Harmon wrote:
>[color=green]
>> On Sat, 17 Jul 2004 23:30:07 GMT in comp.lang.c++, johnny@n0sq.net
>> wrote,[color=darkred]
>>>Can anyone tell me what is wrong with the following code fragment?
>>>
>>> char a = x[index];
>>> char b = y[index];
>>> int result = strtol(&a, NULL, 10) + strtol(&b, NULL, 10);[/color]
>>
>> strtol does not take a pointer to a single char. Very few things do;
>> when you see (char *) you can usually assume that it is a pointer to an
>> array of chars, and most often a zero-terminated string.
>>
>> Using strtol to convert a single char is silly anyway, just subtract
>> '\0'.[/color]
>
> I'm not sure of what you're trying to say about the subtraction.[/color]
To convert a char c to a digit, you can use:
digit = c - '0';
[color=blue]
> Anyway, I normally use atoi() to convert a character (or string) to an
> integer. There's probably a better way to do what I'm trying to do but
> this is a first draft anyway. I need to be able to add 2 large integers
> that are larger than can be stored in an unsigned long.
>
> As it turns out, on my system, any integer type uses 4 bytes (according to
> the return from sizeof()). Anyway, I'm entering the values as strings and
> storing them in a vector.
>
> Besides, I still need to know why the first call to strtol() returns the
> correct value while the second call to strtol() doesn't.[/color]
You pass &b to strtol, which expects a char*. Since &b is compatible with
char* this will compile. However, strtol thinks of its first argument as
pointing to the *beginning* of a string. So instead of just converting one
digit, you will effectively convert the whole trail of the array y starting
at index. This is what strtol does, but it is not what you want. (And this
was already hinted at in the first reply you quoted.)
Best
Kai-Uwe Bux | 
July 22nd, 2005, 05:36 PM
| | | | re: string or character to integer problem
I need to correct myself:
Kai-Uwe Bux wrote:
[color=blue][color=green][color=darkred]
>>>> char a = x[index];
>>>> char b = y[index];
>>>> int result = strtol(&a, NULL, 10) + strtol(&b, NULL, 10);[/color]
>> Besides, I still need to know why the first call to strtol() returns the
>> correct value while the second call to strtol() doesn't.[/color]
>
> You pass &b to strtol, which expects a char*. Since &b is compatible with
> char* this will compile. However, strtol thinks of its first argument as
> pointing to the *beginning* of a string. So instead of just converting one
> digit, you will effectively convert the whole trail of the array y
> starting at index.[/color]
This is not what happens. Since the variable b lies at some unpredictable
location in memory, it is not the trail of the array y that follows but
some random stuff that may or may not consist of digits. This is what
strtol will try to convert. In fact, the programm could crash as well if it
does not own the memory following &b. | 
July 22nd, 2005, 05:36 PM
| | | | re: string or character to integer problem
Thanks. Yep, (a - '0') is a better way to go.
Kai-Uwe Bux wrote:
[color=blue]
> johnny@n0sq.net wrote:
>[color=green]
>> David Harmon wrote:
>>[color=darkred]
>>> On Sat, 17 Jul 2004 23:30:07 GMT in comp.lang.c++, johnny@n0sq.net
>>> wrote,
>>>>Can anyone tell me what is wrong with the following code fragment?
>>>>
>>>> char a = x[index];
>>>> char b = y[index];
>>>> int result = strtol(&a, NULL, 10) + strtol(&b, NULL, 10);
>>>
>>> strtol does not take a pointer to a single char. Very few things do;
>>> when you see (char *) you can usually assume that it is a pointer to an
>>> array of chars, and most often a zero-terminated string.
>>>
>>> Using strtol to convert a single char is silly anyway, just subtract
>>> '\0'.[/color]
>>
>> I'm not sure of what you're trying to say about the subtraction.[/color]
>
> To convert a char c to a digit, you can use:
>
> digit = c - '0';
>[color=green]
>> Anyway, I normally use atoi() to convert a character (or string) to an
>> integer. There's probably a better way to do what I'm trying to do but
>> this is a first draft anyway. I need to be able to add 2 large integers
>> that are larger than can be stored in an unsigned long.
>>
>> As it turns out, on my system, any integer type uses 4 bytes (according
>> to the return from sizeof()). Anyway, I'm entering the values as strings
>> and storing them in a vector.
>>
>> Besides, I still need to know why the first call to strtol() returns the
>> correct value while the second call to strtol() doesn't.[/color]
>
> You pass &b to strtol, which expects a char*. Since &b is compatible with
> char* this will compile. However, strtol thinks of its first argument as
> pointing to the *beginning* of a string. So instead of just converting one
> digit, you will effectively convert the whole trail of the array y
> starting at index. This is what strtol does, but it is not what you want.
> (And this was already hinted at in the first reply you quoted.)
>
>
> Best
>
> Kai-Uwe Bux[/color] | 
July 22nd, 2005, 05:36 PM
| | | | re: string or character to integer problem
Thanks. That is a better way to go. I don't know why I didn't think of that
earlier since it was something I once learned in my C class many years ago.
I'll get back up to speed again (I hope).
David Harmon wrote:
[color=blue]
> On Sun, 18 Jul 2004 01:58:27 GMT in comp.lang.c++, johnny@n0sq.net
> wrote,[color=green]
>>I'm not sure of what you're trying to say about the subtraction.[/color]
>
> I made a mistake in that part anyway. What I mean is that if a is char
> in the range '0' through '9', then a-'0' is the corresponding value in
> the range 0 through 9.
>[color=green]
>>Anyway, I normally use atoi() to convert a character (or string) to an
>>integer.[/color]
>
> Again, atoi(), strtol(), etc are only for converting zero-terminated c
> style array strings, never single characters. If it works for single
> char variables it is only by good or bad luck.
>[color=green]
>>Besides, I still need to know why the first call to strtol() returns the
>>correct value while the second call to strtol() doesn't.[/color]
>
> It depends on what happens by chance to follow the variable in memory.
> Both the first and thee second are still equally wrong.[/color] | 
July 22nd, 2005, 05:36 PM
| | | | re: string or character to integer problem
Kai-Uwe Bux posted:
[color=blue]
> To convert a char c to a digit, you can use:
>
> digit = c - '0';[/color]
But I don't think that this is portable - for example, in a
certain codepage, there's a gap between the letter 'i' and
'j', ie. they're no sequential.
-JKop | 
July 22nd, 2005, 05:36 PM
| | | | re: string or character to integer problem
Le dimanche 18 juillet 2004 à 11:26:31, JKop a écrit dans
comp.lang.c++*:
[color=blue]
> Kai-Uwe Bux posted:
>[color=green]
>> To convert a char c to a digit, you can use:
>>
>> digit = c - '0';[/color]
>
> But I don't think that this is portable - for example, in a
> certain codepage, there's a gap between the letter 'i' and
> 'j', ie. they're no sequential.[/color]
Letters are not guaranteed to be sequential, but digits are. So the
proposed conversion is portable.
--
___________ 2004-07-18 14:11:04
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763 | 
July 22nd, 2005, 05:36 PM
| | | | re: string or character to integer problem
Serge Paccalin posted:
[color=blue]
> Letters are not guaranteed to be sequential, but digits[/color]
are. So the[color=blue]
> proposed conversion is portable.[/color]
:-)
-JKop | 
July 22nd, 2005, 05:36 PM
| | | | re: string or character to integer problem
JKop <NULL@NULL.NULL> wrote in message news:<bnrKc.4859$Z14.6097@news.indigo.ie>...[color=blue]
> Kai-Uwe Bux posted:
>
>[color=green]
> > To convert a char c to a digit, you can use:
> >
> > digit = c - '0';[/color]
>
>
> But I don't think that this is portable - for example, in a
> certain codepage, there's a gap between the letter 'i' and
> 'j', ie. they're no sequential.[/color]
IIRC, this always works for digits; the standard guarantees that
numerals are consecutive from '0' to '9'. /david |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|