2013-04-18 2 views
1

На основе Online CRC calculation, когда я вошел в шестнадцатеричной строки данных =Какая нехватка CRC-CCITT (0xFFFF)?

я получаю результат CRC-CCITT (0xFFFF) =

0x354E (ожидаемый результат)

.

Я использую ниже код, но результаты CalcCRC16() являются 0xACEE. Какая нехватка сценария ниже?

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) with 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); 
      for (int j = 0; j < bytes.Length; j++) { 
       crc = (ushort)(crc^bytes[j]); 
       for (int i = 0; i < 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; 
     } 

    } 
} 

ответ

2

Я нашел ответ, и я поделюсь здесь .. может быть полезен другим.

strInput = 503002080000024400003886030400000000010100

начальная = 0xFFFF

поли = 0x1021

strOutput = 354E

ссылка = Online CRC Calc

public string CalcCRC16(string strInput) { 
    ushort crc = 0xFFFF; 
    byte[] data = GetBytesFromHexString(strInput); 
    for (int i = 0; i < data.Length; i++) { 
     crc ^= (ushort)(data[i] << 8); 
     for (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; 
} 
+0

Ваш код легко конвертируется в C и работает отлично. – Zac

 Смежные вопросы

  • Нет связанных вопросов^_^