04-08-2020 08:14 AM - edited 04-08-2020 08:18 AM
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".
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
04-08-2020 11:48 AM
Please consider the following example. I have changed the representation to U8 (upper bound is 255) for better readibility:
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.
Anyway, is the behavior of the function when fed negative values plausible? Either way, an update of the help entry might be necessary ...
04-08-2020 11:59 AM
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
04-08-2020 01:02 PM
@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.
04-08-2020 03:23 PM
Heck, I'd expect anything other than 0-9, A-F to produce an error.
04-14-2020 03:50 AM
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.
04-14-2020 08:55 AM
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