IPB

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Ascii String To Hex
jwilson
post Nov 2 2009, 02:32 PM
Post #1


Regular
*

Group: EstablishedMember
Posts: 75
Joined: 30-November 06
From: Arkansas, USA
Member No.: 2,105



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?

QUOTE
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 )
update Picpack 3.0 Released
Go to the top of the page
 
+Quote Post
Reynard
post Nov 2 2009, 02:59 PM
Post #2


Super Enthusiast
***

Group: EstablishedMember
Posts: 429
Joined: 24-October 07
From: Scotland
Member No.: 3,861



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

CODE
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!

This post has been edited by Reynard: Nov 2 2009, 03:01 PM
Go to the top of the page
 
+Quote Post
jwilson
post Nov 2 2009, 03:26 PM
Post #3


Regular
*

Group: EstablishedMember
Posts: 75
Joined: 30-November 06
From: Arkansas, USA
Member No.: 2,105



QUOTE (Reynard @ Nov 2 2009, 08:59 AM) *
atoi as it says is a macro for strtoi. Use strtoi if you want to continue parsing your string after the numeric convertion.

CODE
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?
Go to the top of the page
 
+Quote Post
Reynard
post Nov 2 2009, 03:32 PM
Post #4


Super Enthusiast
***

Group: EstablishedMember
Posts: 429
Joined: 24-October 07
From: Scotland
Member No.: 3,861



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
Go to the top of the page
 
+Quote Post
jwilson
post Nov 2 2009, 05:52 PM
Post #5


Regular
*

Group: EstablishedMember
Posts: 75
Joined: 30-November 06
From: Arkansas, USA
Member No.: 2,105



QUOTE (Reynard @ Nov 2 2009, 09:32 AM) *
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.
Go to the top of the page
 
+Quote Post
jwilson
post Nov 3 2009, 10:39 PM
Post #6


Regular
*

Group: EstablishedMember
Posts: 75
Joined: 30-November 06
From: Arkansas, USA
Member No.: 2,105



Does this look okay? The array rx_buffer is global.

CODE
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;
Go to the top of the page
 
+Quote Post
Reynard
post Nov 4 2009, 09:20 AM
Post #7


Super Enthusiast
***

Group: EstablishedMember
Posts: 429
Joined: 24-October 07
From: Scotland
Member No.: 3,861



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:
CODE
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
Go to the top of the page
 
+Quote Post
jwilson
post Nov 4 2009, 02:23 PM
Post #8


Regular
*

Group: EstablishedMember
Posts: 75
Joined: 30-November 06
From: Arkansas, USA
Member No.: 2,105



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.

This post has been edited by jwilson: Nov 4 2009, 02:23 PM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 6th September 2010 - 11:28 PM