Converting a byte array to a string 
Author Message
 Converting a byte array to a string

I saved a plain text file as a custom resource in a resource file that I
want to be compiled with my application. However, the only way that I can
find to retrieve this text is to use the LoadResData function, which returns
a byte array. However, I want the final data to be in a string. I came up
with the following code, except it processes exponentially slower as I
increase the size of text, to the point of an unacceptable speed (4 min for
a 100 KB plain text file). What is the best and fastest way for me to
convert the byte array to a string? Is there maybe a better way to handle
plain text and resource files? Here is my code so far:

    Dim i As Long
    Dim TempByte() As Byte 'Byte array
    Dim TempString As String 'Final string

    'Load data from byte array into string
    TempByte = LoadResData(101, "CUSTOM")
    For i = LBound(TempByte) To UBound(TempByte)
        TempString = TempString & Chr(TempByte(i))
    Next

Thanks in advance,

Jack



Sat, 20 Jul 2002 03:00:00 GMT  
 Converting a byte array to a string
Problem with your code is that it makes a new copy of your result string
each time you add another byte.  The memory chomp isn't much at first but as
you've seen it slows down with large messages.  The easiest way around this
is to create a result string of sufficient length up front.  Use the SPACE$
function with the Number Of Spaces = UBOUND() of the byte array.

Then loop through each element of the byte array moving its value into the
string using the MID$ function.  The code snip below should give you the
idea.  szResult is the resulting string your trying to create.  bArray is
the byte array you read from your resource file.  nLoop is an integer used
as a pointer into both the result string and the byte array.

    szResult = Space$(UBound(bArray))
    For nLoop = 1 To UBound(bArray)
        Mid$(szResult, nLoop, 1) = Chr$(bArray(nLoop))
    Next

Regards
Ian


Quote:
> I saved a plain text file as a custom resource in a resource file that I
> want to be compiled with my application. However, the only way that I can
> find to retrieve this text is to use the LoadResData function, which
returns
> a byte array. However, I want the final data to be in a string. I came up
> with the following code, except it processes exponentially slower as I
> increase the size of text, to the point of an unacceptable speed (4 min
for
> a 100 KB plain text file). What is the best and fastest way for me to
> convert the byte array to a string? Is there maybe a better way to handle
> plain text and resource files? Here is my code so far:

>     Dim i As Long
>     Dim TempByte() As Byte 'Byte array
>     Dim TempString As String 'Final string

>     'Load data from byte array into string
>     TempByte = LoadResData(101, "CUSTOM")
>     For i = LBound(TempByte) To UBound(TempByte)
>         TempString = TempString & Chr(TempByte(i))
>     Next

> Thanks in advance,

> Jack




Sat, 20 Jul 2002 03:00:00 GMT  
 Converting a byte array to a string
Jack

First, I have actually never used a resource file but I note in my VB Language
Reference that there is a LoadResString function as well as a LoadResData
function.  Can you not use this?

In any event, Ian Mooney gave you some good advice regarding the fastest way to
concatenate strings using the Mid statement (not, as he stated, the Mid
function, which is different).  However, if you do need now or later to convert
a byte array to a string there is an easier way:

Dim ab() as Byte
Dim S as String

' populate the byte array, and then...
' simply assign it to the string

S = ab

The only caveat concerns Unicode.  Regular VB text strings are Unicode, two
bytes per character, while your byte array may be ANSI, i.e one byte per
character.  From your code below I would assume that this is the case with the
byte array from your resource file.  If so, you would need to convert:

S = StrConv (ab, vbUnicode)  ' instead of S = ab

(Because of this, I would guess that the LoadResString function is essentially
the same as the LoadResData except that it does the Unicode conversion for you
automatically.)

Regards,

John...........

Quote:

> I saved a plain text file as a custom resource in a resource file that I
> want to be compiled with my application. However, the only way that I can
> find to retrieve this text is to use the LoadResData function, which returns
> a byte array. However, I want the final data to be in a string. I came up
> with the following code, except it processes exponentially slower as I
> increase the size of text, to the point of an unacceptable speed (4 min for
> a 100 KB plain text file). What is the best and fastest way for me to
> convert the byte array to a string? Is there maybe a better way to handle
> plain text and resource files? Here is my code so far:

>     Dim i As Long
>     Dim TempByte() As Byte 'Byte array
>     Dim TempString As String 'Final string

>     'Load data from byte array into string
>     TempByte = LoadResData(101, "CUSTOM")
>     For i = LBound(TempByte) To UBound(TempByte)
>         TempString = TempString & Chr(TempByte(i))
>     Next

> Thanks in advance,

> Jack




Sat, 20 Jul 2002 03:00:00 GMT  
 Converting a byte array to a string
The best and fastest way is:

    TempStr = StrConv(TempByte(), vbUnicode)

--
     Jim Mack
     MicroDexterity, Inc
     www.microdexterity.com


Quote:
> I saved a plain text file as a custom resource in a resource file that I
> want to be compiled with my application. However, the only way that I can
> find to retrieve this text is to use the LoadResData function, which
returns
> a byte array. However, I want the final data to be in a string. I came up
> with the following code, except it processes exponentially slower as I
> increase the size of text, to the point of an unacceptable speed (4 min
for
> a 100 KB plain text file). What is the best and fastest way for me to
> convert the byte array to a string? Is there maybe a better way to handle
> plain text and resource files? Here is my code so far:

>     Dim i As Long
>     Dim TempByte() As Byte 'Byte array
>     Dim TempString As String 'Final string

>     'Load data from byte array into string
>     TempByte = LoadResData(101, "CUSTOM")
>     For i = LBound(TempByte) To UBound(TempByte)
>         TempString = TempString & Chr(TempByte(i))
>     Next

> Thanks in advance,

> Jack




Sat, 20 Jul 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. StrConv function - Converting a byte array to a string

2. HELP: How to convert a byte array into a string, and vice versa

3. Converting string array to byte array

4. Need to convert a 16 byte array of bytes to 4 byte integer

5. Converting from string to array of bytes

6. Convert integers and strings to byte array

7. Convert Byte Array to a String

8. Converting between String and Byte Array

9. convert Byte array to string and vice veras

10. Convert Strings to Byte arrays and back

11. Convert a string to byte array

12. Convert an array of bytes to a string

 

 
Powered by phpBB® Forum Software