Problem with bits in byte array....


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Problem with bits in byte array....

    Henrik, thankyou too for explanation.
    A little different approach...........

    1, take 3 bytes at a time (from low array) and place into a LONG, lower 3 bytes, (zero high byte)
    2, left-shift 6 bits, (puts next 6 bits alone in highbyte) place highbyte into array 1/2 way up, clear highbyte, shift again.........
    for a total of 4 times (unpacks 3 base64 packed bytes to 4 bytes as you layed out)
    3, load next 3 bytes to long etc........................up to end

    not sure about padding at the part to have the correct unpacked string.

    Don

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Problem with bits in byte array....

    Hi Don,
    Thank you but I'm doing my very best to avoid LONGs, the code produced by the long version of the compiler is much bigger than the normal even if you're not actually using a single LONG variable. So, I will do my very best to avoid using LONGs as LONG as I can ;-)

    It's not that it can't be done in another way, I had just settled on this and it had me cornered for a while. Now I know what the problem is and I'll aproach it from another angle. Had I bothered to look further back in the manual than the section on arrays I would've found this:
    7.6.4 Accessing Arrays as Multiple Variable-Types
    It is possible for a single array to be declared with multiple names and for each name to be assigned a different variable type. Note that offset for BITs are limited to a maximum value of 255
    To me, that statement is a little vague... I'm indexing a BYTE-array but on a bit basis so the above may apply (apparently) but it DOES work when "manually" indexing it:
    Code:
    Buffer VAR BYTE[64]
    Index VAR WORD
    
    Index=256
    Buffer.0[256] = 1  'Works
    Buffer.0[Index] = 0  ' Doesn't work
    Time to rethink this.

    /Henrik.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Problem with bits in byte array....

    OK then, good info,
    I know you can create any of this, but trying to be helpfull,
    THIS.....
    -manipulates 2 words to unpack (thats what VB calls it)
    3 bytes from low array to 4 bytes of 6 bit each in
    other array , needs indexing to move to next 3 and 4 locations
    -The mid byte of the 24 bit Base64 is in W1 and W2 since it is used in destination byte 2 and 3
    -only 140 bytes as is to do 24bit at a time.


    Code:
    arr1 var byte[8]   'base64 packed array or part of array
    arr2 var byte[12]  ' array to unpack into
    w1 var word
    w2 var word
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
     
          
    w1= arr1[1]
    w2= arr1[2]
    w1= w1 >> 2
    arr2[1]=w1.byte0 & 63
    w1=w1 >> 2
    arr2[2]= w1.byte1 & 63
    arr2[4]=w2.byte1 & 63
    w2=w2 << 2
    arr2[3]= w2.byte0 & 63
    Don

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Problem with bits in byte array....

    Thank you Don,
    Your help is much appreciated, I hope it doesn't look or feel that it's not!

    I'll give that code a try as it does look tighter than the one I'm currently trying in my effort to avoid the bit-indexing.

    /Henrik.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Problem with bits in byte array....

    A visual of the routine.......

    Name:  Capture20.JPG
Views: 1105
Size:  58.2 KB

    Don

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Problem with bits in byte array....

    Hi Don,
    Thanks for that. I've tried to figure your code out without much luck, that visual representation helps me understand how you intend it to work. First I got thrown off by the arrays not being indexed from 0 and I thoght that was by intention (some trick) to make it work but now I'm not sure and I have to ask if you tried it?

    If I load arr1 with 77,97,110 I expect to get 19,22,5,46 (taken from Wikipedias example) but instead I get 19,0,4,0.

    The reason, I think, is that you're trying to load two bytes from the array into one word in one go with w1=arr1[1] etc.

    /Henrik.

    EDIT: It's not near as small as yours but this works for me:
    Code:
        Out[0] = ((In[0] >> 2) & 63)
        Out[1] = ((In[0] & 3) << 4) + ((In[1] & 240) >> 4) & 63
        Out[2] = ((In[1] & 15) << 2) + ((In[2] & 192) >> 6) & 63
        Out[3] = (In[2] & 63)
    Last edited by HenrikOlsson; - 18th October 2011 at 20:47.

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: Problem with bits in byte array....

    Henrik,
    Yes, I mistakingly was thinking that some 'W=' takes the two consecutive bytes. Also array[0] and [1] confuses me at times with for-next etc. so I just consider array start at [1].
    What you coded is good. The top and bottom (out[0] and out[3]) are simple but the ones where the 6 bit straddle 2 bytes take a little more. I tried to follow your calc on out[2] but my brain started smoking. Just wondering if '+' changes the value as opposed to the '>>' and '<<' and &. But, if it works, never argue with success.
    Anyway, thats all a relatively small simple code.
    Don

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts