Results 1 to 4 of 4

Thread: [RESOLVED]Converting a string to byte array and back

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    50

    Resolved [RESOLVED]Converting a string to byte array and back

    I need something to convert a string into a byte array and back. I've tried:

    bytData() = strData
    bytData() = StrConv(strData, vbFromUnicode)

    The problem is, there are all these 0 value bytes in between each byte. I have tried reading every second byte, but that doesn't always work when converting it back, after modifying it that is. I need this because it is a lot faster than doing this looped 50000 times:

    strData = strData & CTbl(Temp)

    I need a way to speed up my loop, because the code above can take several seconds to complete, and that array takes about 50 milliseconds, if only I can get the byte array right.
    Last edited by evan29; May 11th, 2007 at 07:09 AM.

  2. #2
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Re: Converting a string to byte array and back

    There are several methods for doing so, for example you can use a for loop, together with Asc(Mid()) for the string to byte array conversion, and a for loop and a simple string = string + Cstr(byteArr(i)) for the other.

    EDIT: Didn't notice you are talking about UniCode. Maybe the easiest way in this case would be to use the CopyMemory API.
    Last edited by Neverbirth; May 11th, 2007 at 07:02 AM.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Converting a string to byte array and back

    vb Code:
    1. bytdata = StrConv(strdata, vbFromUnicode)
    this will remove all the zero values from you byte array, but any unicode characters (that use the second byte for each character) will be lost, i have no idea if this will fix the problems you have, you would also need to convert the string you are appending to to it each time, so i don't know that it would gain you any speed, with all the conversions

    you will also need to convert to change back to a regular string
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    50

    Re: Converting a string to byte array and back

    That is a very slow method, but I have found the right answer already...
    I turned it into an array by:

    bytData() = StrConv(strData, vbFromUnicode)

    and converted it back by:

    Encrypt = StrConv(bytData, vbUnicode)

    Now my program works fine, and the byte array is about 10 times faster, especially for really long strings, it has a greatly improved speed.

    I don't need Unicode, because this is changing byte data, not text. The characters have values from 0 to 255. VB strings are stored as Unicode, and when converted to bytes, it comes along with the Unicode.
    Last edited by evan29; May 11th, 2007 at 07:22 AM.

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