Real Software Forums
http://forums.realsoftware.com/

Porting a litle routine
http://forums.realsoftware.com/viewtopic.php?f=13&t=38152
Page 1 of 1

Author:  iwebyou [ Wed Mar 16, 2011 3:58 am ]
Post subject:  Porting a litle routine

Hi,

I'm very new to realbasic. Just bought my lisence yesterday.
However now I'm stock into a problem.
I have to port a litlte routine to realbasic.
I have a perl, javascript and Vb version.

Maybe someone can help. I don't understand that byteshifting stuff.

Javascript code:
Number.prototype.unsign = function(bytes) {
return this >= 0 ? this : Math.pow(256, bytes || 4) + this;
};

function FindCRC(data) {
var CRC=0xffffffff;
data = data.toLowerCase();
for (var j=0; j<data.length; j++) {
var c = data.charCodeAt(j);
CRC ^= c << 24;
for(var i = 0; i<8; i++) {
if(CRC.unsign(8) & 0x80000000) {
CRC = (CRC << 1) ^ 0x04C11DB7;
} else{
CRC <<= 1;
}
}
}
if ( CRC < 0 ) CRC = CRC>>>0;
return CRC.toString(16);
}
alert(FindCRC("Hello World"));

Perl code:
my $dataToHash = "Hello World";
my $tmpstr = sprintf "%08x.tbn\n", xbmcHash($dataToHash);
print substr($tmpstr,length($tmpstr)-13,13);

sub findcrc {

my( $hashInput ) = shift;
$hashInput = lc $hashInput;
my $m_crc = 0xFFFFFFFF;

for my $byte( unpack 'C*', $hashInput ) {
$m_crc = $m_crc ^ ( $byte << 24 );
for (my $rep = 0; $rep < 8; $rep++) {
if (( $m_crc & 0x80000000) == 0x80000000) {
$m_crc = ($m_crc << 1) ^ 0x04C11DB7;
} else {
$m_crc = ($m_crc << 1);
}
}
}
return $m_crc;
}

and an untestet vbcode
Public Function FindCrc(ByVal input As String) As String
Dim chars As Char() = input.ToCharArray()
Dim index As Integer = 0
While index < chars.Length
If Convert.ToSByte(chars(index)) <= 127 Then
chars(index) = System.[Char].ToLowerInvariant(chars(index))
End If
System.Math.Max(System.Threading.Interlocked.Increment(index), index - 1)
End While
input = New String(chars)
Dim m_crc As UInteger = 4294967295
Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(input)
For Each myByte As Byte In bytes
m_crc = m_crc Xor (System.Convert.ToUInt32(myByte) << 24)
Dim i As Integer = 0
While i < 8
If (System.Convert.ToUInt32(m_crc) And System.Convert.ToUInt32(2147483648)) = (2147483648) Then
m_crc = (m_crc << 1) Xor System.Convert.ToUInt32(&H4C11DB7)
Else
m_crc <<= 1
End If
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
Next
Return [String].Format("{0:x8}", m_crc)
End Function

Thank you very much

Author:  iwebyou [ Tue Apr 12, 2011 2:29 pm ]
Post subject:  Re: Porting a litle routine

hm, thats hard :shock:
Lets make this litle bit more intersting.
I would pay 100,-EUR for the solution.

Author:  PSmith [ Sun Jun 05, 2011 7:56 am ]
Post subject:  Re: Porting a litle routine

Did you ever get this converted?

There are lots of free CRC calculator routines around, some more convoluted than others. The one I like uses a lookup table which makes the code much simpler. If interested, I could post it.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/