Convert "35" to 0x35

ok.. i know that perhaps its silly question but i am really stack..

i have a serie of decs like 14, 25, 56, 67

and i need to fill an array with values 0x14, 0x25, 0x56, 0x67 (the byte which its hex value is 14 ).

one byte per value to "serial.write" it on a device.

this arey works for me nice:
byte ft817_set_rx_frequency[]={0x14,0x50,0x00,0x00,0x01};

But i can not fill it having the values 14, 50, 00, 00, 01.

any help please?

Panos, Greece

i have a serie of decs like 14, 25, 56, 67

i need to fill an array with values 0x14, 0x25, 0x56, 0x67

Why? 14 != 0x14.

Where do these values (No idea what a dec is) come from?

this arey works for me nice:
byte ft817_set_rx_frequency[]={0x14,0x50,0x00,0x00,0x01};

But i can not fill it having the values 14, 50, 00, 00, 01.

But 14 != 0x14 and 50 !=0x50.

byte ft817_set_rx_frequency[]={20, 80, 0, 0, 1};
should produce EXACTLY the same results, since 20 == 0x14 and 80 == 0x50.

If you want to prepend your values with "0x", this will work:

char str[5];
sprintf(str, "0x%02d", value);
Serial.print(str);

but the actual values are still decimal.

If you want real hex formatting, what the "0x" suggests, replace the middle line with:

sprintf(str, "0x%02X", value);

BTW, works only propery for one or two digit decimals...

i have the DEC values in bcd_rx[]

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

There is the problem.

Thanks for the help.

The whole task is:

  1. take an 8 digit number like: 43512345
  2. Convert it to a series of 4 bytes like: [43][51][23][45]
  3. Send it to serial port.

Serial.write with : byte ft817_set_rx_frequency[]={0x43,0x51,0x23,0x45};

works ok.

HOW can i fill the ft817_set_rx_frequency[] ?

Panos, Greece

i have the DEC values in bcd_rx[]

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:

  1. 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?

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

  1. Send it to serial port.

Send what to the serial port?

Thanks Paul, but is it really so important for you to now the nature of any device whith a programming problem?

Where does this number come from?

its just a radio frequency, if is it so important for you go and see: The KA7OEI FT-817 pages - CAT (tm) interface programming using the FT-817 (Using the serial interface)

" 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*)'

thanks again.
Panos, Greece

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);
}

There is no frequency 43A.12F45 MHz,

yes indeed .. thats why at the end is so simple:

int freq1.freq2, freq3...
void converthex() 
{
  freq1=freq/10;
  freq2=freq1*16;
  freq3=freq-freq1*10;
  freq4=freq2+freq3;
}

closed.. many thanks and sri :slight_smile:

73, Panos