C
chris.santry
I'm trying to convert the following Perl code to VB.net
###################################################################
# Calculates the checksum for the message
#
# In: The string
# Out: The checksum
sub _calcCheckSum {
my ( $self, $str ) = @_;
my @sum = ();
# Loop through all the characters
for my $pos1 ( 0 .. length($str) - 1 ) {
my $char = substr( $str, $pos1, 1 );
# Convert the character to 8 bits
my $bin = $self->_charToBin($char);
# Sum the bits position
for my $pos2 ( 0 .. length($bin) - 1 ) {
$sum[$pos2] += substr( $bin, $pos2, 1 );
}
}
my $sumstr = "";
# Loop through the sums of the 8 positions
for ( 0 .. 7 ) {
my $tmp = sprintf( "%b", $sum[$_] );
# Add the first bit
$sumstr .= substr( $tmp, length($tmp) - 1, 1 );
}
# Convert the new byte to an integer
my $int = oct("0b$sumstr");
# Return the hex of the integer
return uc sprintf( "%02x", $int );
}
# Converts a charater to 8 bits
# In: Character
# Out: 8 bits as a string
sub _charToBin {
my ( $self, $char ) = @_;
my $bin = sprintf( "%b", ord($char) );
while ( length($bin) < 8 ) {
$bin = "0" . $bin;
}
return $bin;
}
#############################################
and so far I have the following:
Public Function DecimalToBinary(ByVal DecimalNum As Long) As
String
Dim tmp As String
Dim n As Long
n = DecimalNum
tmp = Trim(Str(n Mod 2))
n = n \ 2
Do While n <> 0
tmp = Trim(Str(n Mod 2)) & tmp
n = n \ 2
Loop
' tmp = tmp & StrDup(7 - tmp.ToString.Length, "0")
DecimalToBinary = tmp
End Function
Public Function BinaryToDecimal(ByVal Binary As String) As Long
Dim n As Long
Dim s As Integer
For s = 1 To Len(Binary)
n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ _
(s - 1)))
Next s
BinaryToDecimal = n
End Function
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim str As String
Dim sum(7) As String
Dim i As Decimal
Dim j As Decimal
Dim k As Decimal
Dim curchar As String
Dim ccbs As String
Dim temp As String
Dim sumstr As String
sumstr = ""
str = TextBox1.Text
For i = 0 To str.Length - 1
Dim ccba As Byte()
curchar = str.Substring(i, 1)
ccba = System.Text.Encoding.UTF8.GetBytes(curchar)
ccbs = DecimalToBinary(Asc(curchar))
For k = 0 To 7
If k > ccbs.Length - 1 Then
sum(k) += 0
Else
sum(k) += CInt(ccbs.Substring(k, 1))
End If
Next
Next
For i = 0 To 7
temp = DecimalToBinary(sum(i))
If sumstr = "" Then
sumstr = CInt(temp.Substring(temp.Length - 1, 1))
Else
sumstr = sumstr & CInt(temp.Substring(temp.Length - 1,
1))
End If
Next
MsgBox(Hex(BinaryToDecimal(sumstr)))
End Sub
However, this code gives a different checksum to the perl version..
e.g the perl version give the checksum of
<L1><PA><FA><Ma><WB><FA>Hello! Just a test to see how this works!<CL>
as 39. My version gives it as D8.
Can anyone see where I've gone wrong?
Many thanks in advance guys!
###################################################################
# Calculates the checksum for the message
#
# In: The string
# Out: The checksum
sub _calcCheckSum {
my ( $self, $str ) = @_;
my @sum = ();
# Loop through all the characters
for my $pos1 ( 0 .. length($str) - 1 ) {
my $char = substr( $str, $pos1, 1 );
# Convert the character to 8 bits
my $bin = $self->_charToBin($char);
# Sum the bits position
for my $pos2 ( 0 .. length($bin) - 1 ) {
$sum[$pos2] += substr( $bin, $pos2, 1 );
}
}
my $sumstr = "";
# Loop through the sums of the 8 positions
for ( 0 .. 7 ) {
my $tmp = sprintf( "%b", $sum[$_] );
# Add the first bit
$sumstr .= substr( $tmp, length($tmp) - 1, 1 );
}
# Convert the new byte to an integer
my $int = oct("0b$sumstr");
# Return the hex of the integer
return uc sprintf( "%02x", $int );
}
# Converts a charater to 8 bits
# In: Character
# Out: 8 bits as a string
sub _charToBin {
my ( $self, $char ) = @_;
my $bin = sprintf( "%b", ord($char) );
while ( length($bin) < 8 ) {
$bin = "0" . $bin;
}
return $bin;
}
#############################################
and so far I have the following:
Public Function DecimalToBinary(ByVal DecimalNum As Long) As
String
Dim tmp As String
Dim n As Long
n = DecimalNum
tmp = Trim(Str(n Mod 2))
n = n \ 2
Do While n <> 0
tmp = Trim(Str(n Mod 2)) & tmp
n = n \ 2
Loop
' tmp = tmp & StrDup(7 - tmp.ToString.Length, "0")
DecimalToBinary = tmp
End Function
Public Function BinaryToDecimal(ByVal Binary As String) As Long
Dim n As Long
Dim s As Integer
For s = 1 To Len(Binary)
n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ _
(s - 1)))
Next s
BinaryToDecimal = n
End Function
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim str As String
Dim sum(7) As String
Dim i As Decimal
Dim j As Decimal
Dim k As Decimal
Dim curchar As String
Dim ccbs As String
Dim temp As String
Dim sumstr As String
sumstr = ""
str = TextBox1.Text
For i = 0 To str.Length - 1
Dim ccba As Byte()
curchar = str.Substring(i, 1)
ccba = System.Text.Encoding.UTF8.GetBytes(curchar)
ccbs = DecimalToBinary(Asc(curchar))
For k = 0 To 7
If k > ccbs.Length - 1 Then
sum(k) += 0
Else
sum(k) += CInt(ccbs.Substring(k, 1))
End If
Next
Next
For i = 0 To 7
temp = DecimalToBinary(sum(i))
If sumstr = "" Then
sumstr = CInt(temp.Substring(temp.Length - 1, 1))
Else
sumstr = sumstr & CInt(temp.Substring(temp.Length - 1,
1))
End If
Next
MsgBox(Hex(BinaryToDecimal(sumstr)))
End Sub
However, this code gives a different checksum to the perl version..
e.g the perl version give the checksum of
<L1><PA><FA><Ma><WB><FA>Hello! Just a test to see how this works!<CL>
as 39. My version gives it as D8.
Can anyone see where I've gone wrong?
Many thanks in advance guys!