From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

24bit hex to 2's complement

Solved!
Go to solution

Hello,

 

I need some help with the above problem, I have 24 bit hex data coming in and need to output signed 2's complement numbers. The parts work seperately but not together. Any help would be appreciated.

 

LabView 2011. If needed I can post a downrev version.

 

Many thanks in advance.

barry


0 Kudos
Message 1 of 17
(8,066 Views)

What do you mean by the parts do not work together?  Can you explain what you expect the results to be versus what you are getting?

 

By the way, the spelling is separator

0 Kudos
Message 2 of 17
(8,056 Views)

The attached VI should do the trick.  Start by converting the string to an array of bytes.  In a While loop, split off the top three bytes (using Delete from Array).  Inspect the top byte, which has the sign bit -- if it is 128 or bigger, you need to "extend the sign".  Create a "sign byte" of either 255 (sign set) or 0 (sign cleared) and add it to the front of the array, giving you an array of 4 bytes.  Convert this to an I32 using TypeCast.  Bring out using an Index tunnel, decide if you're done (number of elements in array < 3), and stop the While loop when done.

 

Bob Schor

0 Kudos
Message 3 of 17
(8,052 Views)

000000 should be 0

7FFFFF should be full scale positive, 8388607

800000 should be full scale negative, -8388608

FFFFFF should be -1

 

It works from the attached VI (below) but not when run inside my first attachment.

barry


0 Kudos
Message 4 of 17
(8,043 Views)
Solution
Accepted by topic author BarryGRAEllc

The problem in your first VI is that you are using the hexadecimal string to number function.  But you don't have a Hexadecimal string.  The would be a string like 6 characters 7 F F F F F.  You have a string of 3 bytes 7F FF FF.  You just need to use the typecast like you have.  But you will need to first append a null byte to the front of that so that it typecasts properly into the I32 datatype.

Message 5 of 17
(8,030 Views)

@Ravens Fan wrote:

The problem in your first VI is that you are using the hexadecimal string to number function.  But you don't have a Hexadecimal string.  The would be a string like 6 characters 7 F F F F F.  You have a string of 3 bytes 7F FF FF.  You just need to use the typecast like you have.  But you will need to first append a null byte to the front of that so that it typecasts properly into the I32 datatype.


I don't have LabVIEW 2011 installed yet so I can't open the VI, but I want to note that prepending a null byte won't work.  You need to sign-extend the first 2 bytes.  That is, if the most significant bit of the 24-bit value is 1 then you need to prepend 0xFF.  Assuming that your 24-bit data comes in as as ASCII representation of hex, it will look something like this snippet (you can enter "7F FF FF" into the string control).  If you already have the 24-bit data in a 32-bit value but it's the wrong sign, you can skip Hexadecimal String to Number.

sign-extend 24-bit to 32-bit.png

Message 6 of 17
(8,014 Views)

nathand wrote:

You need to sign-extend the first 2 bytes.


 Not sure why I wrote 2 bytes there; I meant just the first byte.  The code I posted is right.

0 Kudos
Message 7 of 17
(8,011 Views)

Thanks everyone, I am going to try these suggestions later today. I will let you know which ones work.

barry


0 Kudos
Message 8 of 17
(8,004 Views)

Hi Barry and supporters,

have you solved the problem at the end? I have the same kind of situation:

I have my ADC giving me a serial stream of 24 bits of data in two's complement format.

I can read the data through serial port with VISA and obtain the data stream in hex string format.

I tried all the solutions you provide here but still I cannot see my data stream decoded correctly in Int32.

 

I don't attach any code since I just worked with the pre-posted examples.

I hope someone can enlighten me.

 

Tom

0 Kudos
Message 9 of 17
(7,765 Views)

So you want help debugging an application we can't see and you aren't posting code because you got it from somewhere else  (WHERE?) and you may or may not have made any changes to it.

 

 

Please post code.  Also save it with data in a control or indicator saved as the default value so we can see what it looks like.

0 Kudos
Message 10 of 17
(7,759 Views)