Ok, so basically I am parsing a hex array looking for a "D". The exact number varies and the position of the "D" varies too. It could either be 0xD0 or 0x0D (not just those two, the 0 could be any other value and it could be another "D" in which case I take the first one). I need to know whether the "D" is the first or second value. Is there a way of indexing a hex value? I tried using bitRead() but I don't think that is correct. Any help would be much apreciated.
What format is the value being received in ? Is it binary or is it as characters representing the value ?
output:
10
3
const byte msg0 [] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xcd, 0xde };
const byte msg1 [] = { 0x12, 0xd4, 0xcd, 0xde };
char s [80];
int
search (
const byte val,
const byte *buf,
int nByte )
{
for (int n = 0; n < nByte; n++) {
if (val == (buf [n] & 0x0f))
return 2*n;
if (val == ((buf [n] >> 4) & 0x0f))
return 2*n + 1;
}
return -1;
}
void
setup (void)
{
Serial.begin (9600);
Serial.println (search (0xd, msg0, sizeof(msg0)));
Serial.println (search (0xd, msg1, sizeof(msg1)));
}
void
loop (void)
{
}
its being recieved as a hex array
forgive my incompetence, but what is this actually doing?
would it be a simpler method to check the value against all possible hex values including "D" then use that to figure out it its the first or second value? for example, 0D all the way to FD, means its second value, but D0 to DF means its the second value? would be hella slow though
maybe i don't understand what you want
it searches a byte array, checking each 4-bit nibble for a 4-bit (??) value and returns the index of the nibble in the array. an odd index value implies it was the upper nibble in the byte
Below is the array.
37,99,21,92,47,74,87,0D,24,11,72,80,00,00,00,00,00,00,0F
Everything up to the letter D is what I want to extract. the 0 in front of the D needs to be included too, but its part of the same byte.
So I'm trying to find the letter D, then print everything before it.
Post your complete code. Use CODE TAGS!!!
12 34 56 78 9a cd
12 d4
37 99 21 92 47 74 87 0d
const byte msg0 [] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xcd, 0xde };
const byte msg1 [] = { 0x12, 0xd4, 0xcd, 0xde };
const byte msg2 [] = {
0x37, 0x99, 0x21, 0x92, 0x47, 0x74, 0x87, 0x0D,
0x24, 0x11, 0x72, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0F
};
char s [80];
// -----------------------------------------------------------------------------
int
search (
const byte val,
const byte *buf,
int nByte )
{
for (int n = 0; n < nByte; n++) {
if (val == (buf [n] & 0x0f))
return 2*n;
if (val == ((buf [n] >> 4) & 0x0f))
return 2*n + 1;
}
return -1;
}
void
dumpHex (
const byte val,
const byte *buf,
int bufSize )
{
int nByte = 1 + search (0xd, buf, bufSize)/2;
for (int n = 0; n < nByte; n++) {
sprintf (s, " %02x", *buf++);
Serial.print (s);
}
Serial.println ();
}
void
setup (void)
{
Serial.begin (9600);
dumpHex (0xd, msg0, sizeof(msg0));
dumpHex (0xd, msg1, sizeof(msg1));
dumpHex (0xd, msg2, sizeof(msg2));
}
void
loop (void)
{
}
I think you should read each byte left-to-right so the high-order nybble has the lower index:
for (int n = 0; n < nByte; n++) {
if (val == ((buf [n] >> 4) & 0x0f))
return 2*n;
if (val == (buf [n] & 0x0f))
return 2*n + 1;
}
fair enough, but i'm still not sure what the OP really wants
My best guess is that they want to search an array of 'uint4_t' for the first occurrence of a specific 'uint4_t' value.
0xAB & 0x0F == 0x0B
(0xAB >> 4) & 0x0F == 0x0A
Do we still need to perform "& 0x0F" operation when 0s enter automatically at the left during right shifting?
I've solved it. Basically I itterate through every possible hex value that contains a "D".
for (int i = 0; i<responseLength; i++){
if(response[i] == 0x57){
PotentialPANLength = response[i+1];
PotentialPANLocation = i+2;
}
}
for (int n = 0; n<16; n++){
for (int i = 0; i<responseLength; i++){
if (response[i] == n*16 + 13){
ExtraBit = n;
PotentialPANEnd = i;
isthereanextrabit = 1;
}
if (response[i] == 208 + n){
PotentialPANEnd = i;
isthereanextrabit = 0;
}
}
}
for (int i = PotentialPANLocation; i < PotentialPANEnd; i++){
if (response[i] < 10) {
PANasString.concat("0");
PANasString.concat(String(response[i],HEX));
}
else {
PANasString.concat(String(response[i],HEX));
}
}
if (isthereanextrabit = 1){
Serial.println("PAN: " + PANasString + ExtraBit);
}
if (isthereanextrabit == 0){
Serial.println("PAN: " + PANasString);
}
Thanks for your help.