Click here to Skip to main content
14,301,527 members
Rate this:
Please Sign up or sign in to vote.
See more:
Am I right in thinking that in a little endian 4 byte signed int, the sign is held in the top bit of the 4th byte? i.e.(in binary) xxxxxxxx xxxxxxxx xxxxxxxx 1xxxxxxx is negative and xxxxxxxx xxxxxxxx xxxxxxxx 0xxxxxxx is positive?

What I have tried:

Since I only *suspect* that the data I am looking at is signed, rather than *knowing* I am uncertain as to how to prove things either way.
Updated 16-Jan-19 7:53am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

OH, if you are wanting to determine how endian-ness works in VB you could try something like:
(Use your values and try it out.)
Dim a() As Byte = { &H1C }
Dim b(3) As Byte
If BitConverter.IsLittleEndian Then
    b(0) = a(0)
    b(3) = a(0)
End If
Dim key As Integer = BitConverter.ToInt32(b, 0)

Found at: Change byte array to integer in - Stack Overflow[^]

How about this?
Private Function BArrayToInt(ByRef bArray() As Byte) As Integer
    Dim iReturn As Integer
    Dim i As Integer
    For i = 0 To UBound(bArray) - LBound(bArray)
        iReturn = iReturn + bArray(i) * 2 ^ i
    Next i

    BArrayToInt = iReturn

End Function

from : bytearray - Byte Array to a Signed Integer in VB6 - Stack Overflow[^]
Member 14100511 17-Jan-19 0:58am
Thanks raddevus. I should have said I'm working in VB6 as befits my age.
I don't see how the VB6 solution is going to return a signed value, but will give it a try.

Mostly my question is about where the sign bit is held in a little endian 4 byte number.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100