Convert Binary to String

前端 未结 4 1680
情话喂你
情话喂你 2021-01-03 16:57

I want to convert a password which is stored in binary to normal ASCII form so that i can read it. I need a VBscript for that and script should also return this de-crypted p

相关标签:
4条回答
  • 2021-01-03 17:10

    try this code ;)

    the code :

    function BinaryToString(bin)  
     dim next_char 
     dim result
     dim i
     dim ascii
     For i = 1 To Len(bin) + 18 Step 8
            next_char = Mid(bin, i, 8)
            ascii = BinaryToLong(next_char)
            result = result & Chr(ascii)
        Next 
        BinaryToString=result
    end function  
    
    Function BinaryToLong(binary_value)
    Dim hex_result 
    Dim nibble_num 
    Dim nibble_value
    Dim factor 
    Dim bit 
    
        binary_value = UCase(Trim(binary_value))
        If Left(binary_value, 2) = "&B" Then
            binary_value = Mid(binary_value, 3)
        End If
    
        binary_value = Replace(binary_value, " ", "")
        binary_value = Right(String(32, "0") & binary_value, 32)
    
        For nibble_num = 7 To 0 Step -1
            factor = 1
            nibble_value = 0
    
            For bit = 3 To 0 Step -1
                If Mid(binary_value,1 + nibble_num * 4 + bit, 1) = "1"  Then
                    nibble_value = nibble_value + factor
                End If
                factor = factor * 2
            Next 'bit
    
            hex_result = Hex(nibble_value) & hex_result
        Next 'nibble_num
    
        BinaryToLong = CLng("&H" & hex_result)
    End Function
    

    usage:

    response.Write(BinaryToString("00110001001100100011001100110100"))
    

    don't forget to take off " " blank spaces from the binary string

    0 讨论(0)
  • 2021-01-03 17:11

    There are so many ways.

    If it's a binary reg value then from Help (you did read it, didn't you)

    The RegRead method returns values of the following five types.

    Type  Description  In the Form of  
    REG_SZ
     A string
     A string
    
    REG_DWORD
     A number
     An integer
    
    REG_BINARY
     A binary value
     A VBArray of integers
    
    REG_EXPAND_SZ
     An expandable string (e.g., "%windir%\\calc.exe")
     A string
    
    REG_MULTI_SZ
     An array of strings
     A VBArray of strings
    

    If a string, split on space (gives you an array of strings). The least significant bit is 2^0, 2^1, ..., 2^7.


    EDIT


    The normal way, not the only way though, to store a password, is to dump it in the registry.

    Reading it gives you an array, not a scalar variable. So ...

    The second method, handles cases where it's stored in a file.

    0 讨论(0)
  • 2021-01-03 17:21

    If you are dealing with a byte array, you must know the character encoding before you can convert it to string. Without that knowledge the bytes will be converted to the wrong characters.

    The ADODB.Stream object can handle byte arrays. Here is a function that that does that:

    Const adTypeBinary = 1
    Const adTypeText = 2
    Const adModeReadWrite = 3
    
    Function BytesToString(bytes, charset)
        With CreateObject("ADODB.Stream")
            .Mode = adModeReadWrite
            .Type = adTypeBinary
            .Open
            .Write bytes
            .Position = 0
            .Type = adTypeText
            .Charset = charset
            BytesToString = .ReadText
        End With
    End Function
    

    And here is how to use it:

    MsgBox BytesToString(binary, "Windows-1252")
    

    For the sake of completeness, this is the reverse operation:

    Function StringToBytes(str, charset)
        With CreateObject("ADODB.Stream")
            .Mode = adModeReadWrite
            .Type = adTypeText
            .Charset = charset
            .Open
            .WriteText str
            .Position = 0
            .Type = adTypeBinary
            StringToBytes = .Read
        End With
    End Function
    

    Since your input seems to be a string like "00110001 00110010 00110011 00110100", here is a function to convert that to a byte array, which you can then use with BytesToString() shown above:

    Function BinaryStringToBytes(binaryStr)
        Dim b, n, i, l
    
        l = GetLocale
        SetLocale 1031
    
        With CreateObject("ADODB.Stream")
            .Mode = adModeReadWrite
            .Charset = "Windows-1252"
            .Type = adTypeText
            .Open
            For Each b In Split(binaryStr, " ")
                If Len(b) <> 8 Or Replace(Replace(b, "0", ""), "1", "") <> "" Then
                    ' invalid procedure call or argument
                    Err.Raise 5, "BinaryStringToBytes", _
                        "Only stings of 8-blocks of 0s and 1s, " & _
                        "separated by a single space are accepted."
                End If
                n = 0
                For i = 0 To 7
                    n = n + Mid(b, 8 - i, 1) * 2^i
                Next
                .WriteText Chr(n)
            Next
            .Position = 0
            .Type = adTypeBinary
            BinaryStringToBytes = .Read
        End With
    
        SetLocale l
    End Function
    

    Usage

    Dim input, output
    
    input = "00110001 00110010 00110011 00110100"
    output = BytesToString(BinaryStringToBytes(input), "Windows-1252")
    
    MsgBox output  ' -> "1234"
    

    And, more importantly, it can handle multi-byte encodings properly:

    input = "00110001 00110010 00110011 00110100 11000011 10100100"
    output = BytesToString(BinaryStringToBytes(input), "UTF-8")
    
    MsgBox output ' -> "1234ä"
    
    0 讨论(0)
  • 2021-01-03 17:28

    If I'm right, all you're after is converting a binary number to decimal (eg 0100 -> 4)?

    dim binary, n, s
    binary= "00110001"
    
    For s = 1 To Len(binary)
        n = n + (Mid(binary, Len(binary) - s + 1, 1) * (2 ^ (s - 1)))
    Next 's
    
    WScript.Echo binary & " = " & n
    

    outputs

    00110001 = 49

    Converted from here: http://www.vb-helper.com/howto_decimal_to_binary.html

    0 讨论(0)
提交回复
热议问题