Go Down

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


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.
The art of getting good answers lies in asking good questions.


Have you written any code to do the data transmission using IR ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


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

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!


Mar 10, 2013, 08:02 pm Last Edit: Mar 10, 2013, 09:18 pm by Chaul Reason: 1
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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


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

Code: [Select]

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 () { }


Code: [Select]

Got a D!

Note that this method extracts from the right.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics


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
FirstChar = (First char of sended code 35AACF)
SecondChar = (Second char of sended code 35AACF)
ThirdChar = (Third char of sended code 35AACF)

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

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


BIG THANKS to Nick Gammon!
Using Code which post Nick Gammon + I figure out how to use BUF and i get result by myself about what I all time asking from start of this post!!! :)

Check this out!!!

Code: [Select]
void setup ()
  Serial.begin (115200);
  unsigned long hexString = 0x1FD3DC;
  unsigned int buf[8];
  int Variable0;
  int Variable1;
  int Variable2;
  int Variable3;
  int Variable4;
  int Variable5;
  for (byte i = 0; i < 8; i++)
       byte nibble = hexString & 0xF;
       (buf[i]) = nibble;   
       hexString >>= 4;
    Variable0 = (buf[5]);
    Variable1 = (buf[4]);
    Variable2 = (buf[3]);
    Variable3 = (buf[2]);
    Variable4 = (buf[1]);
    Variable5 = (buf[0]);
    Serial.print("V0= ");
    Serial.println (Variable0,HEX);
    Serial.print("V1= ");
    Serial.println (Variable1,HEX);
    Serial.print("V2= ");
    Serial.println (Variable2,HEX);
    Serial.print("V3= ");
    Serial.println (Variable3,HEX);
    Serial.print("V4= ");
    Serial.println (Variable4,HEX);
    Serial.print("V5= ");
    Serial.println (Variable5,HEX);

void loop () { }

Go Up