'______________________________________________[ header ]__ ' Cerro Scripts: OctetString.vbs ' ' Copyright (c) 2007 Philipp Föckeler (www.selfadsi.de) ' ' Collection of useful vbscript functions to handle LDAP ' attributes of typ "Octet-String" '_________________________________________________________________[ OctetToHexStr ]__________ 'Function to convert OctetString (byte array) to a hexadecimal string. ' Function OctetToHexStr(var_octet) Dim n OctetToHexStr = "" For n = 1 To lenb(var_octet) OctetToHexStr = OctetToHexStr & Right("0" & Hex(ascb(midb(var_octet, n, 1))), 2) Next End Function '_________________________________________________________________[ HexStrToOctet ]__________ 'Function to convert hex string to an OctetString (byte array). ' Function HexStrToOctet(var_hex) Dim fso, stream, temp, ts, n Set fso = CreateObject ("Scripting.Filesystemobject") Set stream = CreateObject ("ADOdb.Stream") temp = fso.gettempname () Set ts = fso.createtextfile(temp) For n = 1 To (Len(var_hex) - 1) Step 2 ts.write Chr("&H" & Mid(var_hex, n, 2)) Next ts.close stream.type = 1 stream.open stream.loadfromfile temp HexStrToOctet = stream.read stream.close fso.deletefile temp Set stream = Nothing Set fso = Nothing End function '_______________________________________________________________________________________ function HexStrToAscii __________ 'Function to convert hex string to ascii string. 'If "format" is TRUE, then tabs and crlfs will be inserted ' Function HexStrToAscii(var_hex, format) Dim k, v HexStrToAscii = "" For k = 1 To Len(var_hex) Step 2 v = CInt("&H" & Mid(var_hex, k, 2)) If ((v>31) And (v<128)) Then HexStrToAscii = HexStrToAscii & (chr(v)) Else If (format) Then Select Case v Case 8 HexStrToAscii = HexStrToAscii & vbTab Case 10 HexStrToAscii = HexStrToAscii & vbCrLf Case 13 Case Else HexStrToAscii = HexStrToAscii & "." End Select Else HexStrToAscii = HexStrToAscii & "." End If End If Next End Function '_______________________________________________________________________________________ function AsciiToHexStr __________ 'Function to convert an ascii to a string to hex string. ' Function AsciiToHexStr(var_string) Dim k AsciiToHexStr = "" For k = 1 To Len(var_string) AsciiToHexStr = AsciiToHexStr + Hex(Asc(Mid(var_string, k, 1))) Next End Function '_______________________________________________________________________________________ function PrintoutHex ___________ 'Returns an formatted print out of a hex string. Hex + Dec are displayed. 'The width parameter determines how many bytes are displayed per line. ' Function PrintoutHex(var_hex, width) Dim k1, k2, s1, s2 PrintOutHex = "" For k1 = 1 To Len(var_hex) Step (width *2) s1 = Mid(var_hex, k1, (width *2)) s2 = "" s3 = HexStrToAscii(s1, False, False) For k2 = 1 To Len(s1) Step 2 s2 = S2 & Mid(S1, k2, 2) & " " Next s2 = s2 & String((width *3)-Len(s2), " ") If (k1=1) Then PrintOutHex = PrintOutHex & s2 & "| " & s3 Else PrintOutHex = PrintOutHex & vbcrlf & s2 & "| " & s3 End If Next End Function