user40980: I'm a beginner to C++ and I'm clearly missing some fundamental knowledge of data types. As such, I'm struggling to adapt the examples in the miguelbalboa/rfid library to my use case. I'm simply trying to write an integer to a 13.56Mhz RFID tag. Ideally I'd like to optimize the data so as to be quick to read (it seems fully reading these cards can take a while and I'd like to read the integer, an ID, as quickly as possible).
So on to my actual questions... what is the data type shown in this block of example code (shown below) and how can I turn an integer into the same data type? Finally, how should I read this saved value back into the familiar integer data type?
byte dataBlock[]    = {
    0x01, 0x02, 0x03, 0x04, //  1,  2,   3,  4,
    0x05, 0x06, 0x07, 0x08, //  5,  6,   7,  8,
    0x09, 0x0a, 0xff, 0x0b, //  9, 10, 255, 11,
    0x0c, 0x0d, 0x0e, 0x0f  // 12, 13, 14, 15
};


user27653: The dataBlock[] variable is an array of bytes. One byte can represent an integer ranging from -128 to 127, or a positive integer ranging from 0 to 255. If your integer value will always be in one of those ranges, then you can just put your integer in the first element of the array and zero out the remaining.
byte dataBlock[]    = {
    0x01, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00
};

(That saves a value of 1 to the first element, and zeros to the rest. If you want to change the first element:
dataBlock[0] = yourValue;

If your integer is a 2-byte integer, as is typically found in Arduino with type int, you need to split your value into the high byte and low byte:
dataBlock[0] = (byte)(yourValue & 0xFF); // Low byte
dataBlock[1] = (byte)(yourValue >> 8);   // High byte

Then you write the data block as per the example. To get your 2-byte integer value back:
yourValue = ((int)dataBlock[1])<<8 | dataBlock[0];  // Shift high byte, OR with low byte.


40980: <p>I'm a beginner to C++ and I'm clearly missing some fundamental knowledge of data types. As such, I'm struggling to adapt the examples in the <a href="https://github.com/miguelbalboa/rfid" rel="nofollow noreferrer">miguelbalboa/rfid</a> library to my use case. I'm simply trying to write an integer to a 13.56Mhz RFID tag. Ideally I'd like to optimize the data so as to be quick to read (it seems fully reading these cards can take a while and I'd like to read the integer, an ID, as quickly as possible).</p>

<p>So on to my actual questions... what is the data type shown in <a href="https://github.com/miguelbalboa/rfid/blob/master/examples/ReadAndWrite/ReadAndWrite.ino#L91-L100" rel="nofollow noreferrer">this block of example code</a> (shown below) and how can I turn an integer into the same data type? Finally, how should I read this saved value back into the familiar integer data type?</p>

<pre><code>byte dataBlock[]    = {
    0x01, 0x02, 0x03, 0x04, //  1,  2,   3,  4,
    0x05, 0x06, 0x07, 0x08, //  5,  6,   7,  8,
    0x09, 0x0a, 0xff, 0x0b, //  9, 10, 255, 11,
    0x0c, 0x0d, 0x0e, 0x0f  // 12, 13, 14, 15
};
</code></pre>
 27653: <p>The <code>dataBlock[]</code> variable is an array of bytes. One byte can represent an integer ranging from -128 to 127, or a positive integer ranging from 0 to 255. If your integer value will always be in one of those ranges, then you can just put your integer in the first element of the array and zero out the remaining.</p>

<pre><code>byte dataBlock[]    = {
    0x01, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00
};
</code></pre>

<p>(That saves a value of 1 to the first element, and zeros to the rest. If you want to change the first element:</p>

<pre><code>dataBlock[0] = yourValue;
</code></pre>

<p>If your integer is a 2-byte integer, as is typically found in Arduino with type <code>int</code>, you need to split your value into the high byte and low byte:</p>

<pre><code>dataBlock[0] = (byte)(yourValue &amp; 0xFF); // Low byte
dataBlock[1] = (byte)(yourValue &gt;&gt; 8);   // High byte
</code></pre>

<p>Then you write the data block as per the example. To get your 2-byte integer value back:</p>

<pre><code>yourValue = ((int)dataBlock[1])&lt;&lt;8 | dataBlock[0];  // Shift high byte, OR with low byte.
</code></pre>
