########################################## NOTE: the starting problem in this first post is solved.
Please scroll down to the problem described in the title.
#########################################
Hi,
I want to write a hex-string to an RFID Chip (MIFARE ultralight) without altering:
Let's say I have a string of a hex-number: "d73f7a79"
I could write the string in pairs: "d7 3f 7a 79"
Now I want to transfer the hex-pairs into a data block for sending to the MIFARE chip:
byte dataBlock[] = {
0xd7, 0x3f, 0x7a, 0x79 }
I want the stored data on the chip to be as followed:
Page 0 1 2 3
... ... ... ... ...
15 d7 3f 7a 79
Can anyone tell me how I could transform this string into the data block?
@gcjr this looks promising! Sorry... I'm not experienced as a programmer, so could you please show me how I can use the string "d73f7a79" (string-variable) as input?
I can't really figure out what the hell is going on with the following^^:
as input? you just statically defined as a byte array. i don't know how you input the data.
the function i wrote, expects a char *s as an argument. but "d73f7a79" is a const char . C++ is finicky about using const char which are stored in flash as char * which are typically in RAM and can be modified. so I cast the const char * as a char * to satisfy the compiler.
why String? you defined is as byte above
consider the expanded code below which will read a c-string through the serial monitor
byte buf [10];
char t [80];
void
disp (
byte *buf,
int nByte )
{
for (int n = 0; n < nByte; n++) {
sprintf (t, " 0x%02x", buf [n]);
Serial.print (t);
}
Serial.println ();
}
// -----------------------------------------------------------------------------
int
func (
char *s,
byte *buf,
int bufSize )
{
int val;
unsigned i;
for (i = 0; i < strlen(s); i += 2) {
sscanf (&s [i], "%02x", &val);
*buf++ = val;
}
return i/2;
}
// -----------------------------------------------------------------------------
void
setup (void)
{
Serial.begin (9600);
disp (buf, func ((char *)"d73f7a79", buf, sizeof(buf)));
disp (buf, func ((char *)"1234567890123", buf, sizeof(buf)));
}
// -----------------------------------------------------------------------------
void
loop (void)
{
char s [80];
if (Serial.available ()) {
int n = Serial.readBytesUntil ('\n', s, sizeof(s));
s [n] = 0;
disp (buf, func (s, buf, sizeof(buf)));
}
}
the last line sends the data to the MIFARE chip.
The code above is just a simple example. In reality there are more strings that are separated in "chunks" of 8 characters (-> "d73f7a79").
Via for-loop "string_chunk_bytes" is generated several times and sent to different address pages of the chip.
Long story short: the output of strin_chunk_bytes:
The one problem is, that I get this string from the serial port and I first have to put it in the right format. I struggle with that because the Arduino platform doesn't give me the tools to "look behind the curtain" and figure it out by my own (as I usually get past issues in Matlab e.g).
Thank you very much, let's get the last problem resolved