Question convert 2 x int16 to int32

TimEaden

New member
Joined
Dec 13, 2009
Messages
2
Programming Experience
1-3
hi there

I want to reflect the bit patterns in 2 x int16 (an upper and lower 16 bits) into a single int 32
how can this be done, is bit converter what I am looking for ?

Thanks in advance for any help

Tim
 
Got it sussed

Have figured it out myself
As you might of guessed I am a PLC programmer in which bit manipulation is easy

Vb.Net is a bit harder but I have achieved the answer by using bytes and bit converter

'Int16 to Int32 converter for RFID
'Tim Eaden
'14/12/2009


Public Class Form1

Dim RFIDLow As Int16 = -4480
Dim RFIDHigh As Int16 = -6841
Dim RFIDResult As Int32

Dim LowBytes(1) As Byte
Dim HighBytes(1) As Byte
Dim ByteVals(3) As Byte


Private Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Try

LowBytes = BitConverter.GetBytes(RFIDLow) 'convert int16 to byte arrays
HighBytes = BitConverter.GetBytes(RFIDHigh)

ByteVals(0) = LowBytes(0)
ByteVals(1) = LowBytes(1)
ByteVals(2) = HighBytes(0)
ByteVals(3) = HighBytes(1)

RFIDResult = BitConverter.ToInt32(ByteVals, 0) 'bit coverter takes bytes and converts them to int32

MessageBox.Show("RFID is " & RFIDResult)

Catch ex As Exception

MessageBox.Show("Exception Thrown")

End Try

End Sub
End Class
 
If that was the result you were after then here is a simpler conversion:
VB.NET:
Dim source() As Short = {RFIDLow, RFIDHigh}
Dim target(0) As Integer
Buffer.BlockCopy(source, 0, target, 0, 4)
RFIDResult = target(0)
remarkably, so does this bit shift operation:
VB.NET:
RFIDResult = RFIDLow + (CInt(RFIDHigh + 1) << 16)
 
Back
Top