−18−
Example2 )Microsoft Visual Basic.NET
'***** CRC‑16 PROGRAM *****
Function fncCalcCrc(ByVal sCharData As String) As String
Dim intLoopCnt1 As Integer
Dim intLoopCnt2 As Integer
Dim intLoopCnt3 As Integer
Dim intSdChrCnt As Integer
Dim intCrc16Data As Integer
Dim intCarryFlg As Integer
Dim strWorkBuf As String
Dim int2ByteImage(16) As Integer
Dim strCRC As String
intSdChrCnt = Len(sCharData) / 2
intCrc16Data = &HFFFF
For intLoopCnt1 = 1 To intSdChrCnt
intCrc16Data = intCrc16Data Xor Val("&H" + Mid(sCharData, 2 * intLoopCnt1 ‑ 1, 2))
For intLoopCnt2 = 0 To 7
For intLoopCnt3 = 0 To 15
If (intCrc16Data And (2 ^ intLoopCnt3)) = 0 Then
int2ByteImage(intLoopCnt3) = 0
Else
int2ByteImage(intLoopCnt3) = 1
End If
Next intLoopCnt3
intCarryFlg = int2ByteImage(0)
For intLoopCnt3 = 1 To 15
int2ByteImage(intLoopCnt3 ‑ 1) = int2ByteImage(intLoopCnt3)
Next intLoopCnt3
int2ByteImage(15) = 0
intCrc16Data = 0
For intLoopCnt3 = 0 To 15
intCrc16Data = intCrc ((2 ^ intLoopCnt3) * int2ByteImage(intLoopCnt3))
Next intLoopCnt3
If intCarryFlg = 1 Then
intCrc16Data = intCrc16Data Xor &HA001
End If
Next intLoopCnt2
Next intLoopCnt1
strWorkBuf = "0000"
Mid(strWorkBuf, 5 ‑ Len(Hex(intCrc16Data))) = Hex(intCrc16Data)