Jump to content

Recommended Posts

I am receiving ASCII characters in my UART and storing them into a character array. I need to take 3 or 4 of those chars in the array and evaluate them. They will be one number (e.g. 139). I know how I can do this a sloppy way, just taking each char, converting to decimal, etc.

 

What I wanted to know was if there was a built in function that would basically do this for me?

 

I found this function in the manual, but was a little unclear on how it worked. Can anyone give me a little more information on it and its uses?

 

int atoi( const char* buffer )

(Macro) ASCII to integer. A macro that converts the numerical character string

supplied into a signed integer (16 bit) value using a radix of 10.

buffer: Pointer to a numerical string.

Return: The converted value.

Note: Macro implemented as: #define atoi( buffer ) strtoi( buffer, NULL, 10 )

Link to post
Share on other sites

atoi as it says is a macro for strtoi. Use strtoi if you want to continue parsing your string after the numeric convertion.

 

char myBuffer = "1234ABCD"
unsigned int myInt;
char *myBuf;

myInt = strtoi(myBuffer, &myBuf, 10);

 

myInt will contain 1234

*myBuf will point to "ABCD"

 

The radix is 10 in this example therefore convertion will stop at any non-numeric char (0-9) or end of string.

 

Cheers

 

Reynard

 

Why can I never get the spelling of conversion correct. Doh!

Edited by Reynard
Link to post
Share on other sites
atoi as it says is a macro for strtoi. Use strtoi if you want to continue parsing your string after the numeric convertion.

 

char myBuffer = "1234ABCD"
unsigned int myInt;
char *myBuf;

myInt = strtoi(myBuffer, &myBuf, 10);

 

myInt will contain 1234

*myBuf will point to "ABCD"

 

The radix is 10 in this example therefore convertion will stop at any non-numeric char (0-9) or end of string.

 

Cheers

 

Reynard

 

Why can I never get the spelling of conversion correct. Doh!

In your example lets say the string was "ABCD1234" instead. Would you set *myBuf to point at mybuffer[4] instead of starting at the front of the array?

Link to post
Share on other sites

You would have to start on a numeric digit. You could scan the buffer using isanum or something until you find a number or go straight there is it is a fixed offset.

 

myInt = strtoi(&myBuffer[4], &myBuf, 10);

 

myBuf is a return pointer value.

 

Cheers

 

Reynard

Link to post
Share on other sites
You would have to start on a numeric digit. You could scan the buffer using isanum or something until you find a number or go straight there is it is a fixed offset.

 

myInt = strtoi(&myBuffer[4], &myBuf, 10);

 

myBuf is a return pointer value.

 

Cheers

 

Reynard

Thanks Reynard, that answers my questions.

Link to post
Share on other sites

Does this look okay? The array rx_buffer is global.

 

unsigned int weight()
{
char *weight_ptr;
unsigned int weight_int;
unsigned char digit_flag;
unsigned char j;

for(j=0; digit_flag != 0; j++)//find where digits start
{
	digit_flag = isdigit(rx_buffer[j]);
}

weight_int = strtoi(&rx_buffer[j], &weight_ptr, 10);//convert string to int

return weight_int;

Link to post
Share on other sites

It looks OK but what happens if there are no digits in the string. This may not occur in reallity on your scale.

 

Here is an alternative idea:

int weight(void)
{
char *weight_ptr;
int weight_int;
unsigned char j;

weight_ptr = rx_buffer;
for (j = strlen(rx_buffer); j != 0; --j)
{
	if (isdigit(*weight_ptr))
	{
		return strtoi(weight_ptr, &weight_ptr, 10);
	}
	++weight_ptr;
}
return -1;
}

 

I have made the return value signed to return an error value of -1 if nothing found. Also you don't want to scan the complete buffer if the string contains less characters or you may detect digits from some othe use of the rx_buffer. Only you know the actual usage of the buffer so I am just trying to do a catch all situation.

 

Cheers

 

Reynard

Link to post
Share on other sites

I didn't even think about if no digits were there. My way would give me an endless loop, and that is not good.

 

I am still a little wet behind the ears at coding, I really appreciate the help you have given me.

 

Thanks yet again.

Edited by jwilson
Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...