To download NI software, including the products shown below, visit ni.com/downloads.
Overview
This function allows you to convert a string containing a binary number into the correct numeric representation. Another VI will allow you to convert a binary number into a Decimal Number format.
Description
The LabVIEW String/Number Conversion Palette contains many functions to convert strings of various types (Decimal, Hex, Octal, Fractional, and Exponential) to a number. However there is not a function that converts a Binary String to Number. This can be done very easily by iterating through the string, checking if the current character equals 1, indexing a boolean array and converting that array to a number. You can place this VI on your block diagram as a SubVI and use it as you would with the other conversion functions. The string input will be your binary string that you want converted to a decimal number. By default this SubVI will handle up to 64bit numbers, you can change this by altering the properties of the Boolean Array to Number Function.
Requirements:
LabVIEW 2012 (or compatible)
Steps to Implement or Execute Code:
Additional Information or References
Block Diagram of Binary String to Decimal VI
Block Diagram of Binary Number to decimal VI
**This document has been updated to meet the current required format for the NI Code Exchange.**
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.
I want to not that this code doesn't work.
Example
Input: 100
Expected output: 4
Real output: 1
The reason for this is because the input is a reversed notation of a binary number. To get the expected output: 4 you need to input 001.
To solve this you just need to reverse the input string by the "reverse string" method in labVIEW.
WouterG,
Aha! Thanks for pointing that out. It turned out that all the numbers I tested were panlindroms, and I didn't see this issue. I've updated the code and the image.
No problem, glad I could help.
I have attached the VI which performs Boolean to decimal conversion
steveADI wrote:
You could also use the Scan Value Function on the String/Number Conversion palette and use the binary format specifer (%b) at the input.
That's going to be the easiest route, and likely faster than a G implementation.
But if you want to write something in G, you should use the String to Byte Arrray, Rotate, and OR functions to mask each bit into the value. This approach avoids "Boolean Array to Number", which hits the LV memory manager and can cause application memory bloat if overused. It also parses the string using integer operators, which generally execute faster than string operators.
(This can probably be written using fewer rotations, but I just whipped it together as an example of using these operators.)