TerryLindquist2
asked on
Xor resulting byte
I am using VB6 doing bit manipulation
The values of the individual bits correspond to a specific action needed to be performed or to knowledge of equipment status.Example byte 11101000 the last 0 may mean that a piece of equipment is on. The program sending the string of bytes inverts (XOR's) the bit values of some but not all bytes. I wanted to XOR the byte and get the orginal byte so I wrote the following:
byteA = 11111111 Xor byteA 'byteA = 11011111 in this example
my result is 102112
I expected 00100000
I thought I was given the decimal value, however it converts to 11000111011100000 as binary
How do I get the Xor'ed byte as my result
Thanks Terry
The values of the individual bits correspond to a specific action needed to be performed or to knowledge of equipment status.Example byte 11101000 the last 0 may mean that a piece of equipment is on. The program sending the string of bytes inverts (XOR's) the bit values of some but not all bytes. I wanted to XOR the byte and get the orginal byte so I wrote the following:
byteA = 11111111 Xor byteA 'byteA = 11011111 in this example
my result is 102112
I expected 00100000
I thought I was given the decimal value, however it converts to 11000111011100000 as binary
How do I get the Xor'ed byte as my result
Thanks Terry
ASKER
ozo thanks for the very quick response.
I am very new to this so I must ask:
1. how does this code work?
2. how do I add it to my code?
3. Will it give me the binary 8 bit string I need later in the program?
4. can I substitue a variable like byteA = BinaryToDec(byteA) Xor BinaryToDec("11111111")
Terry
I am very new to this so I must ask:
1. how does this code work?
2. how do I add it to my code?
3. Will it give me the binary 8 bit string I need later in the program?
4. can I substitue a variable like byteA = BinaryToDec(byteA) Xor BinaryToDec("11111111")
Terry
byteA = BinaryToDec("11011111")
byteA = byteA Xor BinaryToDec("11111111")
byteA = byteA Xor BinaryToDec("11111111")
ASKER
My inexperience and the lack of comments has made this difficult for me to understand. I will attempt to run this and see if it will fill my needs. What I am guessing is that Xor cannot accept a byte and perform bit manipulation on the individual bits within that byte thus assembling a new byte. Each bit must be removed, manipulated and then reassembled into the new byte.
Anyone that can help make this more clear will receive extra points. Please note that byteA will always be changing. It is to be compared to a byte 11111111 which will be constant. As noted in my first post I need to receive the inverted byte in return not a dec value.
Anyone that can help make this more clear will receive extra points. Please note that byteA will always be changing. It is to be compared to a byte 11111111 which will be constant. As noted in my first post I need to receive the inverted byte in return not a dec value.
byteA = &HDF
byteA = byteA Xor &HFF
stringA = binString(byteA)
Function binString(N As Long) As String
Do While N > 0
a = a + CStr(N Mod 2)
N = N \ 2
Loop
a = StrReverse(a)
g = Len(a)
If g < 8 Then
t = String$(8 - Len(a), "0")
a = t + a
End If
binString = a
End Function
byteA = byteA Xor &HFF
stringA = binString(byteA)
Function binString(N As Long) As String
Do While N > 0
a = a + CStr(N Mod 2)
N = N \ 2
Loop
a = StrReverse(a)
g = Len(a)
If g < 8 Then
t = String$(8 - Len(a), "0")
a = t + a
End If
binString = a
End Function
you could always try
a = not a
not in vb is a bitwise flip on a byte.
a = not a
not in vb is a bitwise flip on a byte.
ASKER
Msroberts - I like your simple approach. I tried it and my result is still not what I expect, which is 00100000
ByteA as string 'if I use byte I get overflow errors
ByteA = 11011111 'for this example, it will always be different
InvbyteA = Not byteA 'I inserted your suggestion and I got -11011112 as my result
Any idea why this is happen? Any good books on byte and bit manipulation with VB6? I have not had time to try "ozo's" most recent solution.
Thanks Terry
ByteA as string 'if I use byte I get overflow errors
ByteA = 11011111 'for this example, it will always be different
InvbyteA = Not byteA 'I inserted your suggestion and I got -11011112 as my result
Any idea why this is happen? Any good books on byte and bit manipulation with VB6? I have not had time to try "ozo's" most recent solution.
Thanks Terry
ASKER
Thanks Everyone for all their help but I did the following. It may be ugly but it works
ba1 = Left(byteA, 1)
ba1 = ba1 Xor 1
ba2 = Mid(byteA, 2, 1)
ba2 = ba2 Xor 1
ba3 = Mid(byteA, 3, 1)
ba3 = ba3 Xor 1
ba4 = Mid(byteA, 4, 1)
ba4 = ba4 Xor 1
ba5 = Mid(byteA, 5, 1)
ba5 = ba5 Xor 1
ba6 = Mid(byteA, 6, 1)
ba6 = ba6 Xor 1
ba7 = Mid(byteA, 7, 1)
ba7 = ba7 Xor 1
ba8 = Mid(byteA, 8, 1)
ba8 = ba8 Xor 1
InvbyteA = ba1 & ba2 & ba3 & ba4 & ba5 & ba6 & ba7 & ba8
ba1 = Left(byteA, 1)
ba1 = ba1 Xor 1
ba2 = Mid(byteA, 2, 1)
ba2 = ba2 Xor 1
ba3 = Mid(byteA, 3, 1)
ba3 = ba3 Xor 1
ba4 = Mid(byteA, 4, 1)
ba4 = ba4 Xor 1
ba5 = Mid(byteA, 5, 1)
ba5 = ba5 Xor 1
ba6 = Mid(byteA, 6, 1)
ba6 = ba6 Xor 1
ba7 = Mid(byteA, 7, 1)
ba7 = ba7 Xor 1
ba8 = Mid(byteA, 8, 1)
ba8 = ba8 Xor 1
InvbyteA = ba1 & ba2 & ba3 & ba4 & ba5 & ba6 & ba7 & ba8
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Do
BinaryToDec = BinaryToDec + (Left(BinaryValue, 1) * 2 ^ (Len(BinaryValue) - 1))
BinaryValue = Mid(BinaryValue, 2)
Loop Until BinaryValue = ""
End Function
byteA = BinaryToDec("11011111") Xor BinaryToDec("11111111")