How to move from 8-bit ascii binaries to 6-bit ascii binaries?

1

How to move from 8-bit ascii binaries to 6-bit ascii binaries?

For example, is there a link between H in 8-bit ASCII 0100 1000 and H in 6-bit ASCII 011000 ?

I am referring to the article by Eric S. Raymond that was recently edited twice on the conversion tables between the ASCII and the binary. Here is how to recover the 6-bits. But not the binary:

  

To recover the six bits, subtract 48 from the ASCII character value; if the result is greater than 40 subtract 8.

if that is very clear for decimals

Char ASCII Decimal Bits

"0"  48    0       000000 
"1"  49    1       000001
"2"  50    2       000010
"3"  51    3       000011
 ...

It is not for the letters

"H"  72    24      011000

I have made the following code to recover the binaries associated with the ascii symbols. But if I get 000001 for 1 in ASCII, which is successful, I get 1000101 for the letter u instead of 111101

Module Module1

    Public Function traduction_ASCII(trames As String)

        Dim binario_en_8 As Integer = 0
        Dim dec As Integer = 0
        Dim Remainder As Integer
        Dim result As String = ""
        Dim j As Integer = 0
        Dim tabular_binario(40) As String


        'Modificamos los elementos ASCII en binarios'
        For i = 0 To trames.Length - 1
            'Recuperamos el valor numerica del ASCII leido en 8 bits'
            binario_en_8 = Asc(trames.ElementAt(i))

            'est esta necesario pero no se porque'
            dec = Val(binaire_en_8)

            'la modificamos en su valor ASCII en 6 bits'
            dec = dec - 48
            If dec > 40 Then
                dec = dec - 8
            End If

            While dec > 0
                remainder = dec Mod 2
                dec /= 2
                result = Remainder.ToString & result
                j = j + 1
            End While

            While j < 6    
                result = "0" & result
                j = j + 1
            End While

            tableau_binario(i) = result
            result = ""
            j = 0
        Next

        Return traduction_binaire_de_ASCII
    End Function
End Module

The error is that for the program, 1F / 2 = 8 instead of 7 but I do not know why it divides in this way

    
asked by ThePassenger 11.07.2016 в 10:24
source

1 answer

-1

You can do bit manipulation or use the class BitArray . Using the latter, the bits should be rearranged in MSF .

You can also take advantage of the native implementation for Base 64 that comes with the class Convert from .Net. For this case the only thing that should be taken into account is to replace the coding tables and verify the fill symbols.

' Tabla Base 64 '
Const TABLA_BASE64 As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

' Tabla AIVD tomada de "Table 2. ASCII payload armoring" del artículo "AIVDM/AIVDO protocol decoding" de Eric S. Raymond '
Const TABLA_AIVD As String = "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVW'abcdefghijklmnopqrstuvw"

Encoding:

Dim Texto as String = "Texto a codificar"

' Codifica en base 64 '
Dim cod as String = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(Texto))

' Elimina los símbolos de relleno finales '
If cod.EndsWith("==") Then
    cod = cod.Substring(0, cod.Length - 2)
ElseIf cod.EndsWith("=") Then
    cod = cod.Substring(0, cod.Length - 1)
End If

' Reemplaza los símbolos de Base 64 por los de AIVD'
dim sb as system.text.stringbuilder = new system.text.stringbuilder
dim pos as integer
For Each ch As Char In cod
    pos = TABLA_BASE64.IndexOf(ch)
    sb.Append(TABLA_AIVD(pos))
Next

' Texto codificado en AIVD '
cod = sb.tostring

Decoding:

' Reemplaza los símbolos de AIVD por los de Base 64 '
dim sb as system.text.stringbuilder = new system.text.stringbuilder
dim pos as integer
For Each ch As Char In cod
    pos = TABLA_AIVD.IndexOf(ch)
    sb.Append(TABLA_BASE64(pos))
Next

' Agrega los símbolos de relleno para Base 64 '
Dim m as integer = sb.Length Mod 4

If m Then
    sb.Append("="c, 4 - m)
End If

' Texto decodificado '
dim Texto as string = System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(sb.ToString))

This method can be used in any 8-bit encoding system in 6, with the corresponding symbol table.

    
answered by 30.07.2016 в 21:36