Converting byte array to decimal values

Hello guys,

I have a byte array full of ASCII chars that id like to convert to decimal with the same value.

For example, is a byte is "7" ASCII, the decimal value is 55, yet i need to convert it to 7 decimal in order to send the data out in proper format.

any ideas how to convert ASCII chars to their matching DECIMAL values?
thanks.

Delta_G:
http://www.cplusplus.com/reference/cstdlib/atoi/

i tried using atoi and am getting the following error:

invalid conversion from 'byte {aka unsigned char}' to 'const char*' [-fpermissive]

notice my array is of type BYTE.

Delta_G:
You'll have to cast it to an array of char.

byte someArray = {48, 48, 50, 51,0};  //0,1,2,3 in ASCII plus null terminator

int val = atoi ((char*)someArray);




Don't forget to terminate your array with a 0. If it isn't already then you may need to copy to an array one spot larger so you can put it there.

Thank you,

i tried this:

int Batt_Volt3 = atoi ((char*)buff[idx]));

where buff is my buffer of type BYTE.

yet im getting an error:
jetcat: 266: error: expected ')' before 'char'

Delta_G:
byte someArray[] = {48, 48, 50, 51,0}; //0,1,2,3 in ASCII plus null terminator

--> missing some [] brackets for the array.

Alternatively if you don't have the trailing null character you can do something like this

  byte someArray[] = {51, 50, 49, 48}; //3,2,1,0 in ASCII NO null terminator

  unsigned long val = 0;
  for (byte i = 0; i < sizeof(someArray); i++) val = 10 * val + (someArray[i] - '0');

then val is your value and in that case will be 3210

i3dm:
i tried this:
int Batt_Volt3 = atoi ((char*)buff[idx]));
where buff is my buffer of type BYTE.

--> atoi() expects a c string as parameter (a null terminated string of chars)

Delta_G:
You want a pointer to the array. So you either have to lose the [idx] or put a * in front of buff.

both methods still create errors - would you mind posting the line as it should be in the code?
also what will be the value of Batt_Volt3 after this conversion?

post the way your array looks like.

you have 2 pieces of code above, one form Delta_G, one from myself that are building your value from a byte array.

so not sure exactly what you need

There's mismatched brackets here (2 opening and 3 closing) although that error message isn't quite correct for some reason.:

int Batt_Volt3 = atoi ((char*)buff[idx]));
....
....
jetcat: 266: error: expected ')' before 'char'

so apperantly the short answer to converting ASCII numbers to their decinal values is:

dec num = ascii num - 48

thats all :slight_smile:

My array is comma seperated between numbers, and their values change from 1-6 digits, so i guess one digit at a time would be effective.

My array is comma seperated between numbers, and their values change from 1-6 digits, so i guess one digit at a time would be effective.

Anything else we need to know about your array and you forgot to mention?

Looks like you want to parse a string made up of different integers, comma separated. Is that the case? What do you know about the string? fixed number of elements? Max size of the int? can they be negative? atoi(), strtok(), sscanf,... many ways to skin that cat