Storing HEX values in the String / Array


i need send value ":xxxxxabcd" via serial
where abcd hex equivalent of dec value ranging 0000 5000.

i find code convert dec value hex , changed little bit send dec equalent hex value.
code: [select]

#define max_hex_length 4

char data_buffer[20];
int hex[4];
char speed_vfd[] = ":01062001";

void setup(){
serial.begin(9600);
 }//end of main()


void loop(){
  int = serial_ask();
  serial.print("the value entered :");
  serial.println(a);
 convert_to_hex(a);
  display_it();
  send_vfd_speed();
}//end of loop

void convert_to_hex(int a){
 for (int i=0; < max_hex_length;i++){

    hex[i] =  % 16;
    = / 16;
  }//end of loop()
}//end of convert_to_hex
void display_it(){
  //serial.print("hex equalent : ");
  (int j=max_hex_length - 1; j >= 0; j--){
    if (hex[j] <= 9){
      serial.print(hex[j]);
    }
    else{
      switch(hex[j]){
      case 10 :
        serial.print('a');
        break;

      case 11 :
        serial.print('b');
        break;

      case 12 :
        serial.print('c');
        break;

      case 13 :
        serial.print('d');
        break;

      case 14 :
        serial.print('e');
        break;

      case 15 :
        serial.print('f');
        break;

      default :
        serial.println("something wrong");
      }//end of switch
    }//end of if else condition
   
  }//end of loop
 serial.println();

}//end of display_it() function


int serial_ask(){
  serial.println("enter integer decimal value coverted hex");

  while(!data_received()){
    //wait until full data received
  }
  return(atoi(data_buffer));

}//end of serial_ask() fucntion

boolean data_received(){
  while(!serial.available()){
    //wait until user enters data
  }
  if(serial.available()){
    static byte index = 0;
    char input = serial.read();
    if(input != '\r'){
      data_buffer[index] =  input;
      index ++;
    }
    else {
      data_buffer[index] = 0;//terminating null make char array string
      index = 0;
      return true;

    }//end of if else condition

  }//end of if condition
  return false;
}//end of data_received() function


void send_vfd_speed(){
serial.print(speed_vfd);
display_it();
serial.println();
}


but how can using strcat() function?
to have store hex value abcd in string?  there efficient way that?

utoa()


Arduino Forum > Using Arduino > Programming Questions > Storing HEX values in the String / Array


arduino

Comments