Problem converting byte() to string and then back to byte() in VB SCRIPT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arunfr
    New Member
    • Feb 2007
    • 3

    Problem converting byte() to string and then back to byte() in VB SCRIPT

    Hi ,
    I have tried all the methods available to convert byte () to string and vice versa in VBSCRIPT.

    Code:
    Function ByteArray2Text(varByteArray)
    Dim rs
    Const adLongVarChar = 201
    Set rs = CreateObject("ADODB.Recordset") 
    rs.Fields.Append "temp", adLongVarChar, LenB(varByteArray)
    rs.Open
    rs.AddNew
    rs("temp").AppendChunk varByteArray
    rs.Update    ByteArray2Text = rs("temp")
    rs.Close
    Set rs = Nothing
    End Function
    
    
    Function ByteArray2Text(varByteArray)
    Dim byt
    Const adTypeText = 2
    Const adTypeBinary = 1
    Set byt = CreateObject("ADODB.Stream")
    byt.Type = adTypeBinary
    byt.Open 
    byt.Write varByteArray
    byt.Position = 0
    byt.Type = adTypeText
    byt.CharSet = "us-ascii" 
    ByteArray2Text = byt.ReadText 
    byt.Close    Set byt = Nothing
    End Function
    
    
    Function ByteArray2Text(varByteArray)   
     'Convert byte array into a string with VBScript   
     '60kb = 2 seconds, 100kb = 5 seconds, 200kb = 25 seconds    
    Dim strBuffer, lngCounter    
    strData = ""   
     strBuffer = ""    
    For lngCounter = 0 to UBound(varByteArray) 
           strBuffer = strBuffer & Chr(255 And Ascb(Midb(varByteArray,lngCounter + 1, 1)))
            'Keep strBuffer at 1k bytes maximum
            If lngCounter Mod 1000 = 0 Then
                strData = strData & strBuffer
                strBuffer = ""
            End If
        Next    ByteArray2Text = strData & strBuffer
    End Function
    these are some of the methods i tried..
    In all these methods i get microsoft V B script runtime error - Object doesnt support this type or method. some body please help me out
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Hi. Would it be possible to say what it is your are trying to achieve with this conversion?

    Comment

    • arunfr
      New Member
      • Feb 2007
      • 3

      #3
      i would like to get some help to convert byte array to string and vice versa..

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by arunfr
        i would like to get some help to convert byte array to string and vice versa..
        Thanks, I understand that part. What I wanted to know was why you are performing this conversion.

        Comment

        • arunfr
          New Member
          • Feb 2007
          • 3

          #5
          Originally posted by willakawill
          Thanks, I understand that part. What I wanted to know was why you are performing this conversion.
          i have to convert the string to bytearray n compare it with the bytearray in the binary file..similarly i have to convert the byte array in the binary file to string and perform comparison with the original string value of that byte array. i m able to read from binary file n the only problem i face is i m not able to perform this conversion.

          For eg:
          string : The Scripts - I must convert this to byte array n if try to convert this byte array back to string i must get the original string . ie (The Scripts) similarly for byte array...

          Comment

          Working...