# Comparing a string with an array

Status
Not open for further replies.

#### dr pepper

##### Well-Known Member
In C I want to compare a string with an array.
I have a 4 element int array which is in hex, and holds a keycode.
I have a 8 element string which holds the ascii equivalent of the keycode (2 bytes of ascii to represent one byte of hex).
What would be a reasonable way to check to see if the string matches the array?
I'mm battling with atoi and itoa as my value can go as high as FF FF FF FF which these functions see as a negative number.

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
So the hex is packed BCD?? ie.. hex = 0x35 and the string equivalent is 0x30 and 0x35... This appears to be standard Modbus ASCII.... You'll need to convert one to match the other..

If you need to pull 0xFF then you need to cast it to unsigned or the compiler will see it as a negative...

Also You can't use itoa with a long... 0xffffff is a double( long ) so use ltoa instead..

#### dr pepper

##### Well-Known Member
Unless I have got it wrong a unsigned long wont work as ltoa and atol only work with signed values, an 8 digit hex value is 32 bits wide so if the msd is >7 its interpreted as negative.
I could do with lltoa and atoll.
The issue with joining 2 values up is that atol and ltoa have leading zero blanking so 0x02 becomes ascii 2, so a code 0x1a, 0x02 becomes when joined 1a2, not what it should be 1a02.

Last edited:

#### Pommie

##### Well-Known Member
You could convert the old school way,
Code:
 (untested)
unsigned long num;
unsigned char temp;
for(i=0;i<8;i++){
num<<=4;
temp=array[i]-0x30;
if(temp>9)
temp-=7;
num+=temp;
}
Mike.

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Unless I have got it wrong a unsigned long wont work as ltoa and atol only work with signed values,
There is also an ultoa function... Some of these have come from C18... BUT!! It's relatively easy to convert!

The two routines ultoa and atoul are both asm routines.. and for the pic18.... If you need them, I'll post them

#### dr pepper

##### Well-Known Member
I sussed it,
The hex array readCard is encoded into ascii by first encoding it into a long by shifting, then I use ultoa as you suggested Mr. Rogers, it works perfect, the same line that prints 'ascii' also does the conversion from (long) output into ascii characters.

char ascii[10];
unsigned long output = 0;

Serial.println (ultoa (output, ascii, 16));

Then at 'tother end I came up with an idea not too different from yours pommie by converting pairs of ascii characters into 4 hex bytes which can then be compared, this also works perfect.
atoi is no good for this as I found out as atoi only converts numerics, a to f as found in hex are not converted.
Arduino C would benefit from an atoh function.

unsigned char high_nibble,low_nibble,value[4];

for (uint8_t i=0;i<8;i+=2) {
high_nibble = h2d(packetBuffer);
low_nibble = h2d(packetBuffer[i+1]);
value[i/2] = (high_nibble << 4) | low_nibble;
}

unsigned char h2d(unsigned char hex)
{
if(hex > 0x39) hex -= 7; // adjust for hex letters upper or lower case
return(hex & 0xf);
}