Skip to content

Instantly share code, notes, and snippets.

@jmclemo6
Last active February 25, 2018 18:41
Show Gist options
  • Save jmclemo6/96bc6e732bcddfc7c82e7d57cf734b35 to your computer and use it in GitHub Desktop.
Save jmclemo6/96bc6e732bcddfc7c82e7d57cf734b35 to your computer and use it in GitHub Desktop.
Decodes 9 digit DPD to a decimal value
static public System.UInt32 GetDecimalFromDPD(this System.Collections.BitArray input) {
/* 36 bits because the three 10 bit quantities will expand to 12 bits each */
var Bits = new System.Collections.BitArray(36);
/* I could probably shorten this with a loop, but I haven't taken the time to figure that logic out. */
System.Boolean bit_0, bit_1, bit_2, bit_3, bit_4, bit_5, bit_6, bit_7, bit_8, bit_9;
/* Since we have 32 bit of space, but only 30 bits of data we ignore the first two bits */
/* This decodes the first 10 bits of data */
bit_0 = input[2];
bit_1 = input[3];
bit_2 = input[4];
bit_3 = input[5];
bit_4 = input[6];
bit_5 = input[7];
bit_6 = input[8];
bit_7 = input[9];
bit_8 = input[10];
bit_9 = input[11];
Bits[0] = (bit_6 & bit_7) & (!bit_3 | bit_4 | !bit_8);
Bits[1] = bit_0 & (!bit_6 | !bit_7 | (bit_3 & !bit_4 & bit_8));
Bits[2] = bit_1 & (!bit_6 | !bit_7 | (bit_3 & !bit_4 & bit_8));
Bits[3] = bit_2;
Bits[4] = bit_6 & ((!bit_7 & bit_8) | (!bit_4 & bit_8) | (bit_3 & bit_8));
Bits[5] = (bit_3 & (!bit_6 | !bit_8)) | (bit_0 & !bit_3 & bit_4 & bit_6 & bit_7 & bit_8);
Bits[6] = (bit_4 & (!bit_6 | !bit_8)) | (bit_1 & !bit_3 & bit_4 & bit_7);
Bits[7] = bit_5;
Bits[8] = bit_6 & ((!bit_7 & !bit_8) | (bit_7 & bit_8 & (bit_3 | bit_4)));
Bits[9] = (!bit_6 & bit_7) | (bit_3 & bit_6 & !bit_7 & bit_8) | (bit_0 & bit_7 & (!bit_8 | (!bit_3 & !bit_4)));
Bits[10] = (!bit_6 & bit_8) | (bit_4 & !bit_7 & bit_8) | (bit_1 & bit_6 & bit_7 & (!bit_8 | (!bit_3 & !bit_4)));
Bits[11] = bit_9;
/* This decodes the second 10 bits of data */
bit_0 = input[12];
bit_1 = input[13];
bit_2 = input[14];
bit_3 = input[15];
bit_4 = input[16];
bit_5 = input[17];
bit_6 = input[18];
bit_7 = input[19];
bit_8 = input[20];
bit_9 = input[21];
Bits[12] = (bit_6 & bit_7) & (!bit_3 | bit_4 | !bit_8);
Bits[13] = bit_0 & (!bit_6 | !bit_7 | (bit_3 & !bit_4 & bit_8));
Bits[14] = bit_1 & (!bit_6 | !bit_7 | (bit_3 & !bit_4 & bit_8));
Bits[15] = bit_2;
Bits[16] = bit_6 & ((!bit_7 & bit_8) | (!bit_4 & bit_8) | (bit_3 & bit_8));
Bits[17] = (bit_3 & (!bit_6 | !bit_8)) | (bit_0 & !bit_3 & bit_4 & bit_6 & bit_7 & bit_8);
Bits[18] = (bit_4 & (!bit_6 | !bit_8)) | (bit_1 & !bit_3 & bit_4 & bit_7);
Bits[19] = bit_5;
Bits[20] = bit_6 & ((!bit_7 & !bit_8) | (bit_7 & bit_8 & (bit_3 | bit_4)));
Bits[21] = (!bit_6 & bit_7) | (bit_3 & bit_6 & !bit_7 & bit_8) | (bit_0 & bit_7 & (!bit_8 | (!bit_3 & !bit_4)));
Bits[22] = (!bit_6 & bit_8) | (bit_4 & !bit_7 & bit_8) | (bit_1 & bit_6 & bit_7 & (!bit_8 | (!bit_3 & !bit_4)));
Bits[23] = bit_9;
/* This decodes the third 10 bits of data */
bit_0 = input[22];
bit_1 = input[23];
bit_2 = input[24];
bit_3 = input[25];
bit_4 = input[26];
bit_5 = input[27];
bit_6 = input[28];
bit_7 = input[29];
bit_8 = input[30];
bit_9 = input[31];
Bits[24] = (bit_6 & bit_7) & (!bit_3 | bit_4 | !bit_8);
Bits[25] = bit_0 & (!bit_6 | !bit_7 | (bit_3 & !bit_4 & bit_8));
Bits[26] = bit_1 & (!bit_6 | !bit_7 | (bit_3 & !bit_4 & bit_8));
Bits[27] = bit_2;
Bits[28] = bit_6 & ((!bit_7 & bit_8) | (!bit_4 & bit_8) | (bit_3 & bit_8));
Bits[29] = (bit_3 & (!bit_6 | !bit_8)) | (bit_0 & !bit_3 & bit_4 & bit_6 & bit_7 & bit_8);
Bits[30] = (bit_4 & (!bit_6 | !bit_8)) | (bit_1 & !bit_3 & bit_4 & bit_7);
Bits[31] = bit_5;
Bits[32] = bit_6 & ((!bit_7 & !bit_8) | (bit_7 & bit_8 & (bit_3 | bit_4)));
Bits[33] = (!bit_6 & bit_7) | (bit_3 & bit_6 & !bit_7 & bit_8) | (bit_0 & bit_7 & (!bit_8 | (!bit_3 & !bit_4)));
Bits[34] = (!bit_6 & bit_8) | (bit_4 & !bit_7 & bit_8) | (bit_1 & bit_6 & bit_7 & (!bit_8 | (!bit_3 & !bit_4)));
Bits[35] = bit_9;
/* Now we take our decoded data which is in BCD format where every Nybble represents a digit and convert it to decimal*/
System.UInt32 Val = 0;
for(System.Int32 i = 0; i < Bits.Length; i+=4) {
System.Byte current_byte = 0;
for(System.Int32 j = i; j < i + 4; j++) {
current_byte += System.Convert.ToByte(Bits[j]);
}
System.Console.Write($"{System.Convert.ToString(current_byte.GetHighNybble(), 2).PadLeft(4, '0')}_{System.Convert.ToString(current_byte.GetLowNybble(), 2).PadLeft(4, '0')} ");
}
System.Console.Write("\n");
return Val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment