HEX Split

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

Do you need to actually split the hex string or just perform actions based on the value of a particular digit ?

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

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

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


void setup()
{
  Serial.begin(9600);
  pinMode(OUTPUT_PIN, OUTPUT);
  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:

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.

Cheers!

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?

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

Now you can write this as:

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?
Cheers!

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.

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.

I wnat something like this but on Infra red
http://www.pjrc.com/teensy/td_libs_VirtualWire.html

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)

    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.

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.

Then, perhaps you should go back to where I suggested that you use sprintf() and start googling.

Have you written any code to do the data transmission using IR ?

PaulS - Seems the sprintf() function is that what i want, but i can't find no one good example! :frowning:

UKHeliBob - Yes at this moment i just experimenting, and i have transmitting code. Where is conected 4 buttons and i can send 4 diferent codes with it.

But my general what i need is to seperate receiver incoming data.

For example if in transmitter i change first char, second char and third char (example - 486) then transmit it to receiver

then on receiver i need to seperate it and asign to variable.

received code = 486

asigning to variable

Variable1 = (First character = 4)
Variable2 = (Second character = 8)
Variable3 = (Third character = 6)

Looks very simple but no one can help me, yet!

I don't quite get this. I would assume that you are already sending the data as 8-bit characters. That's how your library most likely presents it anyways. I don't get where the number to string conversion comes in. Would be curious to see the code that you have for receiving and sending data.

If you now want to interpret the transmitted data from the hexadecimal representation into characters, then each "character" in that format represent 2 four-bit symbols, since it takes 4 bits to represent 16 values and hexadecimal happens to have 16 symbols. That's the easiest way of interpreting the hexadecimal representation to bits and back. You can take the high and low 4-bits to interpret what you have without the need for string to number and back to string conversions.

Then again, you could just as well be sending ASCII characters and interpret it as such unless you insist on saving that one half of a byte. So, ASCII character 1 could mark led 1, and so on, just to make it easy.

Edit: Oh, and you do realize that there is a difference between 'A' and 0xA? The first one is a character having a number value 65 in the ASCII character table and the latter is the hexadecimal (16-base) representation of the decimal (10-base) number 10.

I don't understand why you are combining the codes in the first place if you have written the transmitting code. If you want to send, receive and act on several type of command such as steering, speed, lights on/ff then why not send simple individual commands to do it rather than combining several together which is more work at both ends.

By all means send a stream of commands one after the other but use a separator of some kind to make them easy to identify at the receiving end.

You don't even need to sprintf it. You are looking at extracting out the number, 4 bits at a time, right?

void setup ()
  {
  Serial.begin (115200);
  unsigned long hexString = 0x1FE3DC;
  
  for (byte i = 0; i < 8; i++)
    {
    byte nibble = hexString & 0xF;
    Serial.println (nibble, HEX);
    hexString >>= 4;
    
    if (nibble == 0xD)
       Serial.println ("Got a D!");
    }
  }  // end of setup

void loop () { }

Output:

C
D
Got a D!
3
E
F
1
0
0

Note that this method extracts from the right.

Thank you for code! That is almost what i want
Greate is that it works. But it is not exactly what I want. Nibble just search an character in hex string, but i need to seperate every char as unique data carrier.

I will try to explain for what i want it. I using Infra red to send data using IrRemote library. I am trying to use it for Laser Tag
For example sender is gun with IR, when gun shoot it send IR hex or dec string to another players receiver.
That code what is sended, need contains data about in which team is that player, what is player number, what gun mode it use, ....
for example 35AACF HEX code is sended

so in hex we know that
hex 0x1 =1
hex 0x2 =2
...
hex 0xE=14
hex 0xF=15

(First Char) ---> 3 (it will means in which team player is (0x3)third team)
(Second Char) ---> 5 (it will means which player by number it is (0x5)fifth player)
(Third Char) ---> A (it will means what gun it use - pistol which subtract (0x5) 5% of lives, or blaster who subtract(0xA) 10%)
(Fourth Char) ---> A (it will means - something else)
.....

so on receiver i need to seperate the code for use it every char as unique
example
FirstChar = (First char of sended code 35AACF)
SecondChar = (Second char of sended code 35AACF)
ThirdChar = (Third char of sended code 35AACF)
....

if (SecondChar == 0x5)
then
add 1 score to player with number 5 and Show on display which player was kill me;

if (ThirdChar == 0xA)
then
then it is blaster and subtract 10% lives from players lives;