c++parsingstring-parsingedid

How to parse numbers data from EDID


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.


Solution

  • 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.