Problem with bits in byte array....


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,621


    Did you find this post helpful? Yes | No

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

    Thanks Don,
    No, both [] and () should work as far as I know. I tried and it didn't make a difference.

    The problem definitely is in the bit-indexing not allowing me to index above 255....

    Take a look at this example:
    Code:
    Buffer VAR BYTE[256]
    Index VAR WORD
    
    CLEAR
    
    HSEROUT["Buffer[32]: ", DEC Buffer[32],13]
    
    Buffer.0[256] = 1        ' Set LSB of Buffer[32]
    HSEROUT["Buffer[32]: ", DEC Buffer[32],13]
    
    Index = 256
    Buffer.0[Index] = 0     ' Clear LSB of Buffer[32]
    HSEROUT["Buffer[32]: ", DEC Buffer[32],13]
    
    Pause 100
    END
    It gives the following results:

    Name:  Byte_array_problem.jpg
Views: 2095
Size:  25.5 KB

    As you can see, direct/literal indexing the 256'th bit works but doing it thru a variable doesn't. In Melanies example (which may be in error) she states that it should be possible to index bits above 255 this way (as long as the indexing variables is WORD, which it is in my case).

    I have no problem index bytes (as bytes) above 255, it's just this bit-indexing that is screwing with me.

    /Henrik.

  2. #2


    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

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,621


    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.

  4. #4


    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

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,621


    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.

  6. #6


    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: 2170
Size:  58.2 KB

    Don

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,621


    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.

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