Convert Number in VB.net

BarbaraS

New member
Joined
Jun 21, 2025
Messages
2
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.
 
Back
Top