0

The code gives additional 4 zeros, where the output should be without leading zeros. But I can't just trim it because with a different hex output it seems to produce different results. where did that four zeros come from? optionstrict on

The wrong output from the code (notice the additional leading 0000 in the wrong output in the front) 0000101001110011101011000110110111000000100000010010000001100000111111101101111101001010111110101011101001001100100101110111010011010110101101101100110000110110110000111001100100000111010011001011110110110010111111110000101011110010111010001000010100000101

The correct and expected binary should be (converted with an online hex to binary tool) 101001110011101011000110110111000000100000010010000001100000111111101101111101001010111110101011101001001100100101110111010011010110101101101100110000110110110000111001100100000111010011001011110110110010111111110000101011110010111010001000010100000101

The VB.net code I used

Private Function HexStringToByteArray(ByVal shex As String) As Byte()
    Dim B As Byte() = Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
    Return Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
End Function

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim hex As String = "0a73ac6dc0812060fedf4afaba4c9774d6b6cc36c399074cbdb2ff0af2e88505"
        Dim bytes As Byte() = HexStringToByteArray(hex)
        If BitConverter.IsLittleEndian Then
            Array.Reverse(bytes)
        End If
        Dim myBA3 As New BitArray(bytes)
        Dim myba3_reversed As New BitArray(myBA3.Length)
        Dim count As Integer = (myBA3.Count - 1)
        Dim myba3BITS As String = Nothing
        For i = 0 To myBA3.Count - 1
            If myBA3(i) = True Then
                myba3BITS &= "1"
            End If
            If myBA3(i) = False Then
                myba3BITS &= "0"
            End If

            count = (myBA3.Count - 1) - i
            myba3_reversed(i) = myBA3(count)

        Next i

        Dim reversedBITS As String = Nothing

        For i = 0 To myba3_reversed.Count - 1

            If myba3_reversed(i) = True Then
                reversedBITS &= "1"
            End If
            If myba3_reversed(i) = False Then
                reversedBITS &= "0"
            End If

        Next i

        Dim bits As String = reversedBITS
    End Sub
3
  • Write a proper question. If you have read up on how to use this site in the Help Center then you know that you need to explain what behaviour you expect and what behaviour you get. Just saying that the output is wrong is meaningless because, for all we know, it's correct your expectation is wrong. Commented Apr 18, 2020 at 15:06
  • As for the leading zero, if you're going to process pairs of digits then it should be obvious that you need an even number of digits to start with. Commented Apr 18, 2020 at 15:07
  • @jmcihinney : Thanks for your insight. I changed the code and the question. please go through it Commented Apr 18, 2020 at 16:56

1 Answer 1

1

Your input starts with "0a". If I use the Windows Calculator app in Programmer mode and enter that in HEX, the BIN output is "1010". Your code is taking each pair of hexadecimal digits and outputting eight binary digits, a buye for a byte. If you wanted to express the binary value 1010 in eight digits, what would it look like? You'd pad the value with four leading zeroes, wouldn't you? Where have you see that before? If your input doesn't have aleading zero then you need to add one. If your output does have leading zeroes and you don't want them, take them off. This is why you need to actually understand what your code is doing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.