for (int k = 0; k < 4; k ++) {
kkk[k]= String(bcd_rx[k], HEX); }
now String kkk[] has the hex values.
the question is how can i fill a byte array with the kkk values with each
value occupying one byte (most of all are not achii chars, sprintf not seem to work?).
if i fill a string array: say that the kkk[1]=43 so the char will be "+" and NOT "C"
which is the char with hex code 43.
or to see it from another view how can i convert the string kkk="43" to a hex number? (i think atoi dont work with non-char vals).
for (int k = 0; k < 4; k ++) {
kkk[k]= String(bcd_rx[k], HEX); }
now String kkk[] has the hex values.
The kkk array contains character representations of the hex values, NOT hex values.
The whole task is:
take an 8 digit number like: 43512345
Where does this number come from?
Assuming 43 is supposed to represent 0x43, the next value, 44 would represent 0x44, etc. So, 49 would represent 0x49. The next hex value is 0x4A. How are you going to represent the "decimal value" 4A in your scheme?
Convert it to a series of 4 bytes like: [43][51][23][45]
You'll need to first convert it to a string, using sprintf, then to 4 strings, then to 4 ints using atoi.
" Command 01 - Set frequency: The current frequency is set using 4 BCD bytes. To set a frequency such as 435.12345 MHz, data bytes 1-4 would be [43][51][23][45] followed by the the set frequency command [01] " .
Send what to the serial port?
Something like : Serial.write( ft817_set_rx_frequency[]={0x43,0x51,0x23,0x45});
with the updated bytes in it.
in my Arduino IDE atoi dont work, i dont know why.
kkk= String(bcd_rx[k], HEX);
int a=atoi(kkk);
returns: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'
To set a frequency such as 435.12345 MHz, data bytes 1-4 would be [43][51][23][45]
I can follow this. Why the function doesn't take a string, I can't imagine, but I'll accept that the developer has his/her reasons.
Something like : Serial.write( ft817_set_rx_frequency[]={0x43,0x51,0x23,0x45});
It does not, though, follow that the values that are to be sent to the serial port are hexadecimal values. In fact, it appears as though the values MUST be decimal. There is no frequency 43A.12F45 MHz, which would be possible if the values really were hex.
returns: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'
There are two ways around this issue. The first is that the String class has a toInt() method that can replace the call to atoi. The second is that the String class has a toCharArray() method that gets the String as a string, which atoi() can handle.
However, the toCharArray will return a character array that atoi() can't handle, because atoi() doesn't understand hex.
uint8_t toInt(char c)
{
switch(c)
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'a': return 0xA;
case 'A': return 0xA;
case 'b': return 0xB;
case 'B': return 0xB;
case 'c': return 0xC;
case 'C': return 0xC;
case 'd': return 0xD;
case 'D': return 0xD;
case 'e': return 0xE;
case 'E': return 0xE;
case 'f': return 0xF;
case 'F': return 0xF;
}
return 0;
}
uint8_t hexCharsToByte( char in[2])
{
return toInt(in[0]) | (toInt(in[1]) << 4);
}