arduino string to hex

hello ive just got done searching google and what i found didnt work the way i need it to

i want to be able to pull a string from serial such as "a90" and have it converted to a hex value like 0xa90

ive tryed

//Start Hex String To Int
int x2i(char *s) 
{
  int x = 0;
  for(;;) {
    char c = *s;
    if (c >= '0' && c <= '9') {
      x *= 16;
      x += c - '0'; 
    }
    else if (c >= 'A' && c <= 'F') {
      x *= 16;
      x += (c - 'A') + 10; 
    }
    else break;
    s++;
  }
  return x;
}
//End Hex String To Int

but it returns 0 for all input
ive gotten this code to convert string to decmal but i need it to be hex no decnal i dont want to have to type dec numbers into the serial port

//Start String To Int
int stringToNumber(String thisString) {
  int i, value, length;
  length = thisString.length();
  char blah[(length+1)];
  for(i=0; i<length; i++) {
    blah[i] = thisString.charAt(i);
  }
  blah[i]=0;
  value = atoi(blah);
  return value;
}
//End String TO Int

this is a little frushtrating but can anyone give me some help converting a serial string or any string to HEX thank you in advance for your help

Works fine for me:

int x2i(char *s) 
{
  int x = 0;
  for(;;) {
    char c = *s;
    if (c >= '0' && c <= '9') {
      x *= 16;
      x += c - '0'; 
    }
    else if (c >= 'A' && c <= 'F') {
      x *= 16;
      x += (c - 'A') + 10; 
    }
    else break;
    s++;
  }
  return x;
}

void setup ()
  {
  Serial.begin (115200);
  Serial.println (x2i ("42AB"), HEX);
  }  // end of setup

void loop ()
  {
    
  }  // end of loop

Next time please post the whole code, so we can see how you are testing it.

I got:

42AB

ill post it in a sec but i also just found this is this the correct usage

 int value = strtoul("0x01",NULL,16);
  Serial.println(value,HEX);
//Start Defining Tv Code
#define KEY_POWERON 0x750;
#define KEY_POWEROFF 0xF50;
#define KEY_TUNER 0x250;
#define KEY_VIDEO1 0x030;
#define KEY_VIDEO2 0x830;
#define KEY_VIDEO3 0x430;
#define KEY_VIDEO4 0xE30;
#define KEY_VIDEO5 0x130;
#define KEY_VIDEO6 0x930;
#define KEY_POWER 0xA90;
#define KEY_MUTE 0x290;
#define KEY_SLEEP 0x6D0;
#define KEY_ANTENNA 0x550;
#define KEY_DISPLAY 0x5D0;
#define KEY_INPUT 0xA50;
#define KEY_1 0x010;
#define KEY_2 0x810;
#define KEY_3 0x410;
#define KEY_4 0xC10;
#define KEY_5 0x210;
#define KEY_6 0xA10;
#define KEY_7 0x610;
#define KEY_8 0xE10;
#define KEY_9 0x110;
#define KEY_0 0x910;
#define KEY_JUMP 0xDD0;
#define KEY_ENTER 0xD10;
#define KEY_MENU 0x070;
#define KEY_UP 0x2F0;
#define KEY_DOWN 0xAF0;
#define KEY_LEFT 0x2D0;
#define KEY_RIGHT 0xCD0;
#define KEY_OK 0xA70;
#define KEY_VOLUMEUP 0x490;
#define KEY_VOLUMEDOWN 0xC90;
#define KEY_CHANNELUP 0x090;
#define KEY_CHANNELDOWN 0x890;
#define KEY_FREEZE 0x3B0;
#define KEY_PIP 0xDB0;
#define KEY_PICTUREMODE 0x270;
//End Defining Tv Code


#include <IRremote.h>
IRsend irsend;

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  // reserve 2000 bytes for the inputString:
  inputString.reserve(2000);
}

//Begin Loop
void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    SendCode(); //start send code void
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}
//End Loop



/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }  
  }

}

