Link to home
Start Free TrialLog in
Avatar of calis
calis

asked on

Bytes conversion to string

Using an API Function and being returned array containing bytes

Example:

Expression      Value
szsize(0)        126
szsize(1)        88
szsize(2)        68
szsize(3)        65
szsize(4)        86
szsize(5)        48
szsize(6)        48
szsize(7)        50
szsize(8)        0

need to convert this into a string is there a function or if not an easy way to do this.
ASKER CERTIFIED SOLUTION
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the 0 represents end of c type string, so maybe you coulds use

sString=""
i = 0
while szsize(i) <> 0
 sString = sString & chr(szsize(0))
 i = i + 1
wend
oops!!

sString=""
                      i = 0
                      while szsize(i) <> 0
                       sString = sString & chr(szsize(i))
                       i = i + 1
                      wend


                       
Avatar of caraf_g
caraf_g

Quicker....

Dim arrDbl() As byte

ReDim arrDbl(2*LBound(szsize) to 2*UBound(szsize))

Dim lngCounter as long

for lngCounter = lbound(szsize) to ubound(szsize)
    arrDbl(2*lngcounter) = szsize(lngcounter)
next

Dim strResult as string
strResult = arrDbl
(just checking)
Option Explicit

Private Sub Command1_Click()

Dim arr1() As Byte

ReDim arr1(0 To 5)
arr1(0) = Asc("H")
arr1(1) = Asc("e")
arr1(2) = Asc("l")
arr1(3) = Asc("l")
arr1(4) = Asc("o")
arr1(5) = 0

Dim arr2() As Byte
ReDim arr2(2 * LBound(arr1) To 2 * UBound(arr1))

Dim lngCounter As Long

For lngCounter = LBound(arr1) To UBound(arr1)
    arr2(2 * lngCounter) = arr1(lngCounter)
Next

Dim strResult As String

strResult = arr2

MsgBox strResult
MsgBox Len(strResult)

End Sub



Yep, it works..
Dim cArray(260) As Byte
Dim sString As String
Dim lVal As Long
lVal = SendMessage(Me.hwnd, WM_GETTEXT, 255&, cArray(0))

sString = StrConv(cArray, vbUnicode)
sString = Left(sString, lVal)
Debug.Print sString
Or:

sString = StrConv(cArray, vbUnicode)
sString = Left(sString, InStr(1, sString, vbNullChar) - 1)
calis,

Go with Erick37's implementation of StrConv... Much quicker!

Cheers!
Never even thought of that one <doh>
caraf_g, alot of people forget about the string conversion and subclassing routines...


Cheers!