Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting MSB of a byte

Status
Not open for further replies.

dbrb2

Instructor
Jul 19, 2004
121
GB
Hi,

I havent used VB much recently. I'm reading dome data from a TCP server using the TcpClient class. This is read into a byte array. However, it turns out the data as received has a parity bit set, so converting it directly into ASCII on't work for many characters. I need to find a way to set the MSB of each byte read to 0 - I suspect this is very simple, but have not yet managed it!

Cheers,

Ben
 
127 in binary is:

0111 1111

If you AND this value with your Byte, the first digit will become 0 and the other digits will remain unchanged.

200 in binary is:

1100 1000 (and it with 127)
0111 1111
---- ----
0100 1000

You have effectively removed the MSB (setting it to 0).

In vb...

Code:
Dim Test as Byte

Test = 200
Debug.Print Test And 127

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks - I new an AND would do it, but wasn't aware I could AND the Byte with an Int like that.

The problem here is that out TCP server is sniffing physical data lines that have 7E2 data on them (7 data, Even parity, 2 stop). However, the server does not strip / set this bit before presenting the data - it sees it simply as a collection of 8 bits.

Therefore any data that comes in, I need to set the parity bit to 0 before decoding into ASCII (so thee 7 data bits are preserved unchanged)

Similarly, any characters that I send must have the MSB set as the parity bit for the remaining 7. I can set the bit as above - slightly trickier is that I will need to know the parity bit for every character I might send. I could either do a horrible lookup table, or alternatively is there a way of accessing individual bits to calculate parity for the 7 data bits?

Cheers,

Ben
 
It's been a really long time since I've thought about parity. As such, please understand that the subroutine I present here may not be 100% correct. However, if it's not correct, it's probably close to what you are looking for.

If I understand correctly, you want the most significant bit to be 1 if there is an even number of 1's in the other 7 bits. Otherwise, the most significant bit should be a 0.

Ex:
65 in binary is: 0100 0001, since there are an even number of 1's, the MSB should be set to 1.

Similarly: 0110 0111 has an odd number of ones, so MSB should be 0.

If I understand this correctly, then the following subroutine should do the trick.

Code:
Private Sub SetParity(ByRef Data As Byte)
    
    Dim i As Integer
    Dim BitCount As Integer
    
    BitCount = 0
    For i = 0 To 7 [green]' Probably only need to go to 6 here[/green]
        If (Data And (2 ^ i)) > 0 Then
            BitCount = BitCount + 1
        End If
    Next
    
    If BitCount Mod 2 = 0 Then
        Data = Data Or 128
    End If
    
End Sub

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Ah yes - that seems obvious now, but then again it always does when someone else points it out :)

You are right in that I only need to loop from 0 to 6, since when using the basic 7 bit ASCII the 8th bit is irrelevant, and will always be 0 anyway.

In fact, even parity is the other way around - MSB set to zero if even, one if odd, but the above will still work. Thanks for your rapid (and correct) advice!

Cheers,

Ben
 
Hmmm...

Actually, doesn't seem to be working quite as planned.

I have my incoming string from the serial port, with parity set

I create a byte array from the string using:
Code:
Dim bytes AS [Byte] () = System.Text.Encoding.ASCII.Getbytes(myString)

I then loop through each byte with:
Code:
For n = 0 to bytes.Length - 1
    bytes(n) = bytes(n) And 127
    newString += Chr(bytes(n)).ToString()
Next n

However, whether I check the contents of the original string or the new string, the parity problem remains.

For example the ASCII for character "1" is
0110001

Because of the parity bit, what VB sees is:
10110001

But even after Anding with 127

01111111

I still decode as the character "±"

10110001

Any thoughts?

Cheers,

Ben

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top