// Start Send Tv Code
void SendCode(){
  int value = x2i(inputString);
   
  Serial.println(value,HEX);
  //int value = x2i("inputString");
  // Debug("inputString",inputString);
  Serial.println(value);

  for (int i = 0; i < 2; i++) {
    irsend.sendSony(value, 12); // Sony TV code
    delay(100);
  }
}
//End Tv Send Code

//Start Hex String To Int
int x2i(char *s) 
{
  int x = 0;
  for(;;) {
    char c = *s;
    if (c >= '0' && c <= '9') {
      x *= 16;
      x += c - '0'; 
    }
    else if (c >= 'A' && c <= 'F') {
      x *= 16;
      x += (c - 'A') + 10; 
    }
    else break;
    s++;
  }
  return x;
}
//End Hex String To Int


//start debug 
void Debug(String Title, String Value){
  Serial.println();
  Serial.println("---start debug---");
  Serial.println(Title);
  Serial.println(Value);
  Serial.println("---end Debug---");

  //end debug
}
 String inputString = "";         // a string to hold incoming data
...
 int value = x2i(inputString);
...
int x2i(char *s)

String is not a char *.

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

ok i tryed modifing it but now i get more errors?

new code is

//Start Defining Tv Code
#define KEY_POWERON 0x750;
#define KEY_POWEROFF 0xF50;
#define KEY_TUNER 0x250;
#define KEY_VIDEO1 0x030;
#define KEY_VIDEO2 0x830;
#define KEY_VIDEO3 0x430;
#define KEY_VIDEO4 0xE30;
#define KEY_VIDEO5 0x130;
#define KEY_VIDEO6 0x930;
#define KEY_POWER 0xA90;
#define KEY_MUTE 0x290;
#define KEY_SLEEP 0x6D0;
#define KEY_ANTENNA 0x550;
#define KEY_DISPLAY 0x5D0;
#define KEY_INPUT 0xA50;
#define KEY_1 0x010;
#define KEY_2 0x810;
#define KEY_3 0x410;
#define KEY_4 0xC10;
#define KEY_5 0x210;
#define KEY_6 0xA10;
#define KEY_7 0x610;
#define KEY_8 0xE10;
#define KEY_9 0x110;
#define KEY_0 0x910;
#define KEY_JUMP 0xDD0;
#define KEY_ENTER 0xD10;
#define KEY_MENU 0x070;
#define KEY_UP 0x2F0;
#define KEY_DOWN 0xAF0;
#define KEY_LEFT 0x2D0;
#define KEY_RIGHT 0xCD0;
#define KEY_OK 0xA70;
#define KEY_VOLUMEUP 0x490;
#define KEY_VOLUMEDOWN 0xC90;
#define KEY_CHANNELUP 0x090;
#define KEY_CHANNELDOWN 0x890;
#define KEY_FREEZE 0x3B0;
#define KEY_PIP 0xDB0;
#define KEY_PICTUREMODE 0x270;
//End Defining Tv Code


#include <IRremote.h>
IRsend irsend;

char inputString[80];         // a char to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  // reserve 2000 bytes for the inputString:

}

//Begin Loop
void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    SendCode(); //start send code void
    // clear the string:
    inputString[0] = '\0';
    
    stringComplete = false;
  }
}
//End Loop



/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    strcat (inputString,inChar);
   
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }  
  }

}

// Start Send Tv Code
void SendCode(){
  int value = x2i(inputString);
   
  Debug("inputString",inputString);
  Serial.println(value);

  for (int i = 0; i < 2; i++) {
    irsend.sendSony(value, 12); // Sony TV code
    delay(100);
  }
}
//End Tv Send Code

//Start Hex String To Int
int x2i(char *s) 
{
  int x = 0;
  for(;;) {
    char c = *s;
    if (c >= '0' && c <= '9') {
      x *= 16;
      x += c - '0'; 
    }
    else if (c >= 'A' && c <= 'F') {
      x *= 16;
      x += (c - 'A') + 10; 
    }
    else break;
    s++;
  }
  return x;
}
//End Hex String To Int


//start debug 
void Debug(String Title, String Value){
  Serial.println();
  Serial.println("---start debug---");
  Serial.println(Title);
  Serial.println(Value);
  Serial.println("---end Debug---");

  //end debug
}

