Results 1 to 2 of 2

Thread: Packing Bits into Bytes

  1. #1

    Thread Starter
    Fanatic Member siyan's Avatar
    Join Date
    Jul 2001
    Location
    GOOOAAAAALLLLL!!!!!
    Posts
    869

    Packing Bits into Bytes

    I have a 192x192 element array of booleans, and I need to store it to a file. I want to pack each 8 bits into one byte for speed and file size reasons. I have something that sort of works but I don't think its doing it quite right....

    VB Code:
    1. Dim I, J as Integer
    2.         Dim NextByte As Byte
    3.  
    4.      
    5.         Dim Writer As New IO.StreamWriter(IO.File.Create("c:\asdf\" & Letter & ".dat"), System.Text.Encoding.ASCII)
    6.  
    7.         For I = 0 To 191
    8.             For J = 0 To 23
    9.                 If BitBoard(J * 8, I) = True Then NextByte = NextByte Or 128 'flip bit 1
    10.                 If BitBoard(J * 8 + 1, I) = True Then NextByte = NextByte Or 64 'flip bit 2
    11.                 If BitBoard(J * 8 + 2, I) = True Then NextByte = NextByte Or 32 'flip bit 3
    12.                 If BitBoard(J * 8 + 3, I) = True Then NextByte = NextByte Or 16 'flip bit 4
    13.                 If BitBoard(J * 8 + 4, I) = True Then NextByte = NextByte Or 8 'flip bit 5
    14.                 If BitBoard(J * 8 + 5, I) = True Then NextByte = NextByte Or 4 'flip bit 6
    15.                 If BitBoard(J * 8 + 6, I) = True Then NextByte = NextByte Or 2 'flip bit 7
    16.                 If BitBoard(J * 8 + 7, I) = True Then NextByte = NextByte Or 1 'flip bit 8
    17.  
    18.                 Writer.Write(Chr(NextByte))
    19.                 NextByte = NextByte AndAlso 0
    20.  
    21.             Next
    22.         Next
    23.         Writer.Close()

    And to unpack it....

    VB Code:
    1. Dim i, J As Integer
    2.         Dim NextChar As Byte
    3.  
    4.         Dim reader As New IO.StreamReader(IO.File.Open("c:\asdf\hello.dat", IO.FileMode.Open))
    5.  
    6.         For i = 0 To 191
    7.             For J = 0 To 23
    8.                 NextChar = reader.Read()
    9.                 If NextChar And 128 Then SampleImage.SetPixel(J * 8, i, Color.Black)
    10.                 If NextChar And 64 Then SampleImage.SetPixel(J * 8 + 1, i, Color.Black)
    11.                 If NextChar And 32 Then SampleImage.SetPixel(J * 8 + 2, i, Color.Black)
    12.                 If NextChar And 16 Then SampleImage.SetPixel(J * 8 + 3, i, Color.Black)
    13.                 If NextChar And 8 Then SampleImage.SetPixel(J * 8 + 4, i, Color.Black)
    14.                 If NextChar And 4 Then SampleImage.SetPixel(J * 8 + 5, i, Color.Black)
    15.                 If NextChar And 2 Then SampleImage.SetPixel(J * 8 + 6, i, Color.Black)
    16.                 If NextChar And 1 Then SampleImage.SetPixel(J * 8 + 7, i, Color.Black)
    17.             Next
    18.             NextChar = NextChar AndAlso 0
    19.         Next
    SampleImage.SetPixel to flip make a pixel black....this is 1 bit color image storage.

    It sort of works but I get weird results...I think its something going on wiht my boolean logic (or lack thereof )

    Any ideas?

    TIA
    Unite, proletariat!

  2. #2

    Thread Starter
    Fanatic Member siyan's Avatar
    Join Date
    Jul 2001
    Location
    GOOOAAAAALLLLL!!!!!
    Posts
    869
    NM I got it....

    And ironically I don't even need it any more.

    -C
    Unite, proletariat!

Posting Permissions

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



Click Here to Expand Forum to Full Width