VB.NET CRC-16

Hi Experts ,

need your help here. I am trying to calculate CRC-16 for a Hex Command.
For Example - When I send 110300000010 - I am expecting 4696 As CRC Checksum.

Can anyone help me or point me to the right code. I would prefer HEX String as Input Data -
Thanks for your great help - Cheers !
vinit_21Asked:
Who is Participating?
 
vinit_21Author Commented:
I managed. Here is the code in VB - incase somebody wants it,

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim x As Long
        Dim mask, i, j, nC, Crc As Integer
        Dim c As String
        Dim txt As String
        txt = "110300D51111"

        Crc = &HFFFF ' crc mit $ffff initalisieren

        For nC = 1 To Len(txt) Step 2
            j = Val("&H" + Mid(txt, nC, 2)) 'im HEX-Format
            Crc = Crc Xor j
            For j = 1 To 8
                mask = 0
                If Crc / 2 <> Int(Crc / 2) Then mask = &HA001
                Crc = Int(Crc / 2) And &H7FFF : Crc = Crc Xor mask
            Next j
        Next nC

        txt = Hex$(Crc) 'Checksumme
        Received.AppendText(txt)
    End Sub
0
 
vinit_21Author Commented:
I found a code in Delphi - See if anyone can help me to convert to vb.net

class function TCM.CRC16( s : string): string;
var
  ts,s2 : string;
  i,j,code: Integer;
  crc,data : word;
  HL:string;
  HByte:string;
  LByte:string;
begin
  j := 1;
  crc := $ffff;
  S2:='';
  for i:=1 to length(s) do
  begin
    if ((copy(s,i,1)>='0') and (copy(s,i,1)<='9'))or((copy(s,i,1)>='a') and (copy(s,i,1)<='f'))
        or((copy(s,i,1)>='A') and (copy(s,i,1)<='F')) then
    begin
        s2:=s2+copy(s,i,1);
    end;
  end;
  S:=S2;
  while j<=Length(S) do
  begin
    while (j<=Length(S)) and (not (S[j] in ['0'..'9','A'..'F','a'..'f'])) do
      inc(j);
    if (j+1>Length(S))or(not (S[j+1] in ['0'..'9','A'..'F','a'..'f'])) then
      ts:='$'+S[j]
    else
      ts:='$'+S[j]+S[j+1];
    Val(ts,data,Code);
    inc(j,2);
    crc := crc xor data;
    for i:=0 to 7 do
    if (crc and $01) <> 0 then
      crc := (crc shr 1) xor $a001
    else crc := crc shr 1;
  end;
  HL:=inttohex(crc,4);
  LByte:=Copy(HL,3,2);
  HByte:=Copy(HL,1,2);
  Result := LByte+HByte;
end;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.