bit pack Int16 into a Ushort VB.net - vb.net

I have to pack and unpack a 16bit Int from/into a Ushort in VB.net
This is how I thought I could do it (doesn't work, gives me overflow exception)
'Pack Int16 into ushort '
Dim usPacked = CType(Data, UShort)
'unpack Int16 from ushort '
Dim unpacked = CType(data,Int16)
Thanks!

You can use the old Union solution
<StructLayout(Runtime.InteropServices.LayoutKind.Explicit)> _
Structure s1
<FieldOffset(0)> Public AsShort As Short
<FieldOffset(0)> Public AsUShort As UShort
End Structure
Dim v1 = GetTheShortValue()
Dim v2 = new s1
v2.AsShort = v1
Dim v3 As UShort = v2.AsUShort

EDIT: Jared's answer is better than this one of mine :(
UShort can store integers from 0 through 65,535.
Short can store integers from -32,768 through 32,767.
Long can store integers from about -2 billion to +2 billion.
You'll get an overflow when you try to put negative numbers into a UShort, or when you try to put numbers over 32,767 into a Short. One solution is to use a Long as an intermediary.
'Pack Int16 into ushort '
Dim usPacked = CType(CLng(nData) + 32768, UShort)
'unpack Int16 from ushort '
Dim unpacked = CType(CLng(usPacked) - 32768, Int16)

Related

Assign a string to a byte array in a VBA user-defined type

I work with a relay module that I normaly connect via USB. That all works perfectly. Now I would like to connect it via the network. All manufacturer's VB.NET code works in vba except for accessing this module over the network.
Public Declare PtrSafe Function DapiOpenModuleEx Lib "DELIB64" (ByVal moduleID As Long, ByVal nr As Long, ByRef exbuffer As DAPI_OPENMODULEEX_STRUCT, ByVal open_options As Long) As Long
' Definitions for DapiOpenEx
Public Type DAPI_OPENMODULEEX_STRUCT
address(255) As Byte
timeout As Long
portno As Long
encryption_type As Long
encryption_password(31) As Byte
End Type
'Open ETH-Module with parameter
Dim handle as Ulong
Dim MyModuleID As UInt32
MyModuleID = 42
Dim open_buffer As New DELib64.DAPI_OPENMODULEEX_STRUCT
open_buffer.address = System.String.Copy(192.168.1.1 As String) As String
open_buffer.portno = 0
handle = DELib.DapiOpenModuleEx(MyModuleID, 0, open_buffer)
I am getting an error "open_buffer.address = System.String.Copy(192.168.1.1 As String) As String "
Can someone help me with what i need to change here?
Dim handle as LongLong
Dim MyModuleID As Long
MyModuleID = 42
Dim open_buffer As Delib64.DAPI_OPENMODULEEX_STRUCT
open_buffer.address = system.String.Copy("192.168.1.1" AS String) As String
open_buffer.portno = 0
handle1 = DapiOpenModuleEx(MyModuleID, 0, open_buffer, 0)
According to your comment, the original line of code is
strcpy((char*) open_buffer.address, "192.168.1.10");
So you need to copy the ASCII (single-byte) string "192.168.1.10" into a VBA byte array. This is surprisingly hard, since the obvious approach of open_buffer.address = StrConv("192.168.1.10", vbFromUnicode) won't work (you can't assign to a fixed-size array that's part of a type).
One obvious solution would be to make a Windows API call to CopyMemory, but if we want a VBA-only solution, a simple loop should suffice:
Dim i As Long
Dim b() As Byte
b = StrConv("192.168.1.10", vbFromUnicode)
For i = 0 To UBound(b)
open_buffer.address(i) = b(i)
Next
open_buffer.address(UBound(b) + 1) = 0 ' C-strings need to be 0-terminated
(I do have the feeling that this should be easier, so I'll gladly upvote competing, simpler answers.)

Is there a VB function to convert 32 bit float to little endian hex

As shown here: https://gregstoll.com/~gregstoll/floattohex/
I need to convert a 32 bit float to a little endian hex (click the swap endiness button before converting). I've managed to do this in python by converting to big endian then reordering, but I have no idea how to approach this issue in VB as I'm entirely new to the language. Using the Hex inbuilt function returns 19a, which i assume means its not correctly evaluating my input as a single.
I've found a recommended solution here but cant get it working:
https://www.tek-tips.com/faqs.cfm?fid=6404
Any suggestions would be great, thanks in advance.
There are a number of ways you could do this - the most obvious being the Copy Memory API. Some time ago, a pretty neat solution was published here: Extracting bits from a float in vba which avoided the need for the API
Basically, you'd just need a couple of short functions:
Option Explicit
Type SingleType
Value As Single
End Type
Type FourBytesType
Value(3) As Byte
End Type
Private Function SingleToBytes(f As Single) As Variant
Dim sngType As SingleType
Dim bytesType As FourBytesType
sngType.Value = f
LSet bytesType = sngType
SingleToBytes = bytesType.Value
End Function
Private Function BytesToHex(bytes As Variant) As String
Dim result As String
Dim i As Long
For i = LBound(bytes) To UBound(bytes)
result = result & IIf(bytes(i) < 16, "0", "") & Hex(bytes(i))
Next
BytesToHex = result
End Function
If you wanted to test Endianness and reverse the array, then something like the following, which kind of uses a Byte Order Mark, could be added. I haven't tested it on a big-endian processor but I think it'd work:
Private Function IsLittleEndianProcessor() As Boolean
Const BOM As Single = 1
Const MSB As Byte = 63
Dim bytes() As Byte
Dim n As Long
bytes = SingleToBytes(BOM)
n = UBound(bytes)
IsLittleEndianProcessor = (bytes(n) = MSB)
End Function
Private Function ChangeEndianness(bytes As Variant) As Variant
Dim result() As Byte
Dim n As Long, m As Long
ReDim result(UBound(bytes))
m = UBound(bytes)
For n = LBound(bytes) To UBound(bytes)
result(m) = bytes(n)
m = m - 1
Next
ChangeEndianness = result
End Function
I'm not actually sure how you want the hex string displayed but you could step backwards through the array to write the hex if needed. Sample test would be:
Public Sub TestMe()
Dim bytes As Variant
Dim output As String
bytes = SingleToBytes(3.1415)
If Not IsLittleEndianProcessor Then
bytes = ChangeEndianness(bytes)
End If
output = BytesToHex(bytes)
Debug.Print output
End Sub

VB.NET Address Pointer

I have a DLL with the following function and is being used in my application
WritetoBuffer(BYTE* pBuffer, DATA_TYPE Type);
This is the code being used in VB 6
Dim pBuffer() as byte
ReDim pBuffer(0 To (300 * 400 * 3 - 1))
Dim ppBuf As Long
ppBuf = VarPtr(pImageBuffer(0))
Dim Rtn As Integer
Rtn = WritetoBuffer(ppBuf, 1)
I am trying to write the equivalent code in VB.NET, but I am facing difficulties in it. Tried using the following function, but it's not working.
Public Function VarPtr(ByVal e As Object) As Intptr
Dim GC As GCHandle = GCHandle.Alloc(e, GCHandleType.Pinned)
Dim GC2 As Intptr = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return GC2
End Function
I haven't used Marshal Class or similar functions before and I am not sure about the right way to do it. Can someone please advice me on this?
As #Dai commented, .ToInt32 is incorrect here, and you can’t free the handle before you use it.
Dim pBuffer(300 * 400 * 3 - 1) As Byte
Dim pinned = GCHandle.Alloc(pBuffer, GCHandleType.Pinned)
Dim Rtn As Integer = WritetoBuffer(pinned.AddrOfPinnedObject(), 1)
pinned.Free()
Try this. You need GCHandle to stay alive while you make your call. Just returning the address of the pointer is not enough from what I understand. When you're done then free the handle.
Dim Handle As GCHandle = GCHandle.Alloc(pBuffer, GCHandleType.Pinned)
Dim ppbuf As IntPtr = Handle.AddrOfPinnedObject.ToInt32
Dim Rtn As Integer
Rtn = WritetoBuffer(ppbuf, 1)
Handle.Free()
In cases such as this you shouldn't need to get a pointer of your byte array. Due to the fact that since BYTE* pBuffer is used as an array, that is all you need it to be in VB.NET as well.
Thus you could declare your P/Invoke like this:
<DllImport("yourfile.dll")> _
Public Shared Function WritetoBuffer(ByVal pBuffer As Byte(), ByVal Type As Integer) As Integer
End Function
...and then use it like this:
Dim pBuffer() as byte
ReDim pBuffer(0 To (300 * 400 * 3 - 1))
Dim Rtn As Integer = WritetoBuffer(pBuffer, 1)

Parsing binary to BigInteger in VB .NET?

How are you?
I wrote a program manipulating big binary chains (string variables). This said manipulation requires me to store my chains in a variable so I can use them as numbers. The only variable type that I have found big enough to store such lengthy numbers is BigInteger (we are talking 1.0E100+).
I would like to use something like:
val = BigInteger.Parse(bin, 2)
But the second parameter needed is a NumberStyles object, which can only refer to a NumberStyles.HexNumber.
Is there a simple/optimal way to do this?
Thank you very much. :)
This converts a binary string to BigInteger in 8 bit chunks. It assumes that the binary string represents a positive number.
Private Function BinToBI(ByRef binstr As String) As BigInteger
Dim t As New List(Of Byte)
Dim s As String
Dim idx As Integer = binstr.Length
Do While idx > 0
'get 8 bits
If idx >= 8 Then
s = binstr.Substring(idx - 8, 8)
Else
s = binstr.Substring(0, idx).PadLeft(8, "0"c)
End If
'convert to byte and add to list
Dim b As Byte = Convert.ToByte(s, 2)
t.Add(b)
idx -= 8
Loop
'force to positive
If t(t.Count - 1) >= 128 Then
t.Add(0)
End If
Dim rv As New BigInteger(t.ToArray)
Return rv
End Function
for testing
Dim d As Double = 1.0E+101
Debug.WriteLine(d.ToString("n2"))
Dim bi As BigInteger
' Dim bin As String = "1111111111111111111111111111111" 'Integer.MaxValue
' Dim bin As String = "111111111111111111111111111111111111111111111111111111111111111" 'Long.MaxValue
Dim bin As String = "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110"
bi = BinToBI(bin)
Debug.WriteLine(bi.ToString("n2"))
This was not well tested but should give you some ideas.

UInt32 to Int32

If I have a VB.Net function that returns an Int32, but uses an unsigned int (UInt32) for calculations, etc. How can I convert a variable "MyUintVar32" with a value of say "3392918397 into a standard Int32 in VB.Net?
In c# if I just do a "return (int)(MyUintVar32);", I get -902048899, not an error.
I've tried several different methods. What is the difference in the way c# handles these conversions versus VB.Net?
I realize this is an old post, but the question has not been answered. Other people my want to know:
Dim myUInt32 As UInt32 = 3392918397
Dim myInt32 As Int32 = Convert.ToInt32(myUInt32.ToString("X"), 16)
the reverse operation:
myUInt32 = Convert.ToUInt32(myInt32.ToString("X"), 16)
Also, one can create a union structure to easily convert between Int32 and UInt32:
Imports System.Runtime.InteropServices
<StructLayout(LayoutKind.Explicit)> _
Public Structure UnionInt32
<FieldOffset(0)> _
Public IntValue As Int32
<FieldOffset(0)> _
Public UIntValue As UInt32
End Structure
Dim MyUnionInt32 as UnionInt32
MyUnionInt32.UIntValue = 3392918397
Dim IntVal as Int32 = MyUnionInt32.UIntValue '= -902048899
the reverse operation:
MyUnionInt32.IntValue = -902048000
Dim UIntVal as UInt32 = MyUnionInt32.UIntValue '= 3392919296
Cheers, TENware
3392918397 is too big to fit into a signed 32-bit integer, that's why it is coming out negative, because the most significant bit of 3392918397 is set.
1100 1010 0011 1011 1101 0011 0111 1101
If you want to maintain integers of this proportion inside a signed integer type, you'll need to use the next size up, a 64-bit signed integer.
It's not an optimal solution, but you can use BitConverter to get a byte array from the uint and convert the byte array to int.
Dim myUInt32 As UInt32 = 3392918397
Dim myInt32 As Int32 = BitConverter.ToInt32(BitConverter.GetBytes(myUInt32), 0)
You can't convert 3392918397 into an Int32 since that number is too large to fit in 31 bits. Why not just change the function to return a UInt32?
Or after doing the Uint32 work check it against MAXINT and 0.
If > MAXINT and < 0 then you're ok. If not you "overflowed" and should throw an exception.
I don't remember if MAXINT is defined. You can use: 2^31 - 1 instead.