Convert Number in VB.net

BarbaraS

New member
Joined
Jun 21, 2025
Messages
3
Programming Experience
1-3
Hello.

I wrote the number 10166 to a binary file. I swapped the bytes beforehand. This results in the number

I can read back the value the function returned, but I can't get the number 10166 back after swapping the bytes. Here's the code.

Private Function _swap4(valueAs Integer) As Integer
Dim tmp As Integer

tmp = value And &HFF
tmp = ((value And &HFF00) >> &H8) Or (tmp << &H8)
tmp = ((value And &HFF0000) >> &H10) Or (tmp << &H8)
tmp = ((value And &HFF000000) >> &H18) Or (tmp << &H8)

_swap4 = tmp
End Function

Private Sub Main()
Dim num as Integer = 10166

num = _swap4(num)
End Sub

This results in num = -1238958080 (the hex value in the file is then 00 00 27 B6).

When I read the value back in, I don't get the 10166 back after calling _swap4, but instead -74.

Where am I making a mistake?
I have to convert the read value again using _swap4, right?
 
You need to debug your code and actually look at the bits at each step to see what's happening and whether it is what you expect to happen. You don't fix code simply by looking at the initial input and the final output. Set a breakpoint at the top and then step through the code line by line. Before each step, ask yourself exactly what you expect to happen. After the step, check whether that actually did happen. Chances are that you'll find the problem yourself. You may still need help fixing it but at least you can provide us with all the relevant information.
 
You need to debug your code and actually look at the bits at each step to see what's happening and whether it is what you expect to happen. You don't fix code simply by looking at the initial input and the final output. Set a breakpoint at the top and then step through the code line by line. Before each step, ask yourself exactly what you expect to happen. After the step, check whether that actually did happen. Chances are that you'll find the problem yourself. You may still need help fixing it but at least you can provide us with all the relevant information.

I can't make sense of it. The value -1238958080 in the debug window is also saved that way. I get exactly the same value after reading it out again. Actually, after using the same swap function, it should be the original 10166 again. The code is a C# implementation, and the same swap function is used there.
 
There are four lines of code that do the calculations and three lines contains multiple operators. It doesn't sound to me like you're debugging at all but just looking at the final result. If you don't understand what each operation is doing, maybe the first thing to do is learn that.
 
There are four lines of code that do the calculations and three lines contains multiple operators. It doesn't sound to me like you're debugging at all but just looking at the final result. If you don't understand what each operation is doing, maybe the first thing to do is learn that.

Thank you. I'm sorry. But no one can explain it to me in a way that I can understand and relate to. I have no idea what to search for on Google either.
 
If you don't know what that code does then why use it at all? If you want to reverse the order of the bytes then just get the bytes and reverse the order. The BitConverter class has methods to convert between an Integer and a Byte array and vice versa, so all that's left is reversing the array, for which the obvious option is Array.Reverse.
 
Back
Top