Skip to content Skip to sidebar Skip to footer

Convert Rows Of Hexadecimal Values To Binary, Vertically By Column

I am working with data coming from a serial device which outputs its data in a very interesting format. The device has a 256x256 array of pixels, whereas each pixel has a 14-bit va

Solution 1:

I had to read it a few times but I think Ive got it

data = \
"""255    255    255    255    255    255    255    255
127    255    255    255    255    255    255    255
255    255    255    255    255    255    255    255
255    255    255    255    255    255    255    255
255    255    255    255    255    255    255    255
255    255    255    255    255    255    255    255"""

data_rows = [map(int,row.split()) for row in data.splitlines()]
data_bin_rows = ["".join(map(lambda val:"{0:08b}".format(val,),row)) for row in data_rows]
pixel_values = zip(*data_bin_rows)
print pixel_values[0],"=",int("".join(pixel_values[0]),2) #pixel0

cant speak to its speed ... but its probably reasonable if your not doing it like a million times a second ... should be much faster than the serial read in anycase ...

Solution 2:

Using numpy should be fast enough or you need to write it with assembler:

importnumpyinput_array= numpy.zeros((32,14,256), dtype="B")
output_array = numpy.zeros((32,8,256), dtype='int16')
for j in range(8):
    bits = (input_array[:,:,:]>>j) & 1for i in range(14):
        output_array[:,j,:]|= bits[:,i,:] << i

Post a Comment for "Convert Rows Of Hexadecimal Values To Binary, Vertically By Column"