I am working on a project where I will be receiving data through a serial port in the form of a series of 32-bit signed integers. I have only worked with unsigned integers before and I don’t know how to handle the negative numbers. From my readings, it appears all the other bits are inverted if the first bit is a 1. I’ve been looking at some of the BitWise functions and thinking I can used BitAnd to test for the first bit being 1 then use OnesComplement to get the value, but this seems very convoluted. Is there a better way?
I assume you read into a memoryblock ?
at that point it is “bytes” - not integers or signed integers etc
when you pull it OUT of the memory block using one of the accessors is when you’ll want to use the right accessor
beyond that you should not need to care
certainly not about flipping bits
try this … just as an exercise
dim mb as new memoryblock(2) mb.Byte(0) = &b10101010 mb.byte(1) = &b10101010
now … is that 2 byte value a signed integer ?
it is if you do
dim i as int16 = mb.Int16Value(0)
or an unsigned ?
it is if you do
dim j as Uint16 = mb.UInt16Value(0)
same bits - different interpretation
I’ve been reading it into a string and using MidB to get the individual bytes. Using a memoryblock looks like a much better way to do it.
Memoryblocks look like they can make everything a lot simpler as long as I don’t run into endian problems. I am using Modbus and the standard calls for big endian 2-byte registers. It is silent on 4-byte (or larger) registers, and some implementations big endian between the 2-byte registers (which looks like it will work with a memoryblock), but some use little endian.
strings are handy in many ways
BUT when dealing with things like network transmission you get “bytes” and memoryblocks do work well for that (I’d say better)
And you dont have to use MidB CharB etc which are BYTE manipulation anyways
Another way to go might be a Structure.