|
-
May 20th, 2003, 09:38 PM
#1
Thread Starter
Frenzied Member
Str -> ByteArray then back..
Hi,
VB Code:
Randomize Timer
Dim iCount As Integer, sData As String, sResult As String, bArray() As Byte
For iCount = 1 To 250
sData = sData & Chr$(Rnd * 254)
Next iCount
bArray = StrConv(sData, vbFromUnicode)
sResult = StrConv(bArray, vbUnicode)
MsgBox (sData = sResult) '=False
Knows somebody whats wrong with this one? why isn't the same? ..or how can i split a string into this byte array then back, to get the original string?
any help appreciated! :|
-
May 20th, 2003, 11:24 PM
#2
You are doing the conversion correctly and there doesn't seem to be anything wrong with your code. I get a True result everytime I run your routine.
What problem are you getting?
-
May 21st, 2003, 09:04 AM
#3
Thread Starter
Frenzied Member
Originally posted by brucevde
You are doing the conversion correctly and there doesn't seem to be anything wrong with your code. I get a True result everytime I run your routine.
What problem are you getting?
uhh.. that's incredible..
i've got FALSE. is there any other way to push this string into a byte array? i've tried with PropertyBag but i've got the PropertyBags Header before the string..
so.. is there any other way?
Jim.
-
May 21st, 2003, 09:41 AM
#4
Frenzied Member
I got True also.
In addition, sData was 250 (as expected) and UBound(bArray) was 249. Since the local affects the code page, could that have anything to do with the problem?
From MSDN:
Code Page
A character set that a computer uses to interpret and display data properly. Code pages usually correspond to different platforms and languages and are used in international applications.
and:
Remarks
When you're converting from a Byte array in ANSI format to a string, you should use the StrConv function. When you're converting from such an array in Unicode format, use an assignment statement.
May I ask what, specifically, you are trying to do with the conversions? Depending on what you want to see, there may be another method.
-
May 21st, 2003, 10:27 AM
#5
Thread Starter
Frenzied Member
In addition, sData was 250 (as expected) and UBound(bArray) was 249.
yes, because bArray's elements are between 0 and 249..
May I ask what, specifically, you are trying to do with the conversions? Depending on what you want to see, there may be another method.
I have a large String (180kbyte), it contains a lots of Position parameters, and i have to send it via winsock. But i dont know how can i split this string into this byte array without any character-page conversions..
at this time, i'll try to rewrite the position-storing routine, to store these parameters directly into a byte array instead of string.. but i have to join this array on the client side before starting to process the values..
VB Code:
Randomize Timer
Dim iCount As Integer, sData As String, sResult As String, bArray() As Byte, iPos As Integer
ReDim bArray(250)
For iCount = 1 To 250
bArray(iCount) = CByte(Rnd * 254)
Next iCount
Open App.Path + "\1.x" For Binary As #1
Put #1, , bArray
Close #1
sResult = StrConv(bArray, vbUnicode)
Open App.Path + "\2.x" For Binary As #1
Put #1, , sResult
Close #1
so its look like this at this time, but the elements of bArray and sResult still isn't the same..
so at this point, how can i join this array without using conversions?
sorry for my poor english, but i hope you can understand me.
Regards,
Jim
-
May 21st, 2003, 11:45 AM
#6
Thread Starter
Frenzied Member
i'm sorry guys for abusing you with this creepy code, but here is it what i've did to join a byte array:
VB Code:
Function JoinArray(Byte_Array() As Byte) As String
On Error GoTo JoinThis
Dim lCount As Long, lCount2 As Long, lCount3 As Long
Dim sPartial() As String
ReDim sPartial(Fix(UBound(Byte_Array) / 256) + 1)
For lCount = 0 To UBound(Byte_Array) Step 256
lCount3 = lCount3 + 1
For lCount2 = lCount To lCount + 255
sPartial(lCount3) = sPartial(lCount3) + Chr$(Byte_Array(lCount2))
Next lCount2
Next lCount
JoinThis:
ReDim Preserve sPartial(lCount3)
JoinArray = Join(sPartial, "")
End Function
also i've tried the simple way but thats horrible slow, but here is it:
(test the both code with an array has elements up to 100,000.)
VB Code:
Function JoinArray_2(Byte_Array() As Byte) As String
Dim lCount As Long, sTemp As String
For lCount = 0 To UBound(Byte_Array)
sTemp = sTemp + Chr$(Byte_Array(lCount))
Next lCount
JoinArray_2 = sTemp
End Function
so.. is there any Expert way to join this st*pid array, without any conversions?
Thanks,
Jim
-
May 21st, 2003, 12:36 PM
#7
Thread Starter
Frenzied Member
VB Code:
Private Declare Sub CopyMemoryString Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Any, Source As Any, ByVal Length As Long)
'....
sResult = String(UBound(bArray) + 1, Chr$(0))
Call CopyMemoryString(sResult, bArray(0), Len(sResult))
Yeah.. finally, this one is really fast, and it works fine.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|