Pages: 1 [2]   Go Down
Author Topic: HEX Split  (Read 1421 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49448
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


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 = smiley-cool
Variable3 = (Third character = 6)

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


Logged

Finland
Offline Offline
Jr. Member
**
Karma: 1
Posts: 84
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 10, 2013, 03:18:58 pm by Chaul » Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18811
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:

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

Note that this method extracts from the right.
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!! smiley

Check this out!!!

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

Pages: 1 [2]   Go Up
Jump to: