Write integer to buffer

I am modifying some code that writes data in the form of byte buffer[34]; to RFID tags (using MIFARE MFRC522), and I need an integer value to be the contents of that buffer.

I have tried typecasting, but can't seem to find a way to convert my integer to the aforementioned type.

Any ideas? Thanks! :slight_smile:

An int type is two bytes on an eight bit Arduino.
What compression scheme do you have to store 34 bytes into two bytes?

My guess and it is only a guess because you haven't posted your code, is that each one of those bytes only holds one bit of information.
If so a loop and shift the LSB of the byte into a long int would do it.

Well, to be more specific, my integer will never be any greater than 180 (or, at the very most, 1023). The original code reads strings of characters (up to a length of 30 at a time) from Serial Monitor, I need to take an already existing integer instead.

Basically, I need a value like 180 converted to "180" so that I can say:

buffer = int_as_arrayOfChars;

I am trying to store a small value in a large space, not vice versa, since "180" is much smaller than "OneHundredEighty" :stuck_out_tongue:

Hope this is more clear. Sorry if what I was implying earlier was nonsensical, haha.

P.S. I will also need the "180" to be converted back to 180 at another point in the code when the RFID tag is read and the "180" needs to be taken as an integer by a servo.

Grumpy_Mike:
My guess and it is only a guess because you haven't posted your code, is that each one of those bytes only holds one bit of information.
If so a loop and shift the LSB of the byte into a long int would do it.

....apart from the two bits left over.

XORduino:
Well, to be more specific, my integer will never be any greater than 180 (or, at the very most, 1023). The original code reads strings of characters (up to a length of 30 at a time) from Serial Monitor, I need to take an already existing integer instead.

Basically, I need a value like 180 converted to "180" so that I can say:

buffer = int_as_arrayOfChars;

I am trying to store a small value in a large space, not vice versa, since "180" is much smaller than "OneHundredEighty" :stuck_out_tongue:

Hope this is more clear. Sorry if what I was implying earlier was nonsensical, haha.

P.S. I will also need the "180" to be converted back to 180 at another point in the code when the RFID tag is read and the "180" needs to be taken as an integer by a servo.

You mean like atoi or strtol?

Hope this is more clear.

Nope.

What is being stored in the byte array?

What form do you want it in?

AWOL:
....apart from the two bits left over.

Doh!

Actually, atoi() would be the precise solution to the second "issue", the conversion from string to integer. I have used atoi before, I just forgot it existed lol.

Something that does the reverse of that, like "itoa", would solve my problem I think.

EDIT:
@Mike
The byte array (byte buffer[34]) normally accepts up to 30 characters at a time via:

Serial.readBytesUntil('#', (char *) buffer, 30);

From what I have gathered from the error messages I have received when using

buffer = analog_servo1;

I need analog_servo1 to be converted to the type byte[34] (unsigned char [34]), since buffer is of the type byte[34]. From this, I have assumed that byte[34] and unsigned char [34] are synonymous and that I will be safe passing a string to the buffer.

EDIT2:
I have now tried using itoa, and this seems to get me closer to what I need, but I get the error:

invalid conversion from 'byte* {aka unsigned char*}' to 'char*' [-fpermissive]

The byte array (byte buffer[34]) normally accepts up to 30 characters at a time via:

Like pulling teeth this is.

What are these characters?
ASCII?
HEX?
Binary?
BCD?
Bits?

Grumpy_Mike:
Like pulling teeth this is.

I guess I don't understand exactly what you're asking then lol. I'm not trying to resist, trust me :stuck_out_tongue:

They're ASCII characters stored on the RFID tag in HEX form.

This is the original code (from github) that I am modifying:

#include <MFRC522.h>

#include <Mouse.h>

/*
 * Write personal data of a MIFARE RFID card using a RFID-RC522 reader
 * Uses MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT. 
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno           Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
 *
 * Hardware required:
 * Arduino
 * PCD (Proximity Coupling Device): NXP MFRC522 Contactless Reader IC
 * PICC (Proximity Integrated Circuit Card): A card or tag using the ISO 14443A interface, eg Mifare or NTAG203.
 * The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com. 
 */

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

void setup() {
        Serial.begin(9600);        // Initialize serial communications with the PC
        SPI.begin();               // Init SPI bus
        mfrc522.PCD_Init();        // Init MFRC522 card
        Serial.println(F("Write personal data on a MIFARE PICC "));
}

void loop() {
        
        // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
        MFRC522::MIFARE_Key key;
        for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
        
        // Look for new cards
        if ( ! mfrc522.PICC_IsNewCardPresent()) {
                return;
        }

        // Select one of the cards
        if ( ! mfrc522.PICC_ReadCardSerial())    return;
        
        Serial.print(F("Card UID:"));    //Dump UID
        for (byte i = 0; i < mfrc522.uid.size; i++) {
          Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
          Serial.print(mfrc522.uid.uidByte[i], HEX);
        } 
        Serial.print(F(" PICC type: "));   // Dump PICC type
         MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
        Serial.println(mfrc522.PICC_GetTypeName(piccType));
         
        byte buffer[34];  
        byte block;
        MFRC522::StatusCode status;
        byte len;
        
        Serial.setTimeout(20000L) ;     // wait until 20 seconds for input from serial
        // Ask personal data: Family name
        Serial.println(F("Type Family name, ending with #"));
        len=Serial.readBytesUntil('#', (char *) buffer, 30) ; // read family name from serial
        for (byte i = len; i < 30; i++) buffer[i] = ' ';     // pad with spaces
        
        block = 1;
        //Serial.println(F("Authenticating using key A..."));
        status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
        if (status != MFRC522::STATUS_OK) {
           Serial.print(F("PCD_Authenticate() failed: "));
           Serial.println(mfrc522.GetStatusCodeName(status));
           return;
        }
        else Serial.println(F("PCD_Authenticate() success: "));
        
        // Write block
  status = mfrc522.MIFARE_Write(block, buffer, 16);
  if (status != MFRC522::STATUS_OK) {
      Serial.print(F("MIFARE_Write() failed: "));
      Serial.println(mfrc522.GetStatusCodeName(status));
            return;
  }
        else Serial.println(F("MIFARE_Write() success: "));

        block = 2;
        //Serial.println(F("Authenticating using key A..."));
        status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
        if (status != MFRC522::STATUS_OK) {
           Serial.print(F("PCD_Authenticate() failed: "));
           Serial.println(mfrc522.GetStatusCodeName(status));
           return;
        }
        
        // Write block
  status = mfrc522.MIFARE_Write(block, &buffer[16], 16);
  if (status != MFRC522::STATUS_OK) {
      Serial.print(F("MIFARE_Write() failed: "));
      Serial.println(mfrc522.GetStatusCodeName(status));
            return;
  }
        else Serial.println(F("MIFARE_Write() success: "));

        // Ask personal data: First name
        Serial.println(F("Type First name, ending with #"));
        len=Serial.readBytesUntil('#', (char *) buffer, 20) ; // read first name from serial
        for (byte i = len; i < 20; i++) buffer[i] = ' ';     // pad with spaces
        
        block = 4;
        //Serial.println(F("Authenticating using key A..."));
        status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
        if (status != MFRC522::STATUS_OK) {
           Serial.print(F("PCD_Authenticate() failed: "));
           Serial.println(mfrc522.GetStatusCodeName(status));
           return;
        }
        
        // Write block
  status = mfrc522.MIFARE_Write(block, buffer, 16);
  if (status != MFRC522::STATUS_OK) {
      Serial.print(F("MIFARE_Write() failed: "));
      Serial.println(mfrc522.GetStatusCodeName(status));
            return;
  }
        else Serial.println(F("MIFARE_Write() success: "));

        block = 5;
        //Serial.println(F("Authenticating using key A..."));
        status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
        if (status != MFRC522::STATUS_OK) {
           Serial.print(F("PCD_Authenticate() failed: "));
           Serial.println(mfrc522.GetStatusCodeName(status));
           return;
        }
        
        // Write block
  status = mfrc522.MIFARE_Write(block, &buffer[16], 16);
  if (status != MFRC522::STATUS_OK) {
      Serial.print(F("MIFARE_Write() failed: "));
      Serial.println(mfrc522.GetStatusCodeName(status));
            return;
  }
        else Serial.println(F("MIFARE_Write() success: "));


        Serial.println(" ");
        mfrc522.PICC_HaltA(); // Halt PICC
        mfrc522.PCD_StopCrypto1();  // Stop encryption on PCD
       
}

This is what the contents of the card looks like (when read by DumpInfo.ino on github):

Sector Block   0  1  2  3   4  5  6  7   8  9 10 11  12 13 14 15  AccessBits
  15     63   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         62   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         61   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         60   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
  14     59   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         58   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         57   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         56   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
   .
   . (cut out to fit within 9000 character maximum)
   .
   1      7   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
          6   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          5   20 20 20 20  20 20 20 20  20 20 20 20  20 20 99 01  [ 0 0 0 ] 
          4   0D 0A 20 20  20 20 20 20  20 20 20 20  20 20 20 20  [ 0 0 0 ] 
   0      3   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
          2   00 0D 0D E2  0D E2 00 0E  52 03 0D E2  00 0E 00 00  [ 0 0 0 ] 
          1   08 F7 08 E3  08 00 01 84  08 FE BF 05  B4 00 0D E2  [ 0 0 0 ] 
          0   40 A8 E7 D5  DA 08 04 00  62 63 64 65  66 67 68 69  [ 0 0 0 ]

Instead of getting a person's name via Serial Monitor, I want it to take a value that I have stored in a variable.

Solved!

Ralph Bacon said to just cast the integer to a string, and then copy the string to the buffer (one character at a time since both are arrays).

Tried it and it works :stuck_out_tongue:

use a union construct,

union dual
{
char charBuff[34];
int intBuff[17];
}

dual myData;

Using this, you can write 34 characters to charBuff[] as characters, and then when want to ship them out to the tag, just write the 17 integers out from intBuff. The greatest typecasting technique ever invented.

refer to them in the program as myData.charBuff, and myData.intBuff.

I'm not sure whether or not this would affect your method, but the buffer I'm dealing with is part of the already existing code (not mine and as a result I don't want to tamper with it), and it is type "byte". Until now, I had never seen a variable declared as type "byte" and thus I don't know the similarities and differences between that and a char. Thanks though! :slight_smile:

I don't know the similarities and differences between that and a char.

A "byte" is unsigned eight bit, a "char" is signed eight bit.
That's all; usually you can get away with a simple fib cast.

AWOL:
A "byte" is unsigned eight bit, a "char" is signed eight bit.
That's all; usually you can get away with a simple fib cast.

Short and sweet. Thanks, AWOL!