Converting to integer to hex to uint8_t

So here is the deal, I am trying to make an IR repeater, and the older version of this library (IRremote) uses results.value to get the IR value. But in order to send an IR signal, you need to use HEX. But it also needs to be in uint8_t variable.

Please help.

Here is my code. (It is just a hack job right now):

#include <IRremote.h>

int code;

IRsend irsend;

const int RECV_PIN = 12;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    switch (results.decode_type) {
      case NEC: Serial.println("NEC");
        code = NEC; break;
      case SONY: Serial.println("SONY");
        code = SONY; break;
      case RC5: Serial.println("RC5");
        code = RC5; break;
      case RC6: Serial.println("RC6");
        code = RC6; break;
      case SHARP: Serial.println("SHARP");
        code = SHARP; break;
      case JVC: Serial.println("JVC");
        code = JVC; break;
      case SAMSUNG: Serial.println("SAMSUNG");
        code = SAMSUNG; break;
      case LG: Serial.println("LG");
        code = LG; break;
      case WHYNTER: Serial.println("WHYNTER");
        code = WHYNTER; break;
      case PANASONIC: Serial.println("PANASONIC");
        code = PANASONIC; break;
      case DENON: Serial.println("DENON");
        code = DENON; break;
      default:
      case UNKNOWN: Serial.println("UNKNOWN");
        code = UNKNOWN; break;
    }
    Serial.println(code);
    irrecv.resume();

    String sendData = "0x" + String(results.value, HEX);
    
    sendCode(sendData);
  }
}


void sendCode(uint8_t value){
  
  switch (code){
    case NEC:
    irsend.sendNEC(value, 32);

    case SONY:
    irsend.sendSony(value, 32);
    break;

    case RC5:
    irsend.sendRC5(value, 32);
    break;

    case RC6:
    irsend.sendRC6(value, 32);
    break;

    case SHARP:
    irsend.sendSharp(value, 32);
    break;

    case JVC:
    irsend.sendJVC(value, 32);
    break;

    case SAMSUNG:
    irsend.sendSamsung(value, 32);
    break;

    case WHYNTER:
    irsend.sendWhynter(value, 32);
    break;

    case PANASONIC:
    irsend.sendPanasonic(value, 32);
    break;

    case DENON:
    irsend.sendDenon(value, 32);
    break;

    case LG:
    irsend.sendLG(value, 32);
    break;
    
    case UNKNOWN:
    irsend.send(value, 32);
    break;
  }
}

The both statements are wrong.

There is no such data type "hex". A decimal, hexadecimal, octal or binary are just a representation for humans. For the controller, the representation of the number doesn't matter, all of this : 48 decimal, 0x30 hex and 0b00110000 binary are perfectly the same.

And next - to be sended by the library, the code must be a array of uint8_t, not a single byte.

So your code is a mess of errors and incorrect operations. You shouldn't convert the code to the "hex" with strings and your sendCode() function should accept a pointer to byte array as parameters rather than single byte;

1 Like

Have you checked

1 Like

I have moved this to Programming Questions....Any problems please notify me, and I'll move it back.

Thanks.

P.S. Programming Questions was a better fit for the question. :slightly_smiling_face:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.