Jump to content

Converting a number to a binary code


Teddy
 Share

Recommended Posts

  • Administrators

Recently an interesting question came up about converting a number to binary within Seeq. The goal was to convert an integer (0-255) to an 8-bit binary number.  This can be done by dividing the integer by 2, 8 times and keeping track of the remainders.  More information about binary numbers can be found here. https://en.wikipedia.org/wiki/Binary_number

 

Note: For this conversion to work the input signal needs to be an integer between 0 and 255, also it cannot have units.

Below is the formula syntax that will do the conversion.

$i=$signal.ceiling().setUnits('')
 

$div1=($i/2).floor()

$div2=($div1/2).floor()
$div3=($div2/2).floor()
$div4=($div3/2).floor()
$div5=($div4/2).floor()
$div6=($div5/2).floor()
$div7=($div6/2).floor()
$div8=($div7/2).floor()

 

$rem1=($i/2-$div1).ceiling().toString()
$rem2=($div1/2-$div2).ceiling().toString()
$rem3=($div2/2-$div3).ceiling().toString()
$rem4=($div3/2-$div4).ceiling().toString()
$rem5=($div4/2-$div5).ceiling().toString()
$rem6=($div5/2-$div6).ceiling().toString()
$rem7=($div6/2-$div7).ceiling().toString()
$rem8=($div7/2-$div8).ceiling().toString()

 

$rem8+$rem7+$rem6+$rem5+$rem4+$rem3+$rem2+$rem1

 

Below is a screenshot of the syntax in the formula tool.

Capture1.PNG

Result:

Capture2.PNG

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...