LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting hexa string to negative decimal

Solved!
Go to solution

Hi everyone,

 

I am getting this string from a serial communication with a sensor : FFFF7748. I would like to convert this negative value to a decimal value.

My idea is to convert this hexa string value to a Binary value, find the complement, and add 1 to the resul and finally convert it to a decimale value 

Would someone have an idea?

Thanks.

User

 

0 Kudos
Message 1 of 10
(4,472 Views)

Use the Hexadecimal string to number function from the number/string conversion palette

Capture.PNG

0 Kudos
Message 2 of 10
(4,470 Views)

@User79 wrote:

Hi everyone,

 

I am getting this string from a serial communication with a sensor : FFFF7748. I would like to convert this negative value to a decimal value.

My idea is to convert this hexa string value to a Binary value, find the complement, and add 1 to the resul and finally convert it to a decimale value 

Would someone have an idea?

Thanks.

User

 


One thing I have problems with when fielding questions of this nature is:

  • Is the incoming string ASCII "FFFF7748", or
  • Is the incoming string an array of bytes 0xFF 0xFF 0x77 0x48 (0xFFFF7748)?

I have a feeling it's the latter.  If this is the case:

Capture.PNG

 

Oops edit - better solution, can change endienness if needed.  Someone will probably post this sooner than me, though.

Capture.PNG

Choosing I32 automatically gets you the negative number representation you want.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 3 of 10
(4,451 Views)

@billko wrote:

Oops edit - better solution, can change endienness if needed.  Someone will probably post this sooner than me, though.


I rarely use Type Cast.  And when dealing with VISA or TCP/IP, the Flatten To String and Unflatten From String are my go to functions for data packing.

 

And, Bill stated, it really does depend on what the data actually is.  ASCII string with the hex values or a raw data stream that just happens to be stored in a string?  The ASCII string needs the Hexadecimal String To Number while the raw data stream need the Unflatten From String.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 10
(4,426 Views)

@User79 wrote:

 

I am getting this string from a serial communication with a sensor : FFFF7748. 

 


As mentioned already by others, the problem is ambiguous and depends if this a formatted string of length 8 exclusively containing the characters 0..F or if this is a binary string of 4 characters. Do you know the value that is supposed to correspond to that string and that it really is negative? (In the most general case, you'll also need to worry about byte order, for example. But yes, this looks like a big-endian negative I32 integer).

 

Also note that this does not convert to "decimal" (as you requested in the title), just to a numeric datatype. "Decimal" is just one way to format a value for human readability exclusively using the character 0..9 and some delimiters (sign, decimal delimiter, E, etc.).

0 Kudos
Message 5 of 10
(4,422 Views)

@crossrulz wrote:

@billko wrote:

Oops edit - better solution, can change endienness if needed.  Someone will probably post this sooner than me, though.


I rarely use Type Cast.  And when dealing with VISA or TCP/IP, the Flatten To String and Unflatten From String are my go to functions for data packing.

 

And, Bill stated, it really does depend on what the data actually is.  ASCII string with the hex values or a raw data stream that just happens to be stored in a string?  The ASCII string needs the Hexadecimal String To Number while the raw data stream need the Unflatten From String.


Funny thing is, I don't usually use it, either - as a direct consequence of reading earlier posts by you on the subject matter.  As soon as hit the post button it was like, wait, that's not the best way to do it.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 6 of 10
(4,408 Views)

Thanks for all your answers.

I understand that my question was ambiguous so I have attached a simplified version of my VI to show what is really happening.

Top part : working ok since my value is positive

Low part : not working well because I need to make a "special" treatment as it is a negative value

 

I hope this made things a bit more clear.

USer

0 Kudos
Message 7 of 10
(4,394 Views)

You cant have a Negative number in a U32 indicator.  change it to I32

 

Ken

Message 8 of 10
(4,390 Views)
Solution
Accepted by topic author User79

Hi User,

 

why don't you follow those suggestions about UnflattenFromString?

check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 9 of 10
(4,389 Views)

GerdW,

Had to run some tests to confirm...Thanks for your answer it works pefectely !

Best regards,

User

0 Kudos
Message 10 of 10
(4,356 Views)