(solved) Best way to combine cells of an array

what is the best way to combine or concentrate ever other cell of an array?

here is an over simplified example of what what trying to do

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
char c1[8] = {1, 2, 3, 4, 5, 6, 7, 8};
char v1[4];// becomes {12, 34, 56, 78}
}


void loop() {
Serial.print("done");
 delay(5000);
}

except I am doing with 512 going into 256

Iterate with an index i through v1[] and do something like:

v1[i] = c1[i*2+1] + c1[i*2]*10;

Awesome thank you! that works. Could explain though I don't understand how that work.

well it worked in that setting but not like this

void setup(){
Serial.begin(9600);
    char dump[256]="0";
    char readData[512]="0";//The character array is used as buffer to read into.
   Serial.readBytes(readData,512);//It require two things, variable name to read into, number of bytes to read.
   for (int i=0;i<256;i++){
dump[i] = readData[i*2+1] + readData[i*2]*10;
}
Serial.print(dump[0]);
}

void loop(){
Serial.print("done");
}

Serial.print(dump[0]); I get 5

the data being sent over is "37A5FFFFFFFFFFB0FFFFFFFF37A5FFFFFFFFFFB0FFFFFFFF37A5FFFFFFFFFFB0FFFFFFFF73103304000004560F0000000F0F0F0F00000000000000004C7B14380000A5005D362D6F5A5AC099C0909C519390816C53DC0100A50800000000000000000000314743454331345635345A3135393833340004010E040502020000000000000030353052303030303031313136335000E6A19C4831444B0000000000003333303800BAB9FE73020207130500634C5D41C8E3F2561E842600F488774D970300000C130000000000003911E039A95C6F47C0F50400E3A50D004E011100D9399C506F030000000000000000000000000000000000000000000000000000"

You cannot "concentrate" characters like that. All possible values of a byte or character cell are already defined and spoken for.

Shure you can convert the ASCII hex data to binary data, it's only a little bit more complicated.

hexadecimal and decimal are not the same basedump[i] = readData[i*2+1] + readData[i*2]*10; works only for decimal and as you have an ASCII representation you need to subtract the code for the '0' character to get to a number dump[i] = (readData[i*2+1] - '0') + (readData[i*2]-'0')*10;

in Hexadecimal you need to take into account that you get also 'A' to 'F', so subtracting '0' is only appropriate if you have a digit, otherwise you need to subtract ('A'-10) to the letter and of course multiply by 16 not 10.

Why not put the combined data straight into the target array and save the 512 bytes of the intermediate array ?

Instead of using readBytes(), read each byte individually, do the maths on byte pairs and put the result straight into the target array. Stop when you have read 512 bytes

Last night after asking why that wasn't working I googled it and realised that the data going out of monitor is ASCII its not hex thats why my 256 bin I am sending becomes 512.

what I am trying to do is turn that ASCII coming to the arduino into hex or decimal into an array. so that ASCII 37 becomes ether 55 or 0x37 either one I can work with.

garnerm91:
what I am trying to do is turn that ASCII coming to the arduino into hex or decimal into an array. so that ASCII 37 becomes ether 55 or 0x37 either one I can work with.

see my answer #6

with a function like this

int8_t intValueOfHexaKey(char c)
{
  if ((c >= '0') && (c <= '9')) return c - '0';
  if ((c >= 'A') && (c <= 'F')) return c - 'A' + 10;
  return -1; // error should not happen though...
}

then you have

dump[i] = intValueOfHexaKey(readData[i*2+1]) + 16* intValueOfHexaKey(readData[i*2]); // assuming all chars are legit

but as UKHeliBob said, it's best to combine data straight into the target array as you read them...

J-M-L:
see my answer #6

with a function like this

int8_t intValueOfHexaKey(char c)

{
  if ((c >= '0') && (c <= '9')) return c - '0';
  if ((c >= 'A') && (c <= 'F')) return c - 'A' + 10;
  return -1; // error should not happen though...
}




then you have


dump[i] = intValueOfHexaKey(readData[i*2+1]) + 16* intValueOfHexaKey(readData[i*2]); // assuming all chars are legit




but as UKHeliBob said, it's best to combine data straight into the target array as you read them...

worked like a dream