new errors are
BareMinimum.cpp: In function 'void serialEvent()':
BareMinimum:85: error: invalid conversion from 'char' to 'const char*'
BareMinimum:85: error: initializing argument 2 of 'char* strcat(char*, const char*)'

You are going to have to do a tiny bit of research about how to use strings in C.

Also this might help:

thank you for pointing me into the right direction i got it all working

//Start Defining Tv Code
#define KEY_POWERON 0x750;
#define KEY_POWEROFF 0xF50;
#define KEY_TUNER 0x250;
#define KEY_VIDEO1 0x030;
#define KEY_VIDEO2 0x830;
#define KEY_VIDEO3 0x430;
#define KEY_VIDEO4 0xE30;
#define KEY_VIDEO5 0x130;
#define KEY_VIDEO6 0x930;
#define KEY_POWER 0xA90;
#define KEY_MUTE 0x290;
#define KEY_SLEEP 0x6D0;
#define KEY_ANTENNA 0x550;
#define KEY_DISPLAY 0x5D0;
#define KEY_INPUT 0xA50;
#define KEY_1 0x010;
#define KEY_2 0x810;
#define KEY_3 0x410;
#define KEY_4 0xC10;
#define KEY_5 0x210;
#define KEY_6 0xA10;
#define KEY_7 0x610;
#define KEY_8 0xE10;
#define KEY_9 0x110;
#define KEY_0 0x910;
#define KEY_JUMP 0xDD0;
#define KEY_ENTER 0xD10;
#define KEY_MENU 0x070;
#define KEY_UP 0x2F0;
#define KEY_DOWN 0xAF0;
#define KEY_LEFT 0x2D0;
#define KEY_RIGHT 0xCD0;
#define KEY_OK 0xA70;
#define KEY_VOLUMEUP 0x490; //varafied
#define KEY_VOLUMEDOWN 0xC90;
#define KEY_CHANNELUP 0x090;
#define KEY_CHANNELDOWN 0x890;
#define KEY_FREEZE 0x3B0;
#define KEY_PIP 0xDB0;
#define KEY_PICTUREMODE 0x270;
//End Defining Tv Code

#include <IRremote.h>
IRsend irsend;


// how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;

void setup ()
{
  Serial.begin(9600);   // Start Serial Port

  pinMode(4,OUTPUT);   // 
  digitalWrite(4,HIGH);// Turn Lights on through Relay
} // end of setup

// here to process incoming serial data after a terminator received
void process_data (char * data)
{
  // Start Send Tv Code
   int value = x2i(data);
    
    Serial.println(value);

  for (int i = 0; i < 2; i++) {
    irsend.sendSony(value, 12); // Sony TV code
    delay(100);
  }
  //End Tv Send Code
  
}  // end of process_data


void loop()
{
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;

  if (Serial.available () > 0) 
  {
    char inByte = Serial.read ();

    switch (inByte)
    {

    case '\n':   // end of text
      input_line [input_pos] = 0;  // terminating null byte

      // terminator reached! process input_line here ...
      process_data (input_line);

      // reset buffer for next time
      input_pos = 0;  
      break;

    case '\r':   // discard carriage return
      break;

    default:
      // keep adding if not full ... allow for terminating null byte
      if (input_pos < (MAX_INPUT - 1))
        input_line [input_pos++] = inByte;
      break;

    }  // end of switch

  }  // end of incoming data

  // do other stuff here like testing digital input (button presses) ...

}  // end of loop

//Start Hex String To Int
int x2i(char *s) 
{
  int x = 0;
  for(;;) {
    char c = *s;
    if (c >= '0' && c <= '9') {
      x *= 16;
      x += c - '0'; 
    }
    else if (c >= 'A' && c <= 'F') {
      x *= 16;
      x += (c - 'A') + 10; 
    }
    else break;
    s++;
  }
  return x;
}
//End Hex String To Int

//start debug 
void Debug(String Title, String Value){
  Serial.println();
  Serial.println("---start debug---");
  Serial.println(Title);
  Serial.println(Value);
  Serial.println("---end Debug---");

  //end debug
}

THANKS TO THIS I FINALLY HAVE CABLE ON MY TV YAY :slight_smile: