hello every one i use this code to convert char array into byte array and actually i am able to do it so but the problem is that i dont know how to access that byte array after it was modified.
void setup() {
Serial.begin(9600);
char arr[] = "abcdef98";
byte out[4];
auto getNum = [](char c){ return c > '9' ? c - 'a' + 10 : c - '0'; };
byte *ptr = out;
for(char *idx = arr ; *idx ; ++idx, ++ptr ){
*ptr = (getNum( *idx++ ) << 4) + getNum( *idx );
}
//Check converted byte values.
for( byte b : out )
Serial.println( b, HEX );
}
void loop() {
}
the output is :
AB
CD
EF
98
but when i try to print 'out' array it is empty and again i can not access 'b' array to do my math on it.
i need the output as an array to send in serial port.
but when i try to print 'out' array it is empty
What do you mean, empty?
Does your real code (the stuff you didn't post) have scope issues?
yes the array is empty. i just printed "out" right after Serial.println( b, HEX ); and it is empty.
taymaz1990:
yes the array is empty. i just printed "out" right after Serial.println( b, HEX ); and it is empty.
There are values in your array: byte out[4]


actually this is my string 000f0111 i want the out array to be like this :
0x00,0x0f,0x01,0x11
so to send it to my rfid card writer.
Those are not printable ASCII codes.
Post the code that you say fails.
void setup() {
Serial.begin(9600);
char arr[] = "00000f01";
byte out[4];
byte arer[4];
auto getNum = [](char c){ return c ; };
byte *ptr = out;
for(char *idx = arr ; *idx ; ++idx, ++ptr ){
*ptr = (getNum( *idx++ ) << 4) + getNum( *idx );
}
int co=0;
//Check converted byte values.
for( byte b : out ){
Serial.println( co );
Serial.println( b,HEX );
arer[co]=b;
co++;
}
// Serial.print(getNum,HEX);
// Serial.print(out[4],HEX);
// Serial.print(arer[4],HEX);
/*
none of this codes commented above worked*/
}
void loop() {
}
this is the code and i have tried every thing i could but i have failed.
Serial.print(out[4],HEX);
A four element array doesn't have an element with the index four - it isn't really surprising it didn't work.
It would've saved all of our time if you'd posted that originally, instead of us having to winkle it out of you.
ok so can you please send me the code that works fine with no problem?
taymaz1990:
ok so can you please send me the code that works fine with no problem?
Try the following code and see what you get:
Serial.println(out[0], HEX);
Are you getting AB?
i get AB the problem is not getting it the problem is that i want the string 00000f01 to be stored in byte array like 0x00,0x00,0x0f,0x01
Who says it isn't being stored in the byte array?
i tried
void setup() {
Serial.begin(9600);
char arr[] = "00000f01";
byte out[4];
byte arer[4];
auto getNum = [](char c){ return c > '9' ? c - 'a' + 10 : c - '0'; };
byte *ptr = out;
for(char *idx = arr ; *idx ; ++idx, ++ptr ){
*ptr = (getNum( *idx++ ) << 4) + getNum( *idx );
}
int co=0;
//Check converted byte values.
for( byte b : out ){
Serial.println( co );
Serial.println( b,HEX );
arer[co]=b;
co++;
}
Serial.println(out[0], HEX);
}
void loop() {
}
but the out put is still this:
0
0
1
0
2
F
3
1
0
TheMemberFormerlyKnownAsAWOL:
Who says it isn't being stored in the byte array?
The output is correct - what's the problem?
TheMemberFormerlyKnownAsAWOL:
The output is correct - what's the problem?
the output should be something like this:
0
0
0
0
0
f
0
1
and this is only the output that is shown i want to get two last nibbles '01' and decrease minus it with one and send it again to rfid. the f here is just a flag to show the last number in nibbles.
You're printing an index and a value - had you forgotten that?
TheMemberFormerlyKnownAsAWOL:
You're printing an index and a value - had you forgotten that?
ictually i am not professional programmer so i dont know what are talking about. can you please share the code that worked for you?
The code that works for me is the code you have.
what i want is a byte array out with this members in it:0x00,0x00,0x0f,0x01
And that's what you have got.