Results 1 to 5 of 5

Thread: convert string to byte array

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    7

    convert string to byte array

    hi guys,
    is it possible to convert a string to a byte array?
    if its possible how can it be done?
    thanks and pardon me for my newbie question.

  2. #2
    Hyperactive Member
    Join Date
    Feb 2003
    Location
    Grenada
    Posts
    346
    Here it is... I assume that you know how to handle arrays, so here's the shortest version possible...
    Put the following code in a module or a form or what not

    VB Code:
    1. Public Function GetByteArray(TextStr As String) As Variant
    2. Dim ByteArray() As Byte
    3. For xdat = 1 To Len(TextStr)
    4. ReDim Preserve ByteArray(xdat - 1)
    5. ByteArray(xdat - 1) = CByte(Asc(Mid$(TextStr, xdat, 1)))
    6. Next xdat
    7. GetByteArray = ByteArray
    8. End Function
    Now you call the above module by the following lines of code...

    VB Code:
    1. Private Sub Form_Load()
    2. Dim tBytes() As Byte
    3. tBytes() = GetByteArray("Testing this array")
    4. End Sub
    Now each character is held in the array with the 1st being in tBytes(0) and the second in tBytes(1) and so on...

    NB... declaring the GetByteArray function as a Variant is simply because you can't pass array variables in functions as returned values... so you can only decare the function as a Variant and set it to the ByteArray variable array...

    If you have anymore questions then post them aight....

    Hope this helped you out.
    If my post has been helpful, then please rate it accordingly...
    If it has solved your question(s), then don't forget to mark the thread as "[Resolved]"... thank you.

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    VB has it's own string conversion function....
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim tBytes() As Byte
    3.     tBytes() = StrConv("Testing this array", vbFromUnicode)
    4. End Sub

  4. #4
    Hyperactive Member
    Join Date
    Feb 2003
    Location
    Grenada
    Posts
    346
    damn!!

    So short and precise...

    Tanx for the compression CV, I prefer your own over mines...lol

    And they work the same way too...
    If my post has been helpful, then please rate it accordingly...
    If it has solved your question(s), then don't forget to mark the thread as "[Resolved]"... thank you.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    7
    hey guys reallly thanks alot.....got to try them out later......hmmm.....i really got a long way to go......thanks!

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