none
Textbox1.text to Hex and convert to little endian RRS feed

  • Question

  • Hello, im trying to input an number into my textbox1.text then when i click button 1 i want the textbox1.text will be converted into hex then convert it to little endian.

    Im using this but im getting error:

            Dim i As Integer = Hex(TextBox1.Text)
            Dim abyt() As Byte = BitConverter.GetBytes(i)
            If BitConverter.IsLittleEndian Then
                Array.Reverse(abyt)
                MsgBox(abyt.ToString)
            End If
        End Sub

    Monday, September 30, 2019 11:20 AM

Answers

  • Hi

    I think those MessageBoxes were correct.

    Assuming your TextBox entry is a decimal.

    (from the site included in the code below)

    ' Form1 with TextBox1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    		' https://en.wikipedia.org/wiki/Endianness
    
    		'	TextBox1.Text = 1631  (a Decimal)
    		'	Decimal 1631 = 065F hex
    		' 065F hex  =  5F 06 00 00 little endian
    		' 065F hex  =  00 00 06 5F big endian
    
    		Dim abyt() As Byte = BitConverter.GetBytes(CInt(TextBox1.Text))
    		If BitConverter.IsLittleEndian Then
    			MessageBox.Show("LittleEndian " & GetString(abyt))
    			Array.Reverse(abyt)
    			MessageBox.Show("BigEndian " & GetString(abyt))
    		End If
    	End Sub
    	Private Function GetString(ByVal bytes As Byte()) As String
    		Dim sb As New Text.StringBuilder(bytes.Length * 2)
    		Dim s As String
    		For Each b As Byte In bytes
    			s = Conversion.Hex(b)
    			If s.Length = 1 Then
    				sb.Append("0" & s & " ")
    			Else
    				sb.Append(s & " ")
    			End If
    		Next
    		Return sb.ToString()
    	End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by extream87 Tuesday, October 1, 2019 12:09 AM
    Monday, September 30, 2019 6:30 PM

All replies

  • Hi

    Try this code:

    ' Form1 with TextBox1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		Dim i As Integer = Convert.ToInt32(TextBox1.Text, 16)
    		Dim abyt() As Byte = BitConverter.GetBytes(i)
    		If BitConverter.IsLittleEndian Then
    			MessageBox.Show("LittleEndian " & GetString(abyt))
    			Array.Reverse(abyt)
    			MessageBox.Show("BigEndian " & GetString(abyt))
    		End If
    	End Sub
    	Private Function GetString(ByVal bytes As Byte()) As String
    		Dim sb As New Text.StringBuilder(bytes.Length * 2)
    		Dim s As String
    		For Each b As Byte In bytes
    			s = Conversion.Hex(b)
    			If s.Length = 1 Then
    				sb.Append("0" & s & " ")
    			Else
    				sb.Append(s & " ")
    			End If
    		Next
    		Return sb.ToString()
    	End Function
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, September 30, 2019 12:46 PM
    Monday, September 30, 2019 12:45 PM
  • leshay one more time thanks for your help.

    The code is not what i want...

    I've changed this to show the result in HEX:

    Dim i As Integer = Convert.ToInt32(TextBox1.Text, 16)

    to

     Dim hexad = Hex(TextBox1.Text)
     Dim i As Integer = Convert.ToInt32(hexad, 16)

    The result:

    Should be: 00 00 5F 06

    Should be: 06 5F 00 00




    • Edited by extream87 Monday, September 30, 2019 5:11 PM
    Monday, September 30, 2019 5:10 PM
  • Hi

    So we are on the same page. Whatn is the string in TextBox1 that you tested with?


    Regards Les, Livingston, Scotland

    Monday, September 30, 2019 5:44 PM
  • Ive used 1631

    Monday, September 30, 2019 5:46 PM
  • Ive used 1631

    Hi

    Is that a HEX number?


    Regards Les, Livingston, Scotland

    Monday, September 30, 2019 5:53 PM
  • Hi

    I think those MessageBoxes were correct.

    Assuming your TextBox entry is a decimal.

    (from the site included in the code below)

    ' Form1 with TextBox1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    		' https://en.wikipedia.org/wiki/Endianness
    
    		'	TextBox1.Text = 1631  (a Decimal)
    		'	Decimal 1631 = 065F hex
    		' 065F hex  =  5F 06 00 00 little endian
    		' 065F hex  =  00 00 06 5F big endian
    
    		Dim abyt() As Byte = BitConverter.GetBytes(CInt(TextBox1.Text))
    		If BitConverter.IsLittleEndian Then
    			MessageBox.Show("LittleEndian " & GetString(abyt))
    			Array.Reverse(abyt)
    			MessageBox.Show("BigEndian " & GetString(abyt))
    		End If
    	End Sub
    	Private Function GetString(ByVal bytes As Byte()) As String
    		Dim sb As New Text.StringBuilder(bytes.Length * 2)
    		Dim s As String
    		For Each b As Byte In bytes
    			s = Conversion.Hex(b)
    			If s.Length = 1 Then
    				sb.Append("0" & s & " ")
    			Else
    				sb.Append(s & " ")
    			End If
    		Next
    		Return sb.ToString()
    	End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by extream87 Tuesday, October 1, 2019 12:09 AM
    Monday, September 30, 2019 6:30 PM
  • leshay i think i find the error... 
    1631=decimal

    06 5F =hex

    5F 06 by adding 2 bytes of 00

    5F 06 00 00

    Its correct and incorrect:

    Seems we need to add something in code to put zeros... If the number in hex = 2 add 00 00 00 if nymber in hex 4 = 00 00 if number in hex = 6 add 00 and then swap

    Like this:
    1631 = 06 5F here we need 00 00
    06 5F 00 00
    =
    00 00 5F 06 = Correct


    • Edited by extream87 Monday, September 30, 2019 7:16 PM
    Monday, September 30, 2019 7:15 PM
  • leshay i think i find the error... 
    1631=decimal

    06 5F =hex

    5F 06 by adding 2 bytes of 00

    5F 06 00 00

    Its correct and incorrect:

    Seems we need to add something in code to put zeros... If the number in hex = 2 add 00 00 00 if nymber in hex 4 = 00 00 if number in hex = 6 add 00 and then swap

    Like this:
    1631 = 06 5F here we need 00 00
    06 5F 00 00
    =
    00 00 5F 06 = Correct


    Hi

    OK what ever floats your boat - I vcan't agree however.


    Regards Les, Livingston, Scotland

    Monday, September 30, 2019 7:28 PM