[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.
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.
Re: Converting a string to byte array and back
vb Code:
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
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.