Example Code

Binary String and Binary Number to Decimal Number Conversion

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

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:

  1. Open the attached folder
  2. Run the Binary Number to Decimal Number.vi to convert from binary Number to decimal
  3. Run the Binary String to Decimal Number.vi to convert from string to decimal

 

 

Additional Information or References

Block Diagram of Binary String to Decimal VI

B String to Decimal.PNG

 

 

Block Diagram of Binary Number to decimal VI

B number to decimal.PNG

 

 

**This document has been updated to meet the current required format for the NI Code Exchange.**

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ben Sisney
FlexRIO V&V Engineer
National Instruments
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
WG-
Member
Member
on

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.

Ben_S
Active Participant
Active Participant
on

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.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ben Sisney
FlexRIO V&V Engineer
National Instruments
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
WG-
Member
Member
on

No problem, glad I could help.

steveADI
Member
Member
on

You could also use the Scan Value Function on the String/Number Conversion palette and use the binary format specifer (%b) at the input.

pmg08
Member
Member
on

I have attached the VI which performs Boolean to decimal conversion

Staab_Engineering
Member
Member
on

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.

ASCII_Binary_Striing_to_Little-Endian_U64.png

(This can probably be written using fewer rotations, but I just whipped it together as an example of using these operators.)