Home Arduino Code Converting ASCII to Integer in your Arduino or other C code (specifically in a VirtuabotixPacket)
Converting ASCII to Integer in your Arduino or other C code (specifically in a VirtuabotixPacket)

Converting ASCII to Integer in your Arduino or other C code (specifically in a VirtuabotixPacket)

0

So by this point we have the ability to transmit and receive data packets between Arduino/Versalino systems and any other serial device (to include another Arduino). Now the big question is how to translate data that is sent on our packets into a form usable by our micro-controller subroutines, and that is where ASCII to INT conversions come into play.

First off let’s go through the details of how to convert ASCII to INT in a general form that can be applied to C, C++, and Arduino coding with relative ease. Once we have gone over the process we will show how to process and convert ASCII data in a VirtuabotixPacket into an Integer.

[wdsm_ad id=”68″ class=” ” ]


#include <stdio.h>//you must include this library.
char conversionBuffer[5];//this is where we will store our ASCII data
//remember you always need to be at least 1 more than your number of characters
//so that you can store a null character to indicate the end of the string.
int myConvertedNumber=0;//this is where we will store the conversion
void setup()
{
Serial.begin(9600);//for serial output
}
void loop()
{
conversionBuffer[0]='-';//we store "-999" followed by null character 0
conversionBuffer[1]='9';
conversionBuffer[2]='9';
conversionBuffer[3]='9';
conversionBuffer[4]=0;
myConvertedNumber=atoi(conversionBuffer);//we convert and store the number into myConvertedNumber
Serial.println(myConversionNumber, DEC);//output for viewing pleasure over the Serial port.
}

Okay so in the code above what is probably the most complicated concept to understand is what I called the “Null Character” in the code above. Basically when C functions parse through an array of characters they look for the null character to determine when the string is over. The reason that this has to be the case is because of the way that character arrays are handled by the functions themselves.

When you parse through an array with no defined length you could technically continue reading data on into infinity from the rest of your device or computers memory. This is both a blessing and a curse when it comes to C based (and other pointer supporting languages) because it allows for a very dynamic use of memory, but it also opens us up for going out of bounds on accident, and even going as far as to write to other portions of our memory if we aren’t careful. Luckily atoi is a read only operation, but you still want to ensure that you put a null character at the end of your character array to keep it from parsing through adjacent variables and giving you numbers you certainly wouldn’t otherwise expect.

Once you understand the null character and it’s role in the operation, the remaining functionality is actually quite simple. The atoi function simply performs a few mathematically operations on each character, and calculates a resulting number just like you or I would when processing the written number into a usable figure in our minds.

Below we will go over how to implement the atoi function into 4 byte data packet processing (as is set up in this guide on Packet format changing)


#include <stdio.h> //you must include this library to get the atoi function
char conversionBuffer[5];//this is where we will store our ASCII data
int myConvertedNumber=0;//this is where we will store the conversion

Be sure to add the library call, and the variable declarations to the top of your sketch (don’t forget to make your VirtuabotixPacket structure support 4 byte messages, or change this sketch to only populate 3 bytes and a null character on byte 4 instead of 5).

{
while(listenforcommand())//loop while messages are still in the queue
{
if (myPacket.received==true && myPacket.target=='R') //if our target "R"
{
conversionBuffer[0]=myPacket.msg1;
conversionBuffer[1]=myPacket.msg2;
conversionBuffer[2]=myPacket.msg3;
conversionBuffer[3]=myPacket.msg4;
conversionBuffer[4]=0;
myConvertedNumber=atoi(conversionBuffer);//we convert and store the number into myConvertedNumber
//do something with the number, and make it something clever!
}
}

There you have it, you can now parse integers from ASCII messages. Of course if you are at all familiar with data processing you are shouting “OMG that is the most inefficient way you could transfer data!” And, though I totally agree, this method is still quite handy when you are trying to interface with an existing protocol that uses ASCII instead of storing the actual byte values in the bytes themselves (which is admittedly much more efficient).

That said this method has one benefit you may not be aware of particularly when it comes to packets that are being sent over a serial port. Recall that we decided to use “||” to mark our packet headers, what then would happen if our raw bytes for our message number actually ended up being a “||” themselves? This would be perfectly possible considering that a | is just another character after-all.

“Well you look for a specific length for your packets so you should be fine.” you might say, and for most cases you would be right, but what if your receiving end device gets a buffer overrun and misses the real header, and gets the identical data that looks like a header? The answer is who knows what would happen, but there is a chance that it would just so happen to match one of your existing targets, and tell it to do something weird. So, though that chance is extremely remote, I mention it as a caution here.

Anyways, I hope that helped you solve the problem of ASCII to Integer conversion in your Versalino/Arduino or other C project. I can’t wait to share the next step with you as we develop the VirtuabotixPacket and other projects together.

[wdsm_ad id=”68″ class=” ” ]

Joseph Dattilo Writer, Electrical Engineer, CEO and founder of Virtuabotix LLC, and completely crazy in every way.

LEAVE YOUR COMMENT

Your email address will not be published. Required fields are marked *