- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have character arrays of 3 ascii characters representing range values from a sonar. I need to do some calculation on these values now. How can I convert the ascii char array to a valid integer
Ex. Char* [3] = {'2','5','5'} I need to convert to int = 255 Thanks ChaseLink Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In C, you can just use the stdlib.h function ATOI. to convert ascii to integer. In HDL, you take each value, subtract 30, then mutiply by the correct power of ten multiplier to based on the bit location, then sum them all up,
Pete- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Awesome the C method is what I was looking for. however, when I add the following call to convert...
int Sonar_Height_Int = atoi(Sonar_Height); Where Sonar_Height is declared as char* Sonar_Height[3] because the sonar I am communicating with through uart sends data as a set of 3 ascii char digits representing the range distance in inches. I get the following warning... passing arg 1 of `atoi' from incompatible pointer type What does this warning mean and in what way will it affect my program's performance?- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
char* Sonar_Height
The above code declares an array of character pointers. I think you probably want an array of characters instead:char Sonar_Height
Also, the atio function expects a NULL terminated string. You could increase the character array to length 4 and set the last character to NULL.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Oh haha of course I see the mistake of the declaration now. Thank you so much for your assistance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Also look for strtoul() - which will tell you how much of the string was
processed, allowing for much better error detecton. (But I'm not certain that it is in the lib that Altera provide.)- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page