c# - What the lack of CRC-CCITT (0xFFFF)? -
based online crc calculation, when entered hex string data =
503002080000024400003886030400000000010100
i result crc-ccitt (0xffff) =
0x354e (expected result)
.
i use code below, results of calccrc16() 0xacee. lack of script below?
using system; using system.windows.forms; using system.runtime.remoting.metadata.w3cxsd2001; using system.diagnostics; namespace windowsformsapplication1 { public partial class form1 : form { public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { string result = calccrc16("503002080000024400003886030400000000010100"); debug.print(result); // result = acee // result expected = 354e } // crc-ccitt (0xffff) poly 0x1021 // input (hex string) = "503002080000024400003886030400000000010100" // result expected (hex string) = "354e" public string calccrc16(string strinput) { ushort temp = 0; ushort crc = 0xffff; byte[] bytes = getbytesfromhexstring(strinput); (int j = 0; j < bytes.length; j++) { crc = (ushort)(crc ^ bytes[j]); (int = 0; < 8; i++) { if ((crc & 0x0001) == 1) crc = (ushort)((crc >> 1) ^ 0x1021); else crc >>= 1; } } crc = (ushort)~(uint)crc; temp = crc; crc = (ushort)((crc << 8) | (temp >> 8 & 0xff)); return crc.tostring("x4"); } public byte[] getbytesfromhexstring(string strinput) { byte[] bytaroutput = new byte[] { }; if (!string.isnullorempty(strinput) && strinput.length % 2 == 0) { soaphexbinary hexbinary = null; try { hexbinary = soaphexbinary.parse(strinput); if (hexbinary != null) bytaroutput = hexbinary.value; } catch (exception ex) { messagebox.show(ex.message); } } return bytaroutput; } } }
i found answer , share here.. may useful others.
strinput = 503002080000024400003886030400000000010100
initial = 0xffff
poly = 0x1021
stroutput = 354e
reference = online crc calc
public string calccrc16(string strinput) { ushort crc = 0xffff; byte[] data = getbytesfromhexstring(strinput); (int = 0; < data.length; i++) { crc ^= (ushort)(data[i] << 8); (int j = 0; j < 8; j++) { if ((crc & 0x8000) > 0) crc = (ushort)((crc << 1) ^ 0x1021); else crc <<= 1; } } return crc.tostring("x4"); } public byte[] getbytesfromhexstring(string strinput) { byte[] bytaroutput = new byte[] { }; if (!string.isnullorempty(strinput) && strinput.length % 2 == 0) { soaphexbinary hexbinary = null; try { hexbinary = soaphexbinary.parse(strinput); if (hexbinary != null) { bytaroutput = hexbinary.value; } } catch (exception ex) { messagebox.show(ex.message); } } return bytaroutput; }
Comments
Post a Comment