Skip to content

Instantly share code, notes, and snippets.

@matma
Last active December 22, 2015 03:29
Show Gist options
  • Save matma/6410749 to your computer and use it in GitHub Desktop.
Save matma/6410749 to your computer and use it in GitHub Desktop.
Compute CRC for POSNET fiscal printers
private static readonly Encoding encoding = Encoding.GetEncoding( 1250 );
static readonly ushort[] Table = new ushort[256];
static bool przeliczone = false;
void fillTable() {
for( int i = 0; i < Table.Length; ++i ) {
ushort num2 = 0;
ushort num3 = (ushort)( i << 8 );
for( int j = 0; j < 8; ++j ) {
if( ( ( num2 ^ num3 ) & 0x8000 ) != 0 ) {
num2 = (ushort)( ( num2 << 1 ) ^ 0x1021 );
} else {
num2 = (ushort)( num2 << 1 );
}
num3 = (ushort)( num3 << 1 );
}
Table[i] = num2;
}
}
byte[] computeCRC( byte[] buffer ) {
if( !przeliczone ) {
fillTable();
przeliczone = true;
}
ushort num = 0;
for( int i = 0; i < buffer.Length; i++ ) {
num = (ushort)( ( num << 8 ) ^ Table[( num >> 8 ) ^ ( 0xff & buffer[i] )] );
}
return encoding.GetBytes( String.Format( "{0:X4}", num ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment