Data type confusion, need integer from char array

130 Views Asked by At

Once again I have the wrong data type. This is an arduino project.

I have a char array. The last 9 characters are rgb, I get them as triplets. So 000255000.

I need to pass those to a function but as integers, like 0, 255, 0. I'm ok if 000 turns into 0, but I need 045 to turn into 45.

I've tried to cast them, like:

blue = (int)message[11];
blue += (int)message[12];
blue += (int)message[13];

That did not work. I could however cast them to strings,which I did, then I tried: Yes, I know this was not a great idea, but it was worth a shot.

char tempBlue[4];
blue.toCharArray(tempGreen, sizeof(tempGreen));
iBlue = atoi(tempGreen);

That also did not work.

I'm lost as to how to do this. I have no idea how ( if you can ) concatenate integers or I would have tried that.

EDIT------

Am I asking the wrong question. Should I be doing this the reverse way around. Concatenate first then to integer? I have them as characters to begin with.

5

There are 5 best solutions below

5
On BEST ANSWER

To convert each character to its respective int do the following

int first = message[11] - '0';
int second= message[12] - '0';
int third = message[13] - '0';

To see why this works, you can check here: Why does subtracting '0' in C result in the number that the char is representing?

To concatenate ints, you could use this function

unsigned concatenate(unsigned x, unsigned y) {
     unsigned pow = 10;
     while(y >= pow)
         pow *= 10;
     return x * pow + y;        
}

I did not write this function, it was written by @TBohne originally here

0
On

you could try something like this

#include <stdio.h>

int main()
{
  // example
  char message[] = { "000255000" };
  int r=0, g=0, b=0;

  // make sure right number of values have been read
  if ( sscanf(message, "%03d%03d%03d", &r, &g, &b ) == 3 )
  {
    printf( "R:%d, G:%d, B:%d\n", r,g,b );
  }
  else
  {
    fprintf(stderr, "failed to parse\n");
  }
}

sscanf will skip any white space so even a string like message[] = " 000 255 000 "; will work.

0
On

Just do the conversion manually:

int convert_digits(char *cp, int count) {
    int result = 0;
    for (int i = 0; i < count; i += 1) {
        result *= 10;
        result += (cp[i] - '0');
    }
    return result;
}

char *input = "000045255";

int main(int ac, char *av[]) {
    printf("r=%d g=%d, b=%d\n",
        convert_digits(cp, 3),
        convert_digits(cp+3, 3),
        convert_digits(cp+6, 3));
}
1
On

I'd go with:

char *partMessage = (char*)malloc(4);
partMessage[3] = '\0';
strncpy(partMessage, message + 11, 3);
result = (int)strtol(partMessage, NULL, 10);
free(partMessage);
0
On

Convert string to a number and then numerically peal off 3 digits at a time.

const char *s = "000255000";
char *endptr;
unsigned long num = strtoul(s, &endptr, 10);

// add error checking on errno, num, endptr) here if desired.

blue = num%1000;
num /= 1000;
green = num%1000;
num /= 1000;
red = num%1000;

// Could add check to insure r,g,b <= 255.