Connecting Tech Pros Worldwide Forums | Help | Site Map

string or character to integer problem

johnny@n0sq.net
Guest
 
Posts: n/a
#1: Jul 22 '05
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



David Harmon
Guest
 
Posts: n/a
#2: Jul 22 '05

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'.

johnny@n0sq.net
Guest
 
Posts: n/a
#3: Jul 22 '05

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.

David Harmon
Guest
 
Posts: n/a
#4: Jul 22 '05

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.

Kai-Uwe Bux
Guest
 
Posts: n/a
#5: Jul 22 '05

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
Kai-Uwe Bux
Guest
 
Posts: n/a
#6: Jul 22 '05

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.

johnny@n0sq.net
Guest
 
Posts: n/a
#7: Jul 22 '05

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]

johnny@n0sq.net
Guest
 
Posts: n/a
#8: Jul 22 '05

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]

JKop
Guest
 
Posts: n/a
#9: Jul 22 '05

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
Serge Paccalin
Guest
 
Posts: n/a
#10: Jul 22 '05

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
JKop
Guest
 
Posts: n/a
#11: Jul 22 '05

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

David Rubin
Guest
 
Posts: n/a
#12: Jul 22 '05

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
Closed Thread