Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1.         public static string decodePID(byte high, byte low)
  2.         {
  3.             // high and low are the two bytes of the code from OBD port
  4.             /*
  5.             Define an array of bitmasks to pull out code letter and numbers
  6.                         11000000,
  7.                         00110000,
  8.                         00001111,
  9.                         11110000,
  10.                         00001111
  11.              * */
  12.             byte[] mask = new byte[] { 0xC0, 0x30, 0x0F, 0xF0, 0x0F };
  13.             // Lookup array for binary -> PID letter conversion.
  14.             char[] type = new char[] { 'P', 'C', 'B', 'U' };
  15.             // define placeholder for decoded values
  16.             char[] retStr = new char[5];
  17.             // Look up code type by top two bits
  18.             retStr[0] = type[(high & mask[0]) >> 6];
  19.             // convert remaining digits
  20.             retStr[1] = (char)(((high & mask[1]) >> 4) + 48);
  21.             retStr[2] = (char)((high & mask[2]) + 48);
  22.             retStr[3] = (char)(((low & mask[3]) >> 4) + 48);
  23.             retStr[4] = (char)((low & mask[4]) + 48);
  24.             // build string and return
  25.             return new string(retStr);
  26.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement