I need to create and check 16-bit crc’s, using a polynomial of 0x1021 and an initial value of 0xFFFF ( BIG endian ). Does anyone have a class or module written they’d be willing to share?
Many thanks,
-Bill
I need to create and check 16-bit crc’s, using a polynomial of 0x1021 and an initial value of 0xFFFF ( BIG endian ). Does anyone have a class or module written they’d be willing to share?
Many thanks,
-Bill
I think I have this somewhere…
I appreciate your time! (given how late it is where you are) Thanks!
Just sitting on the hotel bar and checking forum for news…
I made a class for RealBasic long ago…
http://forums.realsoftware.com/viewtopic.php?f=1&t=26533
Haven’t updated it. Shouldn’t be too hard, if it’s not still working.
EDIT: May have to right-click the link and ‘save as’…
Christian: Well then let me say “Thanks in advance” and let you get back to a more
important endeavor! Cheers!
Eric: I’ll go check it out!
Eric: thanks for the link - it may take a little dusting off but I’m gonna give is a shot.
Christian: if you dig anything up it’d be appreciated Thanks!
maybe like this?
[code] 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
next
crc = BitwiseAnd(crc, &hFFFF)
return crc
[/code]
The input would be a string, for instance “*F?”, and return “0x8021”. Do you have an adaptation of your routine you posted that could use the same 0x1021 polynomial and initial value of 0xFFFF and iterate over a string? (or does XOJO call that “Text” now?)
Many thanks!
dim m as memoryblock = yourstring
to convert.
I tried to modify this example to get the CRC16 CCITT (XModem)
, but haven’t succeeded
Has anyone an idea how to code that?
The expected result can be seen in this On-line CRC calculation
There are examples in other programming languages, but I don’t quite understand what to do with all the >>, <<, ^
.
It sure looks similar to what @Antonio Rinaldi has posted in this thread.
Replacing the ‘table’ isn’t the big issue, but understanding what’s going on below it, and get that in sync with the >>, <<, ^
used in other languages…
In Xojo, these operators correspond to Bitwise.ShiftRight
, Bitwise.ShiftLeft
, and xor
.
Thanks. That helped
One more question I’m not seeing the solution in front of me…
How can I create an Array of UInt16 values (I want/need to use the HexValues) ?
It’s that ‘Table’ being used… but I don’t want to .Append every single item, but declare it as an array in form of a list.
Dim arrayUInt16s() As UInt16
arrayUInt16s = Array(&h2042, &h3063, &h4084)
For Target64Bit the compiler complains: Expected UInt16(), but got UInt64()
For Target32Bit the compiler complains: Expected UInt16(), but got UInt32()
Even if I try this:
Dim arrayUInt16s() As UInt16
arrayUInt16s = Array(UInt16(&h0000), UInt16(&h1021))
Please lookup Ctype in the documentation.
Or maybe pass first item as a variable of type UInt16
I already tried to define the first item as UInt16:
Dim arrayUInt16s1() As UInt16 = Array(CType(&h0000, UInt16), &h1021)
Dim arrayUInt16s2() As UInt16 = Array(CType(&h0000, UInt16), CType(&h1021, UInt16))
First line doesn’t compile. It only does if every single item is CType’d.
Same for this - doesn’t compile:
Dim iFirst As UInt16 = &h0000
Dim arrayUInt16s1() As UInt16 = Array(iFirst, &h1021)
Is there really no other way than CType every single item in the list? Otherwise I would need the list twice - for Target32Bit and Target64Bit…
Maybe you just use append to add values to the array?
It seems to be the only way… But to have the ‘Table’ look better in code, I’ve worked around by using a String
Thanks @Andrew Lambert for your input and @Antonio Rinaldi for his head start.
@Christian Schmitz : I’m sure you have a Plugin function for that? In which Plugin-part is it - and how would one do it with your Plugin?
So here is a possible way to get the CRC-16 XMODEM
or CRC-16 CCITT-FALSE
(see commented-out line of code). I’ve found a nicer webservice to check the result: Online CRC Calculator
[code]Public Function CRC16_CCITT_XMODEM(psValue As String) as UInt16
Dim mb As MemoryBlock = psValue
Static aCRCTable() As UInt16
If (aCRCTable.Ubound < 0) Then
Dim sBuildCRCTable As String = _
"&h0000, &h1021, &h2042, &h3063, &h4084, &h50A5, &h60C6, &h70E7, " + _
"&h8108, &h9129, &hA14A, &hB16B, &hC18C, &hD1AD, &hE1CE, &hF1EF, " + _
"&h1231, &h0210, &h3273, &h2252, &h52B5, &h4294, &h72F7, &h62D6, " + _
"&h9339, &h8318, &hB37B, &hA35A, &hD3BD, &hC39C, &hF3FF, &hE3DE, " + _
"&h2462, &h3443, &h0420, &h1401, &h64E6, &h74C7, &h44A4, &h5485, " + _
"&hA56A, &hB54B, &h8528, &h9509, &hE5EE, &hF5CF, &hC5AC, &hD58D, " + _
"&h3653, &h2672, &h1611, &h0630, &h76D7, &h66F6, &h5695, &h46B4, " + _
"&hB75B, &hA77A, &h9719, &h8738, &hF7DF, &hE7FE, &hD79D, &hC7BC, " + _
"&h48C4, &h58E5, &h6886, &h78A7, &h0840, &h1861, &h2802, &h3823, " + _
"&hC9CC, &hD9ED, &hE98E, &hF9AF, &h8948, &h9969, &hA90A, &hB92B, " + _
"&h5AF5, &h4AD4, &h7AB7, &h6A96, &h1A71, &h0A50, &h3A33, &h2A12, " + _
"&hDBFD, &hCBDC, &hFBBF, &hEB9E, &h9B79, &h8B58, &hBB3B, &hAB1A, " + _
"&h6CA6, &h7C87, &h4CE4, &h5CC5, &h2C22, &h3C03, &h0C60, &h1C41, " + _
"&hEDAE, &hFD8F, &hCDEC, &hDDCD, &hAD2A, &hBD0B, &h8D68, &h9D49, " + _
"&h7E97, &h6EB6, &h5ED5, &h4EF4, &h3E13, &h2E32, &h1E51, &h0E70, " + _
"&hFF9F, &hEFBE, &hDFDD, &hCFFC, &hBF1B, &hAF3A, &h9F59, &h8F78, " + _
"&h9188, &h81A9, &hB1CA, &hA1EB, &hD10C, &hC12D, &hF14E, &hE16F, " + _
"&h1080, &h00A1, &h30C2, &h20E3, &h5004, &h4025, &h7046, &h6067, " + _
"&h83B9, &h9398, &hA3FB, &hB3DA, &hC33D, &hD31C, &hE37F, &hF35E, " + _
"&h02B1, &h1290, &h22F3, &h32D2, &h4235, &h5214, &h6277, &h7256, " + _
"&hB5EA, &hA5CB, &h95A8, &h8589, &hF56E, &hE54F, &hD52C, &hC50D, " + _
"&h34E2, &h24C3, &h14A0, &h0481, &h7466, &h6447, &h5424, &h4405, " + _
"&hA7DB, &hB7FA, &h8799, &h97B8, &hE75F, &hF77E, &hC71D, &hD73C, " + _
"&h26D3, &h36F2, &h0691, &h16B0, &h6657, &h7676, &h4615, &h5634, " + _
"&hD94C, &hC96D, &hF90E, &hE92F, &h99C8, &h89E9, &hB98A, &hA9AB, " + _
"&h5844, &h4865, &h7806, &h6827, &h18C0, &h08E1, &h3882, &h28A3, " + _
"&hCB7D, &hDB5C, &hEB3F, &hFB1E, &h8BF9, &h9BD8, &hABBB, &hBB9A, " + _
"&h4A75, &h5A54, &h6A37, &h7A16, &h0AF1, &h1AD0, &h2AB3, &h3A92, " + _
"&hFD2E, &hED0F, &hDD6C, &hCD4D, &hBDAA, &hAD8B, &h9DE8, &h8DC9, " + _
"&h7C26, &h6C07, &h5C64, &h4C45, &h3CA2, &h2C83, &h1CE0, &h0CC1, " + _
"&hEF1F, &hFF3E, &hCF5D, &hDF7C, &hAF9B, &hBFBA, &h8FD9, &h9FF8, " + _
“&h6E17, &h7E36, &h4E55, &h5E74, &h2E93, &h3EB2, &h0ED1, &h1EF0”
Dim aBuildCRCTableItems() As String = Split(sBuildCRCTable, ", ")
For i As Integer = 0 To aBuildCRCTableItems.ubound
aCRCTable.Append(Val(aBuildCRCTableItems(i)))
Next
End If
Dim bTemp As Byte
Dim crc As UInt16 = &h0 //CRC-16 XMODEM
'Dim crc As UInt16 = &hffff //CRC-16 CCITT-FALSE
For i As Integer = 0 To mb.Size-1
bTemp = Bitwise.BitXor(Bitwise.ShiftRight(crc, 8), mb.Byte(i))
crc = Bitwise.BitXor(Bitwise.ShiftLeft(crc,8), aCRCTable(bTemp))
Next
Return crc
End Function[/code]
It has been fun learning more about CRC… I’ve dropped the static pre-calculated Tables and implemented the Algorithms in pure Xojo Code.
So if you’re interested or in case you ever need different “flavours” of CRC8, CRC16, CRC32: Have a look at this Xojo Code Example: CRCCalculator (Cyclic Redundancy Check: CRC8, CRC16, CRC32).