Below I’ve pasted a function in .c for calculating the CRC (check sum) for a string. I want to duplicate this functionality in Xojo, however my .c skills are a quite dusty. Below the .c snippet I’ve pasted some Xojo code that was suggested by a great source in our forum. However, it’s not working like the c. version does. Any help with this conversion would be greatly appreciated!
/* Works from LSb to MSb so the polynomial needs to be bit reversed. */
for(i = 0; i < 8; i++) {
crc = (crc >> 1) ^ (-(int)(crc & 1) & BIT_REVERSE_SHORT(POLYNOMIAL16));
}
}
return crc;
}
/************************************** END OF FILE **************************************/[/code]
[code]// Thanks to Christian Schmitz for posting this earlier.
dim m as memoryblock = “3E6400104395FFF938DAB6C641B9BC243FD7B51D000D” // crc: 6D7E
dim crc as integer = &hFFFF
dim u as integer = m.size-1
for i as integer = 0 to u
dim ch as integer = m.UInt8Value(i)
dim cl as integer = 0
for j as integer = 0 to 7
cl = BitwiseXor( crc, ch)
crc = Bitwise.ShiftRight(crc, 1)
if BitwiseAnd(cl, 1) <> 0 then
crc = BitwiseXor(crc, &h1021)
end if
ch = Bitwise.ShiftRight(ch, 1)
next
I think the only thing missing from christians is the macro conversion to some source code to do what the macro does (in a hugely unsafe way
This “seems” about right (but its “forum code” meaning its completely untested as its all written inline here so it may not even compile)
Should be close enough to get you started
Protected Function BIT_REVERSE_SHORT(x as Uint16) As Uint16
return x_BRS(x)
End Function
Protected Function x_BRS(x as uint16) As uint16
return BiteWise.BitOr( Bitwise.ShiftRight(( x__BRS(x), 8) and &h00FF) , BitWise.ShiftLeft( (x__BRS(x) and &h00FF) , 8 ))
End Function
Protected Function x__BRS(x as uint16) As uint16
return
Bitwise.BitOr( BitWise.ShiftRight(x___BRS(x) , 4) and &h0F0F , BitWise.ShiftLeft(x___BRS(x) and &h0F0F , 4) )
End Function
Protected Function x___BRS(x as uint16) As uint16
return BitWise.BitOr( BitWise.ShiftRight( x____BRS(x), 2) and &h3333 , BitWise.ShiftLeft( (x____BRS(x) and &h3333) , 2) )
End Function
Protected Function x____BRS(x as uint16) As uint16
return BitWise.BitOr( (bitwise.ShiftRight(x,1) and &h5555), BitWise.ShiftLeft((x and &h5555), 1) )
End Function