Convert ASCII to binary in VB.NET

2

I have a problem trying to convert ASCII to binary. In effect, I am translating an AIS frame (which I used for navigation on ships) symbol by symbol on six bits from 15 (where the payload begins). And despite having !AIVDM,1,1,,A,15O86n001TJ3KutH8ar@<h;l06Hh,0*5D the translation is for each symbol: 1100001 .

Here the payload is: 15O86n001TJ3KutH8ar@<h;l06Hh

Public Sub decodage_AIS(tramaAs String)
    Dim coleccion_ASCII(trames.ToString.Length) As String
    Dim variable As Char
    Dim binaryDigits() As Char

    For i = 15 To tableau_ASCII.Length - 1
        variable = trama(i)
        equivalent_byte = equivalent_byte + System.Convert.ToString(Asc(variable), 2).PadLeft(4, "0")
        binaryDigits = equivalent_byte.ToCharArray
        My.Application.DoEvents()
    Next

For example, 1 should be converted to 000001 but is converted to 1100001 like all other symbols, 5 in 000101 is converted to 1100001 .

Finally, I expect this conversion:

1       000001
5       000101
O       011111
8       001000
6       000110
n       110110
0       000000
0       000000
1       000001
T       100100
J       011010
3       000011
K       011011
u       111101
t       111100
8       001000
a       101001
r       111010
@       010000
<       001100
h       110000
;       001011
l       110100
0       000000
6       000110
H       011000
h       110000

I have read the answer from user3947014 and also this from Pradeep Kumar for the question Convert from string to binary vb.net

** I think the problem comes from variable = trama(i)**

    
asked by ThePassenger 06.07.2016 в 16:34
source

1 answer

1

Starting from taking each value as a char as you already have defined in the variable variable (what an original! Hehe):

Dim binaryCharCode As String
binaryCharCode = Convert.ToString(variable, 2)

What this ToString(char c, int i) does is basically transform the char c into base i .

From what I see with your definition you want to treat the numbers in that string as numbers itself and not as a char so it is necessary to check in each cycle if the char with which we iterate can be (or not) a number:

Dim num As Integer
If Not Integer.TryParse(variable, num) Then
    num = -1
End If

Now if the variable num is different from -1 means we have a number and we have a letter (or something else):

If num = -1 Then
    'Tratamos cómo número
Else
    'Tratamos como char
End If

To put everything together (with your code):

Dim coleccion_ASCII(trames.ToString.Length) As String
Dim variable As Char
Dim equivalent_byte As String

For i = 15 To tableau_ASCII.Length - 1
    variable = trama(i)
    Dim num As Integer
    If Not Integer.TryParse(variable, num) Then
        num = -1
    End If
    If num = -1 Then
        equivalent_byte = equivalent_byte + Convert.ToString(num, 2)
    Else
        equivalent_byte = equivalent_byte + Convert.ToString(variable, 2)
    End If
Next
My.Application.DoEvents()

Highlight that you had inside of your loop the DoEvents and should go outside (if I'm not wrong).

    
answered by 06.07.2016 в 17:07