Using each digit of a byte

This one is kind of interesting.

A byte consists of two digits, right Now, each digit of this byte holds data for a different object.
Say the byte is 7F. The 7 would be the data for one object and the F would be the data for the other object.
Or say the byte is 04. 0 would be the data for the first object and 4 would be the data for the second object.

Get it

How would I write the byte this way
Say I have two textboxes, txt1 and txt2. txt1.Text = 4 and txt2.Text = 9. I want to write the byte 49h to an address to be specified later.

And to read it
Now say that the byte is 3F. I want txt1.Text = 3 and txt2.Text = F.

I hope I have made clear what I am trying to do. Enjoy.




Answer this question

Using each digit of a byte

  • Hital

    create a new windows application

    drop three textboxes, an error provider and two buttons on your form.

    put this code in the form1.vb

    Imports System.Globalization
    Public Class Form1

    Protected
    Overrides Sub OnLoad(ByVal e As System.EventArgs)
    TextBox1.MaxLength = 1
    TextBox2.MaxLength = 1
    TextBox3.MaxLength = 3
    Button1.Enabled =
    False
    Button2.Enabled = False
    MyBase.OnLoad(e)
    End Sub

    Private
    Sub TextBox1_TextChanged(ByVal sender As Object, _
    ByVal e As EventArgs) _
    Handles TextBox1.TextChanged, TextBox2.TextChanged
    Button1.Enabled =
    False
    If TextBox1.Text.Trim() = "" Or _
    TextBox2.Text.Trim() =
    "" Then Return
    Dim ctrl As Control = DirectCast(sender, Control)
    Dim b As Byte
    ErrorProvider1.SetError(ctrl, _
    IIf(
    Byte.TryParse(ctrl.Text, NumberStyles.HexNumber, _
    Nothing, b), "", "Invalid Input"))
    Button1.Enabled = ErrorProvider1.GetError(TextBox1) =
    "" And _
    ErrorProvider1.GetError(TextBox2) =
    ""
    End Sub

    Private
    Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) _
    Handles Button1.Click
    MessageBox.Show( _

    Byte.Parse(TextBox1.Text + TextBox2.Text, _
    NumberStyles.HexNumber).ToString())

    End Sub

    Private
    Sub TextBox3_TextChanged(ByVal sender As Object, _
    ByVal e As EventArgs) _
    Handles TextBox3.TextChanged
    Button2.Enabled =
    False
    Dim b As Byte
    Dim ctrl As Control = DirectCast(sender, Control)
    If ctrl.Text.Trim() = "" Then Return
    ErrorProvider1.SetError(ctrl, _
    IIf(
    Byte.TryParse(ctrl.Text, NumberStyles.Integer, _
    Nothing, b), "", "Invalid Input"))
    Button2.Enabled = ErrorProvider1.GetError(ctrl) =
    ""
    End Sub

    Private
    Sub Button2_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) _
    Handles Button2.Click
    Dim c As Char() = String.Format("{0:X2}", _
    Byte.Parse(TextBox3.Text)).ToCharArray()
    TextBox1.Text = c(0).ToString
    TextBox2.Text = c(1).ToString

    End Sub
    End Class

    Build and run.



  • nightowl03d

    Okay, that worked right. The only problem I am having now is converting the string value into a byte value. I have tried CByte and Convert.ToByte but they come up with errors. Any ideas


  • Jeffrey van Gogh - MS

    Just incase you hadnt got it yet. . .

    Imports System.Globalization
    Public Class Form1
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    TextBox1.MaxLength = 2
    TextBox2.MaxLength = 2
    TextBox3.MaxLength = 2
    Button1.Enabled =
    False
    Button2.Enabled = False
    MyBase.OnLoad(e)
    End Sub

    Private
    Sub TextBox1_TextChanged(ByVal sender As Object, _
    ByVal e As EventArgs) _
    Handles TextBox1.TextChanged, TextBox2.TextChanged
    Button1.Enabled = TextBox1.Text.Trim() <>
    "" And _
    TextBox2.Text.Trim() <>
    ""
    Dim ctrl As Control = DirectCast(sender, Control)
    Dim b As Byte
    Dim isError As Boolean = Not Byte.TryParse(ctrl.Text, b)
    If Not isError Then isError = b > 16
    ErrorProvider1.SetError(ctrl, IIf(isError
    And Not ctrl.Text.Trim() = "", "Input Error", ""))
    Button1.Enabled = Button1.Enabled
    And _
    ErrorProvider1.GetError(TextBox1) =
    "" And _
    ErrorProvider1.GetError(TextBox2) =
    ""
    End Sub

    Private
    Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    MessageBox.Show(
    String.Format("{0:X}{1:X}", _
    Byte.Parse(TextBox1.Text), Byte.Parse(TextBox2.Text)))
    End Sub

    Private
    Sub TextBox3_TextChanged(ByVal sender As Object, _
    ByVal e As EventArgs) Handles TextBox3.TextChanged
    Button2.Enabled =
    False
    Dim ctrl As Control = DirectCast(sender, Control)
    If ctrl.Text.Trim() = "" Then Return
    Dim b As Byte
    If
    Not Byte.TryParse(ctrl.Text, NumberStyles.HexNumber, Nothing, b) Then _
    ErrorProvider1.SetError(ctrl,
    "Invalid Input")
    Button2.Enabled = ErrorProvider1.GetError(ctrl) =
    ""
    End Sub

    Private
    Sub Button2_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
    Dim b As Byte = Byte.Parse(TextBox3.Text, _
    NumberStyles.HexNumber,
    Nothing)
    Dim c As Char() = String.Format("{0:X2}", b).ToCharArray()
    MessageBox.Show(
    String.Format("{0} and {1}", c(0), c(1)))
    End Sub
    End
    Class



  • Kris K

    To get the two nybbles of a byte (its digits, as you refer to them) you may use the following:

    Dim B As Byte = &h79
    Dim RightNybble As Byte = B And 15 '9
    Dim LeftNybble As Byte = (B >> 4) And 15 '7

    That's because a Byte is made of 8 bits. Each four bits make a nybble (or nibble). The &h75 number in the example above is actually this sequence of bits:

    0111 1001

    To isolate some bits we use an 'And mask', that is, we apply the 'And' bitwise operator in a way that we turn off the bits we don't want, while leaving unnaffected the bits we want. So, to isolate the right-most nybble, we apply an and mask that 'protects' the first four bits:

    0111 1001 And
    0000 1111 <- the mask,
    ---------
    0000 1001

    The numeric value of our mask is 15, that's why you see that 'B And 15' operation in the first snippet.

    If we were to just mask the leftmost nybble, we wouldn't get it's 'figure' value, but only it's numeric value, which is 7 * 16. We could divide the number by 16 afterwards, but it's not really necessary. Instead, we shift the bits to the right by four positions (in effect, displacing the original rightmost nybble):

    0111 1001
    >>      4 (Shift right by 4 bits)
    ---------
    0000 0111 (the 1001 bits are gone)

    At this point you have your leftmost nybble. Notice however that if the number you're converting is greater than 255, you'd better apply the And mask to this result as well. That's the purpose of the '(B >> 4) And 15' in the snnipet.

    Now, to convert a sequence of chars to Byte, you may use, as suggested, the Byte.Parse method, or, if you prefer, the Convert class:

    Const HexBase As Integer = 16
    Dim B As Byte = System.Convert.ToByte("7F", HexBase)

    Regards,

    Branco Medeiros


  • Heydar

    Dim b As Byte
    If
    Not Byte.TryParse(ctrl.Text, NumberStyles.HexNumber, Nothing, b) Then _
    ErrorProvider1.SetError(ctrl,
    "Invalid Input")


  • Charoite

    Branco,
    That worked perfectly. And was very easy for me to understand what you were saying. Thank you for explaining your snippet and how it works and why it works, instead of just posting a snippet or link.



  • Venu_nair

    Hi,

    You can use shared method "Parse" of the Byte structure to convert a string into a byte: eg(note that when implementing you should add error handling code).

    Dim MyByte as Byte = Byte.Parse(txt1.Text & txt2.Text, Globalization.NumberStyles.HexNumber)

    To get the string representable of a hex number, you can use the Hex function in VB:

    dim MyByteStr as String = Hex(MyByte)



  • Peter Mooney

    Alright Blair, that worked. Except it worked backwards. The two textboxes that are accepting input are in decimal format. They will be values from 0 to 15. When the value are written to the buffer, they will be converted to one byte and stored (in hex format of course).

    So say, txt1 was 15 and txt2 was 13. It would store the value FD into the buffer. And I already have error implementations in place.

    Then the other way. If the value in the buffer was E4 then txt1 would be 14 and txt2 would be 4.

    Thank you for your clear code though.


  • Using each digit of a byte