/    Sign up×
Community /Pin to ProfileBookmark

Need help converting a C# function

Original code

[code]
public string Hash(string input)
{
uint m_crc = 0xffffffff;
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
bytes = encoding.GetBytes(input.ToLower());
foreach (byte myByte in bytes)
{
m_crc ^= ((uint)(myByte) << 24);
for (int i = 0; i < 8; i++)
{
if ((System.Convert.ToUInt32(m_crc) & 0x80000000) == 0x80000000)
{
m_crc = (m_crc << 1) ^ 0x04C11DB7;
}
else
{
m_crc <<= 1;
}
}
}
return String.Format(“{0:x8}”, m_crc);
}
[/code]

My failing attempt in javascript… HELP!

[code]
function findCRC( string ) {
var CRC = 0xffffffff;
for ( var j = 0; j < string.length; j++ ) {
var c = string.charCodeAt(j);
CRC ^= c << 24;
for (var i = 0; i < 8; i++) {
if ( CRC & 0x800000000 )
CRC = (CRC << 1) ^ 0x04C11DB7;
else
CRC <<=1;
}
}
return CRC.toString(16);
}
[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@justinbarneskinSep 25.2009 — Give a sample for [I]string[/I] and

what it should return
Copy linkTweet thisAlerts:
@jamesbcox1980Sep 25.2009 — I don't know anything about C#, although I hear Java is built off it. So I don't recognize anything in it. Maybe it would help if you tell us exactly what the function is doing in plain English step by step. I could then write a javascript function which matches that.
Copy linkTweet thisAlerts:
@FiascoauthorSep 25.2009 — Thanks for your interst guys. I figured it out by brute force (tweak and look at the inspector output in safari) until I came up with this.

Input would be something like c:movieswag the dog.iso

Output would be something like 3ea45a04. (example not actual)

This is for XBMC media server which uses this alogarithm to name the thumbnails for it's media files (dvd covers ect) based on the path and filename.

The purpose is for AJAX iphone remote which controls xbmc, onkyo receiver, usb-uirt for IR devices, DirecTV, lutron homeworks and a few other home automation chores.

<i>
</i>Number.prototype.unsign = function(bytes) {
return this &gt;= 0 ? this : Math.pow(256, bytes || 4) + this;
};

function FindCRC(data) {
var CRC=0xffffffff;
data = data.toLowerCase();
//console.log(data);
for (var j=0; j &lt; data.length; j++) {
var c = data.charCodeAt(j);
CRC ^= c &lt;&lt; 24;
for(var i = 0; i&lt;8; i++) {
if(CRC.unsign(8) &amp; 0x80000000)
CRC = (CRC &lt;&lt; 1) ^ 0x04C11DB7;
else
CRC &lt;&lt;= 1;
}
}
if ( CRC &lt; 0 ) CRC = CRC&gt;&gt;&gt;0;
return CRC.toString(16);
}
×

Success!

Help @Fiasco spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...