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.

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
Build and run.
nightowl03d
Jeffrey van Gogh - MS
Kris K
To get the two nybbles of a byte (its digits, as you refer to them) you may use the following:
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:
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:
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):
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:
Regards,
Branco Medeiros
Heydar
Charoite
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
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.