# atoi of first character in a string converts the whole string

• (2 Pages)
• 1
• 2

## 21 Replies - 3206 Views - Last Post: 27 July 2012 - 03:04 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=287030&amp;s=42851e77efa3222003d4d6b7b37c6010&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 seijurojushi

• New D.I.C Head

Reputation: 1
• Posts: 35
• Joined: 24-July 12

# atoi of first character in a string converts the whole string

Posted 26 July 2012 - 10:14 AM

As mentioned, the first iteration of the loop below converts the whole string instead of just the first character, whereas the rest of the conversions happen one character at a time. I've had this problem several times before but managed to overcome it by somehow modifying the first number I got. Since now I've found this great site, I will ask for your help
The code shown is a constructor in a class I'm writing to handle large integers. It's meant to take the argument number in string form that it's constructed with and convert it into an array of shorts where each digit is at a separate index so that operations can be performed on each digit separately.

After compiling the following using "1000" as the argument the contents of the array are:
number[0]: 1000
number[1]: 0
number[2]: 0
number[3]: 0

```
BigInt(string numberStr)
{
numSize = numberStr.size();
number = new short[numSize];
for(int i=0; i<numSize; i++)
{
number[i] = atoi(&numberStr.at(i));
}
}

```

Is This A Good Question/Topic? 0

## Replies To: atoi of first character in a string converts the whole string

### #2 Skydiver

• Code herder

Reputation: 6114
• Posts: 21,049
• Joined: 05-May 12

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 10:26 AM

Yes, taking as much of the string as possible is the expected behavior of atoi(): http://en.cppreferen...tring/byte/atoi

If you know that the number in the string is base 10 you could simply do:
```for each digit in string:
number[i] = numberStr[i] - '0';

```

Obviously increment i.

This post has been edited by Skydiver: 26 July 2012 - 10:31 AM

Was This Post Helpful? 1

### #3 seijurojushi

• New D.I.C Head

Reputation: 1
• Posts: 35
• Joined: 24-July 12

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 10:43 AM

Uhh if I'm being slow... but I don't really see what that would do... you are assigning a char to a short? and removing a 0 from it? or is there an assumed atoi() there?
Was This Post Helpful? 0

### #4 seijurojushi

• New D.I.C Head

Reputation: 1
• Posts: 35
• Joined: 24-July 12

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 10:59 AM

I dunno if I'm being slow**

alright I just made it like
```char c = numberStr.at(i);
number[i] = atoi(&c);

```

so it doesn't take more than one char... I just thought that something was wrong and there was a right way to do it, but since its how atoi behaves...
Thanks anyway
Was This Post Helpful? 0

### #5 Skydiver

• Code herder

Reputation: 6114
• Posts: 21,049
• Joined: 05-May 12

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 11:02 AM

Silly question time: Why allocate an entire short for a single digit?

So lets say you have a 64 digit decimal number. Allocating an array of 64 short's will use up 128 bytes. Of those 128 bytes you only use 64 bytes because you only use the low byte of each word. Even worse each in each byte, you only use the low nibble. So in short, you are only using 25% of the space you are allocating.

Maybe you'll lose more time trying to juggle between high nibble and low nibbles unless you lean on your CPU's BCD's support and the compiler discerns that you are playing with BCD numbers. Even if you just used one byte per digit, you'll using 64 bytes instead of 128 bytes where 50% is unused.

Presumably you'll be doing some big hairy computations with large numbers and you want every ounce of CPU processing you can get. Wouldn't that mean that you'd also like to get as much data as possible within the CPU's memory cache as possible instead of having it go wait for slower main memory to fill up more of the cache?

Or is the idea: "Memory and CPU time is cheap. Programmer brain cells are expensive. Just use short."? If that is true, why not just use int?
Was This Post Helpful? 0

### #6 seijurojushi

• New D.I.C Head

Reputation: 1
• Posts: 35
• Joined: 24-July 12

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 11:07 AM

Its not about any of that, I just used the smallest data type I knew, my other thought was to just use the strings directly and just a switch statement to find out which number it is, but I thought that it would just waste alot more CPU time.
I've no idea about most of the stuff you mentioned, I've been programming for 2 years... not all of us are experts
I would really like to use something smaller than short, what should I use then?
Was This Post Helpful? 0

### #7 Skydiver

• Code herder

Reputation: 6114
• Posts: 21,049
• Joined: 05-May 12

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 11:12 AM

seijurojushi, on 26 July 2012 - 10:59 AM, said:

I dunno if I'm being slow**

alright I just made it like
```char c = numberStr.at(i);
number[i] = atoi(&c);

```

so it doesn't take more than one char... I just thought that something was wrong and there was a right way to do it, but since its how atoi behaves...
Thanks anyway

