using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Converting { class Converter { private Dictionary, byte> enc = new Dictionary, byte>(); private Dictionary, byte> dec = new Dictionary, byte>(); private static byte[] encode = { 0x0C, 0x0D, 0x0E, 0x0F, 0x08, 0x09, 0x0A, 0x0B, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03 }; private static byte[] decode = { 0x09, 0x0A, 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x0D, 0x0E, 0x0F, 0x00 }; public static byte conv(byte pos, byte value, bool enc = true) { byte ret = 0; //pos / 8; //int offset_high = 14; //int offset_low = 0; //int offset_high = 14; int offset_high = 0; int offset_low = 0; if (enc) { offset_high = (14 - (pos / 16) >= 0) ? 14 - (pos / 16) : 15; offset_low = (14 - (pos % 16) >= 0) ? 14 - (pos % 16) : 15; if (pos % 16 == 15) { offset_high = (offset_high - 1 >= 0) ? offset_high - 1 : 15; } offset_high = offset_high % 16; offset_high = offset_high + (((value / 8) + offset_low) / 16); int ol = ((value / 8) + offset_low) % 16; int oh = ((((value & 0x0F) & 0x7) * 2) + offset_high) % 16; ret = (byte)(encode[oh] << 4); ret = (byte)(ret | encode[ol]); } else { offset_low = decode[(pos+1)%16]; offset_high = ((pos / 16)+1)%16; if (pos % 16 == 15) { offset_high = (offset_high + 1) % 16; } offset_high = decode[offset_high]; byte nh = (byte)(value >> 4); byte nl = (byte)(value & 0x0F); int oh = (nh + offset_high) % 16; int ol = (nl + offset_low) % 16; ret = (byte)(encode[oh] << 4); ret = (byte)(ret | encode[ol]); } //int offset_high = pos + 1; //int offset_low = 1; //int offset_low = (offset_high / 16) + 1; //byte n1 = (byte)(value >> 4); //byte n2 = (byte)(value & 0x0F); //int over = (((value / 8) + offset_low) / 16) % 2; //ret = (byte)(nibble_high[(((value & 0x0F) & 0x7) + offset_high) % 8] + over << 4); return ret; } public Converter() { for (int i = 0; i < 256; i++) { for (int j = 0; j < 256; j++) { enc.Add(new Tuple((byte)i, (byte)j), conv((byte)i, (byte)j, true)); dec.Add(new Tuple((byte)i, conv((byte)i, (byte)j, true)), (byte)j); } } } public byte encoding(byte pos, byte value) { return enc[new Tuple(pos, value)]; } public byte decoding(byte pos, byte value) { return dec[new Tuple(pos, value)]; } } }