Hex value in array

How do I put a Hex value in array
I have read this line many times. " Hex is understood by human the computer only understand binary so no need to store in hex"

So pls don't give me this answer I need to store the Hex value in array if I directly try to assign it a Hex value
An error saying no literal operator found occurs
Pls tell how would I store hex value in array

anshchawla:
How do I put a Hex value in array
I have read this line many times. " Hex is understood by human the computer only understand binary so no need to store in hex"

So pls don't give me this answer I need to store the Hex value in array if I directly try to assign it a Hex value
An error saying no literal operator found occurs
Pls tell how would I store hex value in array

Are you trying to store the two ASCII characters that are printed as hex characters? Or are you trying to store the BCD 4 bit value, with two of those in each byte?

Paul

See I am new to. Arduino so I didn't understand what you meant
Basically I want to store an IR code recorded using an IR receiver into an array
But the IR code is in hex form
Eg one of the codes is 95D6DEA1
But an error occurs if I try to store this hex value in array

Perhaps if you included the mystery code and the mystery error message, with the line number it gave, someone could help. How long is that code from the IR receiver? 8 bytes long or 4 bytes long?

Paul

https://forum.arduino.cc/index.php?topic=601621.0
Go here you will find my code

anshchawla:
small helppppppp!!!!!!! required - Jobs and Paid Consultancy - Arduino Forum
Go here you will find my code

Do you have two forum postings relating to the same project?

Paul

In the IR class, the value member of decode_results is an unsigned long.

FWIW, sendNEC also expects an unsigned long as the data parameter.

Your array is defined as an array of type byte:

byte gone[50];

Try

unsigned long gone[50];

You've also got a line:

Serial.println(c);

but haven't declared a variable 'c'. With that line commented, the code compiles. Whether or not is works is another matter.

anshchawla:
See I am new to. Arduino so I didn't understand what you meant
Basically I want to store an IR code recorded using an IR receiver into an array
But the IR code is in hex form
Eg one of the codes is 95D6DEA1
But an error occurs if I try to store this hex value in array

Let us review few concepts before going to save your IR data (95D6DEA1) in an array.

1. The basic data size is 8-bit, and it is known as byte.

2. In computer/microcontroller memory, data is always stored in bit form like: 10010101. Let us designate these bits as bit-0 (the right most one as we are face the pattern), b-t1, ..., bit-7 (bo, b1, ..., b7). What is the decimal value of it? This is an area of another business -- we will not go there now.

3. Let us give a symbolic name x to the bit pattern of Step-2. Now, we have: x = 10010101.

4. If I ask you to write the value of x on a piece of paper, you will write 10010101. Correct!

5. In Step-4, you have written 8 symbols (10010101). Is it not a cumbersome job of writing so many symbols/digits? There is also a chance of error as a bit appear may be written as 0; whereas, it should be 1. Is there any better way of writing the value if x, which will reduce this endurance. Yes, there is the following way/method:

(1) Take 4-bit block from the left; now, we have two blocks: 1001 and 0101. Get the value of these blocks in base 2 (binary base); now, we have this value for the 1st block (from left): 1x23 + 0x22 + 0x20 + 1x20 = 8 + 0 + 0 + 1 =9.

(2) Similarly, the 2nd block 0101 will end up with : 5. Now we have: 95 in place of 10010101. Are they different? No! They are not different -- they are the same. The 2nd form (95) is known as hexadecimal (hex) representation of 10010101, and we do it as a matter of convenience. The actual processing is done on the bit pattern (10010101) and not on hex form (95).

6. In your data stream (95D6DEA1), you have 4-byte data. Let us assume that they are binary codes (and they are in binary form and not ASCII codes). Let us store them in an array in the following way:

byte nyData[4] = {0x95, 0xD6, 0xDE, 0xA1}; 
//0x is appended to mean that the digits following 0x is in hex form; 
//so, before processing they must be converted into bit form like: 
//95---> 10010101, D6--->11010110, and etc.

Hope that you have got the points.

So I understood this
I have to save the data in 4 bytes rather than 1
But how will I ask Arduino to combine the 4 bytes when I need my orignal(hex) number back
Also how will I ask Arduino to break it into 4 parts and then store the parts in 4 bytes

anshchawla:
So I understood this
I have to save the data in 4 bytes rather than 1
But how will I ask Arduino to combine the 4 bytes when I need my orignal(hex) number back
Also how will I ask Arduino to break it into 4 parts and then store the parts in 4 bytes

To understand all those you have asked for, you need to do some exercises using UNO, NANO, and theor Serial Monitors. When you are ready, please let us know.

I didn't understand what kind of excercise u were talking about
Did u mean like some kind of homework for me ?

anshchawla:
I didn't understand what kind of excercise u were talking about
Did u mean like some kind of homework for me ?

Yes! Homework via this thread/Forum.

K tell me what research I have to do

There is no need to do any research! Just tell me if you have an Arduino UNO and a PC. I will give you a simple home task about an array.

i have arduino nano as well as a pc

Blackfin:
In the IR class, the value member of decode_results is an unsigned long.

FWIW, sendNEC also expects an unsigned long as the data parameter.

Your array is defined as an array of type byte:

byte gone[50];

Try

unsigned long gone[50];

You've also got a line:

Serial.println(c);

but haven't declared a variable 'c'. With that line commented, the code compiles. Whether or not is works is another matter.

this kind of worked out for me the problem remaining is that i had if i want to store the number in hex form i have to use 0x before the number the problem is that if arduino gets the number from somewhere how do i ask it to put 0x before alloting to to an element of array

While I don’t expect you to understand the reference yet, look for the C
“atol() and/or sscanf() functions
They will do 90% of what you need in two lines of code.
Take your time...

I managed to avoid all this
Store decimal conversion of my hex number into a string but am not able to store the
The value stored in string into the array since both formats are different
array is in unsigned long format
How can I store the value stored in string into the array????

See reply #16

another problem has ocuured which needs to be fixed before using sscanf command
when i try to print the decimal converion of a hexadecimal number the printed number doesnt match with the actual conversion of the hex number what should i do to repair this problem

pls help

i have attached a photo below so that you can see what i am saying

String c;
 
 int t =0;
 int a=0;
 int b=0;
 unsigned long  gone[50];
 #include <IRremote.h> 
 int RECV_PIN = 10;


 int i = 0;
 int m = 0;

 
void setup() {

pinMode(13 ,OUTPUT);
pinMode(12 ,OUTPUT);
pinMode(9 ,OUTPUT);
pinMode(3 ,INPUT);
pinMode(2 ,INPUT);
Serial.begin(9600);

}

void loop() {
    a = digitalRead(2);
    b = digitalRead(3);
      
  if( a == HIGH || ( i != 0 && i != 50 )){
    IRsend irsend;
     Serial.println("transmit");
     Serial.println(i);
     irsend.sendNEC(gone[i], 32);
     Serial.println(gone[i]);
     Serial.println(" ");
     digitalWrite(13,HIGH);    
   delay(200);
    digitalWrite(13,LOW);
 i ++;
  }else{i = 0;
  }
    if( b == HIGH ){
      
      digitalWrite(12,HIGH);
      IRrecv irrecv(RECV_PIN);
      irrecv.enableIRIn();
      decode_results results;
      delay(100);
     if (irrecv.decode(&results)) {
      Serial.println("recieved");

 c = '0x' + results.value  ;  
// gone[m] = c;
    Serial.println(gone[m]);
     Serial.println(c);
    Serial.println(results.value, HEX);
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    irrecv.resume();
    m ++;
  }

  }
  

}