Ok, I don't know if I got the subject right, but I saw a Binary calculation party going on here and I couldn't resist crashing in

How's about this?:

Options()

Action:

Converts a decimal number to a binary option string or
converts a binary option string to a decimal number or
lists the selected options.

Syntax:

Options(Value, Mode, CheckValue)

Parameters:

Value: This can be a decimal or a binary string, this depends on the Mode.

Mode: 0 = Convert decimal number to binary string
1 = Convert binary string to decimal number

CheckValue: If you're not sure of your value, set this value to 1 (true) and
you'll get a string returned which sums up the selected options.

Returns:

A decimal number or binary string or a list of selected options, depending on the mode
and the checkvalue.

Remarks:

Be careful of big numbers (Kix gets confused if you use a decimal number bigger
than 2,147,483,647)
Dependecies:

KiXtart 4.0 (Final)

Examples:

$test = Options(7,0); will return '111'
$test = Options(6,0); will return '011'

$test = Options(10001,1); will return '17'

$test = Options(10001,1,1); will return 'selected options: 1, 16'

$test = Options(13,0,1); will return 'selected options: 1, 4, 8'

$test = Options(2 + 16 + 1024,0); will return '01001000001'

Source:

code:

Function Options($_value, $_mode, optional $_test)

DIM $_result, $_ttxt

SELECT
CASE $_mode=0
$_value=val($_value) $_i=1
While $_value => $_i * 2
$_i = $_i * 2
Loop
While $_i > 0
if $_value => $_i
If $_test $_ttxt = ', $_i' + $_ttxt Endif
$_result = '1' + $_result
$_value = $_value - $_i
else
$_result='0'+$_result
endif
$_i = $_i / 2
Loop

CASE $_mode = 1
$_i = 1 $_result = 0
For $_p = 1 to len($_value)
if substr($_value,$_p,1)='1'
If $_test $_ttxt = $_ttxt + ', $_i' Endif
$_result = $_result + $_i
endif
$_i = $_i * 2
Next
ENDSELECT

if $_test
$Options = "Selected Options: "+Right($_ttxt,len($_ttxt)-2)
else
$Options = $_result
endif

EndFunction


Author(s):

DrillSergeant

_________________________
The Code is out there