[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# VB.NET CRC-16

Posted on 2012-08-25
Medium Priority
1,379 Views
Last Modified: 2012-08-25
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 !
0
Question by:vinit_21
• 2
2 Comments

Author Comment

ID: 38332995
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

Accepted Solution

vinit_21 earned 0 total points
ID: 38333230
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
###### Suggested Courses
Course of the Month19 days, 20 hours left to enroll

#### 872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.