Go Down

Topic: HEX Split (Read 7929 times) previous topic - next topic


I need to split or seperate a HEX, for example I have HEX value received from IR Remote and value what i get is 1FE3DC
So I need to split every hex. How I can do that? I would like something like this.

I have this:
hexString = 1FE3DC;

I need asign like this!!:
1_HexNumber = 1    (first number of my hex)
2_HexNumber = F    (second number of my hex)
3_HexNumber = E    (third number of my hex)
4_HexNumber = 3    (fourth number of my hex)
5_HexNumber = D    (fifth number of my hex)
6_HexNumber = C    (sixth number of my hex)

then i can use it like this:
if (5_HexNumber == D)
   digitalWrite(LedPin, HIGH);
   digitalWrite(LedPin, LOW);


hexString[ 0 ] has value '1'
hexString[ 5 ] has value 'C'


Thank you for answer!
But can someone write full code please?


Mar 10, 2013, 12:46 pm Last Edit: Mar 10, 2013, 12:53 pm by UKHeliBob Reason: 1
Do you need to actually split the hex string or just perform actions based on the value of a particular digit ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


Because I want to do something depend from values.

if i have HEX ----> 123ABC

Asign each HEX number to value
String1 = '1'
String2 = '2'
String3 = '3'
String4 = 'A'
String5 = 'B'
String6 = 'C'

then i can use every sended HEX number to do something

if(String1 == '1') -----> turn on a RED led
if(String1 == '2') -----> turn on a GREEN led
if(String1 == '3') -----> turn on a BLUE led

if(String2 == '1')  ----> turn on a motor 
if(String2 == '2')  ----> turn off a motor   
if(String2 == '3')  ----> turn .....


OK.  It seems an odd thing to do but if that is what you want, so be it.

What type of variable is hexString in your example ?  I know what the name is, but that does not mean anything.
How is hexString created ?

Please post the code that you are using to make it easier to help you
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


There is a code, it is IRRemote

This example just turn on and off led if is received by IR right HEX and it is 0x111ABC in my example

But i would like to seperate this HEX to asign every character to variable for to do something with it

1_Value = (firs hex number of my code      --> 1)
2_Value = (second hex number of my code --> 1)
3_Value = (third hex number of my code    --> 1)
4_Value = (fourth hex number of my code  --> A)
5_Value = (fifth hex number of my code    --> B)
6_Value = (sixth hex number of my code   --> C)

example what does do every value:
if(1_Value == '1')  ----> turn ON the LED
if(1_Value == 'A')  ----> turn OFF the LED 
if(6_Value == '1')  ----> turn ON the ...
if(6_Value == 'A')  ----> turn OFF the ...

Code: [Select]

#include <IRremote.h>
int RECV_PIN = 11;
int OUTPUT_PIN = 13;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
  irrecv.enableIRIn(); // Start the receiver
int on = 0;
unsigned long last = millis();

void loop() {
if (irrecv.decode(&results)) {
  Serial.println(results.value, HEX);
    if (results.value == 0x111ABC) {     
        if (millis() - last > 100) {
        on = !on;
        digitalWrite(OUTPUT_PIN, on ? HIGH : LOW);
      last = millis();
    irrecv.resume(); // Receive the next value


Try this:
Code: [Select]

uint8_t read_nibble(uint32_t data, uint8_t ix)
  return ((data >> (ix * 4)) & 0xf);
  for (uint8_t i = 0; i < 8; i++)
    Serial.println(read_nibble(0x12345678, i), HEX);

Please not that the nibbles are numbered from 0..7, from right to left. Changing the order I leave as homework.



I am not a very good programmer.
Can you please show in my example where i need to put your solution?

But i still doesn't understand how this will asign my HEX every char to Variable?
May be you can place all code, please?


then i can use it like this:
if (5_HexNumber == D)
   digitalWrite(LedPin, HIGH);
   digitalWrite(LedPin, LOW);

Now you can write this as:
Code: [Select]

if (read_nibble(results.value, 5) == 0xD)

You will just have to figure out the numbering or change read_nibble() to the number scheme you want for your code. So I think we are done here?


OP, I think you are barking up the wrong tree. Each button on the remote sends a unique code. There is no discernable pattern to the value sent, though. So, you won't be able to predict which button to press to cause a specific LED to light up.

Explaining WHAT you want to do with the remote, rather than HOW you think you should do it, would be useful.
The art of getting good answers lies in asking good questions.


So i don't understand where i need to put this

uint8_t read_nibble(uint32_t data, uint8_t ix)
 return ((data >> (ix * 4)) & 0xf);

may be you can stisk your solution with my code what i post?
I would like all code sticked together not only peace of code. I am simply a newbie.

May be there is some easier way without uint8 ....
I think all that could be done with Array[] function, or may be i wronk?


My major wjat i need is split HEX to peaces

for example i receive  AC5D7A HEX code
i need to asign every leter like unique

So, if i receive for example code AC5D7A
then i want use every peace of it seperately
Value1 = A
Value2 = C
Value3 = 5
Value4 = D
Value5 = 7
Value6 = A

ok, it's doesn't metter or it is HEX or DEC
May be i can use a DEC
if i receive 123976
then asign this values as seperated
Value1 = 1
Value2 = 2
Value3 = 3
Value4 = 9
Value5 = 7
Value6 = 6


You can use sprintf() to convert a numeric value to a string, in whatever format you like, including HEX.

Then, you can pick apart the characters in whatever way you want. Given that you may get AC5D7A when pressing the '1' button and 123976 when pressing the '2' button, how is that useful, though? You'd have to create a table showing the button to press and the resulting hex code, in order for the user to know which button to press to make, for instance, pin 7 go high.

The result on the Arduino side will be a series of 6 to 10 actions that happen when any given button on the remote is pressed. How converting the value to a HEX string and performing each action individually is better than performing the set of actions because the value was AC5D7A is what I am having trouble understanding.
The art of getting good answers lies in asking good questions.


I wnat something like this but on Infra red

If it is send "Hello"

Then it receive Hello but splited
using buf

for example if it will be a remote control car, where every character has something meaning.
First sended char is changed tepend of joistic value 0-255 for speed
If joistic is at midl the value is 127, if full forward 255, if full backward then 0.

Second sended char is changed tepend of joistic value 0-255 for steering
left 0, midl 127, right 255

then receiver need to receive every char seperately for control of speed by first char (forward speed and backwardspeed)
Second char (Steering Left and steering right)
Code: [Select]

    if (vw_get_message(buf, &buflen)) // Non-blocking
int i;

        digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
// Serial.print("Got: ");

for (i = 0; i < buflen; i++)
             c = (buf[i]);

      joystic_y= (buf[0]);
      joystic_x = (buf[1]);
      light_1 = (buf[3]);

I want the same but in IRRemote
Idon't know how to make buf of my received char or dec or hex to asign it to Variable, this is my major problem.

Go Up