LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Hexadecimal string to number: Minus sign (hyphen vs. en dash) for negative numbers

Hello everybody,

 

I am a little puzzled by the Hexadecimal string to number function and/or the respective help.

 

In the last row of the illustration table in the help (http://zone.ni.com/reference/en-XX/help/371361R-01/glang/hex_string_to_number/) it says that "Negative numbers are not permitted for hex" and suggests that for negative numbers (here more specific: strings such as "–30"), the function returns "0".

 

T139_HexToNumber_MinusSign_HyphenVsEnDash.jpg

As long as I use an en dash ("–" or Alt+0150) as a minus sign as the LabVIEW help does (see index 1 in screenshot), this works, but isn't this quite unintuitive? I certainly would have used a hyphen as a minus sign in the string (see index 0 in screenshot) if I wanted to indicate a negative number [of course acutally, neither is an actual minus sign]. I doublechecked, and numeric indicators etc. seem to display a hyphen ("-") for negative values as well.

 

Could this be a bug in the VI/Help or might this actually be the supposed behavior?

 

Thank you and best regards

T139

0 Kudos
Message 1 of 7
(4,146 Views)

Please consider the following example. I have changed the representation to U8 (upper bound is 255) for better readibility:

 

T139_HexToNumber_MinusSign_HyphenVsEnDash_U8.jpg

 

I suppose that the comment of the last row in the help is simply wrong, the en dash had nothing to do with a minus sign and was merely a (possibly poorly chosen) example for an invalid leading character.

 

T139_HexToNumber_MinusSign_HyphenVsEnDash_help.jpg

 

Anyway, is the behavior of the function when fed negative values plausible? Either way, an update of the help entry might be necessary ...

0 Kudos
Message 2 of 7
(4,091 Views)

There are two separate issues here.  One is your mis-understanding about the difference between the "representation" of a number by the computer and the "value" of the number.  The other is a (possible) "unexpected" (and, as far as I am concerned, "wrong") hidden feature in Hex String to Number function.

 

First, consider a 32-bit Integer, and how they can be represented as "binary digits" (or "bits") inside the computer.  With 32 bits, you can represent any non-negative number from 0 to 2^32 - 1 as a sum of powers of 2 from 0 .. 31.  In LabVIEW, such a representation is called an "unsigned 32-bit integer", or a U32.

 

But what if you want signed numbers?  Well, you change the representation of the number in the computer.  You reserve one bit (2^31) as a "sign" bit, and say "If this is 0, the number is as represented by the lower 31 bits, but if it is 1, the number represented by the lower 31 bits is a negative number".  But what negative number?  There are multiple ways to express negative numbers, but the one LabVIEW (and almost all other computers and computer languages use) is called "Two's Complement", the complement of the number with respect to 2^32, so -1 is represented by 2^32-1, which is FFFFFFFF (in Hex). 

 

Hexadecimal is a way of representing the bits of the number stored in memory, usually without regard to whether it is an I32 (where the number is "Sign-bit and 31-bit unsigned number) or U32 (where the number is unsigned 32-bit number).  Thinking of it this way, "negative" numbers don't make sense for Hex values that already represent a negative quantity when expressed as a signed value.  

 

I, personally, consider using an en-dash as a character in a Hex string an "illegal character" that should properly trigger an Error on conversion.

 

Bob Schor

Message 3 of 7
(4,084 Views)

@Bob_Schor wrote:

I, personally, consider using an en-dash as a character in a Hex string an "illegal character" that should properly trigger an Error on conversion.


I would expect the + sign to do the same.


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 7
(4,073 Views)

Heck, I'd expect anything other than 0-9, A-F to produce an error.

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 5 of 7
(4,052 Views)

The way it is now, VI behavior and help are inconsistent, so my exclusive suggestions would be either:

 

1) adapt help entry to current VI behavior
a) mention two's complement conversion
b) append respective example in table
c) allow minus (and plus?) sign as leading character in string

d) remove example "negative numbers are not permitted for hex" in combination with the en dash 

 

2) update VI behavior and help
a) return 0 (instead of rasing an error as the VI has no error handling) when input string starts with a minus and/or (?) plus sign, i. e. to treat them as illegal characters just like any other character except for [A-F][a-f][0-9]

b) in last example of help entry, replace en dash with a hyphen and leave comment "negative numbers are not permitted for hex" as is

 

What is the best way to approach NI support with this? Which suggestion would you prefer? Is there something to add?

 

Thanks for your help.

0 Kudos
Message 6 of 7
(3,956 Views)

Go to Support.ni.com and open a Support ticket.  One of the things you can do is "Report a Bug".  You don't have a lot of space in the ticket to describe the bug, but you can copy and paste a link to your Forum Post, then summarize the bug.

 

Bob Schor

0 Kudos
Message 7 of 7
(3,930 Views)