Re: How to read 12 bits from a stream ?
- To: mathgroup at smc.vnet.net
- Subject: [mg131885] Re: How to read 12 bits from a stream ?
- From: roby <roby.nowak at gmail.com>
- Date: Wed, 23 Oct 2013 23:45:54 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-outx@smc.vnet.net
- Delivered-to: mathgroup-newsendx@smc.vnet.net
- References: <l450c4$shl$1@smc.vnet.net>
Am Dienstag, 22. Oktober 2013 06:53:24 UTC+2 schrieb J=E1nos L=F6bb: > Hi, > > > > I am trying to read a mixed file - binary and text - with OpenRead and Read and BinaryRead, but I am not seeing a seek option, that is to move the pointer inside the file where I want it to be. Of course I can do a read in a loop with a counter, but I hoped there is a better way to do it. > > > > The file is 16.1MB, the first 912 elements are characters and binary mixed, followed by 16MB binary section. The binary in this second section is packed in the way that 2 times 12 bits of little endian value is packed into 3 bytes. I need to unpack them and put them into big endian order. I am not seeing how can I read 12 bits at once with BinaryRead, it is aligned at the multiples of 8 bits. > > > > Any good tip ? > > > > Thanks ahead, > > J=E1nos Hi Janos, SetStreamPosition[stream, pos] does the positioning miracle BinaryRead[stream, "UnsignedInteger24",ByteOrdering -> -1] lets you read 24 bits BinaryRead[stream, "UnsignedInteger24",ByteOrdering -> -1]//{BitAnd[#, 2^12- 1], BitAnd[BitShiftRight[#, 12], 2^12 - 1]} & gets you two 12bit numbers. Regards Robert