That is working because chances are likely that the byte after the c is a non-digit character, so the atoi() is stopping right away. You are lucky that you are not getting a crash.

Try this instead and see if you get the expected results.
```char arr[6] = { '0', '1', '2', '3', '4', '\0' };

int n = atoi(&arr[2]);
cout << n;     // Outputs "234"

```

This post has been edited by Skydiver: 26 July 2012 - 11:13 AM

Was This Post Helpful? 0

### #8 jimblumberg

Reputation: 5451
• Posts: 16,975
• Joined: 25-December 09

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 11:14 AM

Quote

I would really like to use something smaller than short, what should I use then?

I suggest you start by studying this link: Basic Data Types.

Quote

I just thought that something was wrong and there was a right way to do it, but since its how atoi behaves..

How do you think atoi() behaves? You do know that it doesn't work with a single character, correct?

Jim
Was This Post Helpful? 0

### #9 Skydiver

• Code herder

Reputation: 6114
• Posts: 21,049
• Joined: 05-May 12

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 11:15 AM

seijurojushi, on 26 July 2012 - 11:07 AM, said:

Its not about any of that, I just used the smallest data type I knew, my other thought was to just use the strings directly and just a switch statement to find out which number it is, but I thought that it would just waste alot more CPU time.
I've no idea about most of the stuff you mentioned, I've been programming for 2 years... not all of us are experts
I would really like to use something smaller than short, what should I use then?

A char is smaller than short. This maybe a useful reference for you: http://en.cppreferen.../language/types
Was This Post Helpful? 0

### #10 seijurojushi

• New D.I.C Head

Reputation: 1
• Posts: 35
• Joined: 24-July 12

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 11:20 AM

@ jimblumberg
No, I did not know that.
Cool, the thought that you could use char to store an actual number never crossed my mind

@ Skydiver
Ahhh, I see... was never taught that in uni
could you then please further explain to me your first method, I really have no idea what happened there
Was This Post Helpful? 0

### #11 Skydiver

• Code herder

Reputation: 6114
• Posts: 21,049
• Joined: 05-May 12

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 11:24 AM

Somehow your professor in university or the book you were using to learn skipped the classic implement atoi() lesson/exercise.

I'll start typing a quickie lesson, but if Jim beats me to an explanation, I'll stop typing.

This post has been edited by Skydiver: 26 July 2012 - 11:25 AM

Was This Post Helpful? 0

### #12 seijurojushi

• New D.I.C Head

Reputation: 1
• Posts: 35
• Joined: 24-July 12

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 11:28 AM

Oh we did plenty of work with it, we were just told it converts a char into an int
noone mentioned the fact that it keeps going in memory till it reaches a non-char...
Was This Post Helpful? 0

### #13 jimblumberg

Reputation: 5451
• Posts: 16,975
• Joined: 25-December 09

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 11:30 AM

Quote

Ahhh, I see... was never taught that in uni

And I suppose that you were never taught how to research and read the documentation for functions you want to use. You need to learn to find, read, and understand the documentation for the functions you are trying to use. Just blindly throwing a function at a problem without understanding how that function works will lead to nothing but trouble. In today's internet world you have the documentation for these standard functions a few keystrokes and a couple of clicks of the mouse away. Learn to use the readily available resources to help solve your problems. Become friends with the internet search engine of your choice.

Edit:

Quote

noone mentioned the fact that it keeps going in memory till it reaches a non-char...

No, it keeps going until it finds the end of string character '\0'. Remember this end of string character is a character. There is no such thing as a non-character.

Edit 2: atoi() will also stop when it encounters a non-numeric character.

Jim

This post has been edited by jimblumberg: 26 July 2012 - 11:40 AM

Was This Post Helpful? 0

### #14 seijurojushi

• New D.I.C Head

Reputation: 1
• Posts: 35
• Joined: 24-July 12

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 11:35 AM

Yeah I guess you're right, the thought of checking documentation never crosses my mind for some reason. Not to mention the function did what I expected whenever else I used it, so I thought it was just my logic...
Was This Post Helpful? 0

### #15 Skydiver

• Code herder

Reputation: 6114
• Posts: 21,049
• Joined: 05-May 12

## Re: atoi of first character in a string converts the whole string

Posted 26 July 2012 - 11:38 AM

Characters are typically encoded in ASCII. See ASCII table here: http://www.ascii-code.com/

Notice that the number characters '0' through '9' range from values 48 through 57.

So you can do something like:
```char someDigit = '4';

// Slow way
char zero = '0';
int encodingOfZero = zero;          // 48
int encodingOfDigit = someDigit;    // 52
int valueOfDigit = encodingOfZero - encodingOfDigit;    // 4

// Shortcut
int quickValue = someDigit - '0';   // 4

```

This post has been edited by Skydiver: 26 July 2012 - 11:53 AM

Was This Post Helpful? 1

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }