Results 1 to 9 of 9

Thread: [RESOLVED] Compiling byte array

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    294

    Resolved [RESOLVED] Compiling byte array

    I want to compile a byte array (0 To 255) of numbers from 0-255, something like

    b(0) = 12
    b(1) = 18
    b(2) = 24
    b(3) = 8
    b(4) = 45
    b(5) = 23
    b(6) = 67
    b(7) = 1
    ...

    But I can't seem to do it, ideas?

  2. #2
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Compiling byte array

    whats the problem?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    294

    Re: Compiling byte array

    I suck at explaining :P

    I need a loop that will fill a byte array full of numbers from 0 to 255 in a random order.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    294

    Re: Compiling byte array

    This is what I was tring to do:
    VB Code:
    1. Option Explicit
    2. Private Sub CompileByteArrays()
    3.     Dim sNumbers As String
    4.     Dim iLoop As Integer
    5.     Dim iRand As Integer
    6.     Dim b1(0 To 255) As Byte
    7.     Dim b2(0 To 255) As Byte
    8.     For iLoop = 0 To 5
    9.         sNumbers = sNumbers & ";" & iLoop
    10.     Next iLoop
    11.     sNumbers = Mid$(sNumbers, 2)
    12.     For iLoop = 0 To 5
    13.         iRand = Int(NumTok(sNumbers, 59) * Rnd) + 1
    14.         b1(iLoop) = GetTok(sNumbers, iRand, 59)
    15.         b2(GetTok(sNumbers, iRand, 59)) = iLoop
    16.         sNumbers = RemTok(sNumbers, iRand, 59)
    17.     Next iLoop
    18.     Text1.Text = ""
    19.     Text2.Text = ""
    20.     For iLoop = 0 To 5
    21.         Text1.Text = Text1.Text & "b(" & iLoop & ") = " & b1(iLoop) & vbCrLf
    22.         Text2.Text = Text2.Text & "b(" & iLoop & ") = " & b2(iLoop) & vbCrLf
    23.     Next
    24. End Sub
    Compiles two "floped" byte arrays:
    Code:
    b(0) = 3
    b(1) = 4
    b(2) = 0
    b(3) = 2
    b(4) = 1
    b(5) = 5
    Code:
    b(0) = 2
    b(1) = 4
    b(2) = 3
    b(3) = 0
    b(4) = 1
    b(5) = 5
    Last edited by frozen; Apr 14th, 2006 at 05:56 AM.

  5. #5
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Compiling byte array

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim b1(0 To 255) As Byte
    4. Dim b2(0 To 255) As Byte
    5. Dim i            As Integer
    6.     Randomize
    7.     For i = 0 To 255
    8.         b1(i) = Rnd * 255
    9.         b2(i) = Rnd * 255
    10.         Debug.Print "b1(" & i & ")=  " & b1(i) & "   b2(" & i & ")=  " & b2(i)
    11.     Next i
    12.  
    13. End Sub

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    294

    Re: Compiling byte array

    The number can only be in the byte array once.
    What I was trying to do was make a program to compile a random byte array to remap the ASCII values of characters in a string. The sub I wrote compiles bEncodeMap() and bDecodeMap() for the following functions.
    VB Code:
    1. Public Function bEncode(ByRef s As String) As String
    2.     Dim i As Integer
    3.     bEncode = ""
    4.     For i = 1 To Len(s)
    5.         bEncode = bEncode & Chr(bEncodeMap(Asc(Mid$(s, i, 1))))
    6.     Next i
    7. End Function
    8.  
    9. Public Function bDecode(ByRef s As String) As String
    10.     Dim i As Integer
    11.     bDecode = ""
    12.     For i = 1 To Len(s)
    13.         bDecode = bDecode & Chr(bDecodeMap(Asc(Mid$(s, i, 1))))
    14.     Next i
    15. End Function

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Compiling byte array

    Instead of adding them from index 0 to 255 in random order, try creating a sorted array first then call a randomizer procedure which will swap the values contained in 2 randomly selected indices (from 0 to 255, the 2 generated indices must not be the same). Swap positions of array member pairs n number of times... make it a large num, you would end up doing around 255 factorial comparisons if you approached it as inserting random numbers from position 0 to 255 anyway.
    Last edited by leinad31; Apr 14th, 2006 at 07:35 AM.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    294

    Re: Compiling byte array

    huh? lol.

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Compiling byte array

    Quote Originally Posted by frozen
    huh? lol.
    Pls be more specific. Which part did you not understand?

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