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"
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...