On Fri, Oct 7, 2016 at 12:10 PM, Jérémy Béjanin <jeremy.beja...@gmail.com> wrote: > The format is fairly complicated, but I need to take 4 bytes from that > buffer and extract two 12-bit numbers from that 32-bit string (along with > some reversing operations, as you can see in the function). I cannot seem to > find bit operations that address/concatenate individual bits without going > through a string format and parsing. Would you have any guidance as to where > in the manual I should look?
I didn't follow that way too complicated string handling but sth on the line of a & 0xfff and (a >> 16) & 0xfff should do it. You need to adjust to the actual bits location of course. If you have never done anything with bits before, this might be useful in general. https://en.wikipedia.org/wiki/Bitwise_operation > > On Thursday, October 6, 2016 at 8:15:41 PM UTC-4, Yichao Yu wrote: >> >> On Thu, Oct 6, 2016 at 5:40 PM, Jérémy Béjanin <jeremy....@gmail.com> >> wrote: >> > Thanks! this works perfectly. >> > >> > The pointer_to_array() function seems to be deprecated, however, do you >> > know >> > how to use the suggested replacement, unsafe_wrap()? >> >> Yes. >> >> > >> > And if it's not too much to ask, I am wondering how I can do the >> > conversion >> > from this UInt8 buffer more efficiently. This is what I am currently >> > doing, >> > where blocks is the buffer: >> > >> > # We now have all the blocks in the blocks array, we need to extract the >> > # 12-bit samples from that array. The data in the blocks is organized in >> > 4 >> > # bytes / 32-bit words, and each word contains two 12-bit samples. >> > # Calculate the number of 32-bit words in the blocks array >> > numwords = div(numblocks*DIG_BLOCK_SIZE,4) >> > # Initialize array to store the samples >> > data = Array{Int32}(2*numwords) >> > for n=1:numwords >> > # Convert next four bytes to 32-bit string >> > s = >> > >> > reverse(bits(blocks[(n-1)*4+1]))*reverse(bits(blocks[(n-1)*4+2]))*reverse(bits(blocks[(n-1)*4+3]))*reverse(bits(blocks[(n-1)*4+4])) >> > # Parse the first 12 bits as the first sample and the 12 bits 4 bits >> > later as the second sample >> > data[(n-1)*2+1] = parse(Int,reverse(s[1:12]),2) >> > data[(n-1)*2+2] = parse(Int,reverse(s[17:28]),2) >> > end >> > >> > This is pretty slow, I assume due to the translation between numbers and >> > strings. Is there a better way to do this? >> >> I don't know what exactly the format is but you should not go though >> string and you should be able to do what you want with simple >> interger/bits operations. >> >> > >> > Thanks, >> > Jeremy >> >