Last active
December 22, 2015 03:29
-
-
Save matma/6410749 to your computer and use it in GitHub Desktop.
Compute CRC for POSNET fiscal printers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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