Results 1 to 7 of 7

Thread: Str -> ByteArray then back..

  1. #1

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Str -> ByteArray then back..

    Hi,

    VB Code:
    1. Randomize Timer
    2.     Dim iCount As Integer, sData As String, sResult As String, bArray() As Byte
    3.     For iCount = 1 To 250
    4.         sData = sData & Chr$(Rnd * 254)
    5.     Next iCount
    6.  
    7.     bArray = StrConv(sData, vbFromUnicode)
    8.    
    9.     sResult = StrConv(bArray, vbUnicode)
    10.    
    11.     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! :|

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    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?

  3. #3

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284
    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.

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    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.

  5. #5

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284
    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:
    1. Randomize Timer
    2.     Dim iCount As Integer, sData As String, sResult As String, bArray() As Byte, iPos As Integer
    3.     ReDim bArray(250)
    4.    
    5.     For iCount = 1 To 250
    6.         bArray(iCount) = CByte(Rnd * 254)
    7.     Next iCount
    8.  
    9.     Open App.Path + "\1.x" For Binary As #1
    10.         Put #1, , bArray
    11.     Close #1
    12.  
    13.     sResult = StrConv(bArray, vbUnicode)
    14.  
    15.     Open App.Path + "\2.x" For Binary As #1
    16.         Put #1, , sResult
    17.     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

  6. #6

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284
    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:
    1. Function JoinArray(Byte_Array() As Byte) As String
    2. On Error GoTo JoinThis
    3.     Dim lCount As Long, lCount2 As Long, lCount3 As Long
    4.  
    5.     Dim sPartial() As String
    6.     ReDim sPartial(Fix(UBound(Byte_Array) / 256) + 1)
    7.  
    8.         For lCount = 0 To UBound(Byte_Array) Step 256
    9.             lCount3 = lCount3 + 1
    10.             For lCount2 = lCount To lCount + 255
    11.             sPartial(lCount3) = sPartial(lCount3) + Chr$(Byte_Array(lCount2))
    12.             Next lCount2
    13.         Next lCount
    14. JoinThis:
    15.     ReDim Preserve sPartial(lCount3)
    16.     JoinArray = Join(sPartial, "")
    17. 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:
    1. Function JoinArray_2(Byte_Array() As Byte) As String
    2.     Dim lCount As Long, sTemp As String
    3.         For lCount = 0 To UBound(Byte_Array)
    4.             sTemp = sTemp + Chr$(Byte_Array(lCount))
    5.         Next lCount
    6.     JoinArray_2 = sTemp
    7. End Function

    so.. is there any Expert way to join this st*pid array, without any conversions?

    Thanks,
    Jim

  7. #7

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284
    VB Code:
    1. Private Declare Sub CopyMemoryString Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Any, Source As Any, ByVal Length As Long)
    2.  
    3.  
    4. '....
    5.  
    6. sResult = String(UBound(bArray) + 1, Chr$(0))
    7. 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
  •  



Click Here to Expand Forum to Full Width