0

I have a way of obtaining the EDID info of monitors with nVidia API, this gives me an array of 128 unsigned chars. When reading up on the edid data format on wikipedia however I noticed that the letters in the manufacturer id (bytes 8-9) are represented as 5 bit numbers, so I don't know how I go about reading that into C++ as meaningful data.

My plan was to just define a struct type which matched the format of the edid and cast my char array to that struct type, but I don't know if that's possible now since the smallest sized data types I know of in C++ are one byte in size.

Thanks.

Bill.

m0sa
  • 10,712
  • 4
  • 44
  • 91
Bill Walton
  • 811
  • 1
  • 16
  • 31

2 Answers2

2

In order to extract and manipulate information which is less than one byte, you need to use bit-wise operations.

For example, in order to extract a 5-bit number stored as the first (least-significant) 5-bits of a char, you could say:

unsigned char x = (BYTE & 0x1F);

Which will store the value represented by the right 5 bits of BYTE in x. In that example, I used an AND operator (the & operator in C/C++), which basically uses a mask to hide the 3 most-significant (left-most) bits of the value, (using the hex value 1F, which is 00011111 in binary) isolating the initial 5 bits.

Other bitwise operators include OR, XOR, and left/right bit-shifting, which are performed in C++ using the |, ^, << and >> operators respectively.

Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
  • Thanks Charles, I have another question though. The data is stored in a char array, so I will need some kind of offset from which to read the data, the first number is bits 0-4, the second bit is bits 5-9...so that will be the last 3 bits of the first byte and the first bit of the second byte, etc. I don't know how I would start reading from some offset from a char – Bill Walton Apr 17 '12 at 12:27
  • The same way you would address an offset in a char array normally, using the `[]` operator. If you wanted to perform a bit operation to extract 5 bits of the 2nd byte, for example, you could say `array[1] & 0x1F` – Charles Salvia Apr 17 '12 at 12:28
0

Use bit manipulation (i.e. shifting) to extract the bits of each character and use a lookup table (for better portability) to convert them into characters.

uint16_t EDID_vendor_ID = EDID[8] | EDID[9] << 8;
char char_LUT[]={' ', 'A', 'B', /*...*/ 'Z'};
char ID[3] = { char_LUT[ (EDID_vendor_ID >> 6) & 7 ], 
               char_LUT[ (EDID_vendor_ID >> 3) & 7 ],
               char_LUT[ (EDID_vendor_ID     ) & 7 ] }
datenwolf
  • 159,371
  • 13
  • 185
  • 298