cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
Liranlev
Level II

How to convert decimal value to binary value in another column?

How to convert decimal value to binary value in another column?

8 REPLIES 8
MRB3855
Super User

Re: How to convert decimal value to binary value in another column?

Hi @Liranlev : Here ya go (you can choose whatever base you like in the Hex function).

 Hex( X, base( 2 ) ), where X is the decimal value.

Liranlev
Level II

Re: How to convert decimal value to binary value in another column?

OK
Let's say for example now I have a column with value "1011"
Do you have any idea how can I seperate it to 4 columns?
1 0 1 1

jthi
Super User

Re: How to convert decimal value to binary value in another column?

Here is one suggestion (create new column which you can use with Text to Columns as it doesn't allow using "" as limiter)

Names Default To Here(1);

dt = New Table("Untitled",
	Add Rows(1),
	Compress File When Saved(1),
	New Column("Column 1", Character, "Nominal", Set Values({"1011"}))
);

dt << new column("Separated", Character, "Nominal", Formula(
	Concat Items(Words(:Column 1, ""), "|")
));

dt << Text to Columns(columns(:Separated), Delimiters("|"));

jthi_0-1717061447151.png

Allow the Text to Columns() function to specify an empty delimiter 

-Jarmo
Liranlev
Level II

Re: How to convert decimal value to binary value in another column?

It's great but I have some problem.

1. It should open new columns in the table I work in.

2. How can it take values from specific column instead of writing "1011"?

jthi
Super User

Re: How to convert decimal value to binary value in another column?

I'm not sure what you mean with either of those.

  1. "Open new columns"? you open tables, not columns in JMP. If you wish to refer columns, what is "new" column?
  2. 1011 isn't written there to anywhere else besides column which is used for the formula reference. So create formula column using your column and it should work.

To learn JMP scripting Scripting Guide (jmp.com)

-Jarmo
MRB3855
Super User

Re: How to convert decimal value to binary value in another column?

Hi @Liranlev: Word( 1, Y, "" ) will give the first digit,  Word( 2, Y, "" ), will give the 2nd, etc., where Y is the binary representation. 

Liranlev
Level II

Re: How to convert decimal value to binary value in another column?

But I don't know how long my binary number. I should use loop

txnelson
Super User

Re: How to convert decimal value to binary value in another column?

There is no need for a loop.....Taking @jthi example and adding a second row to his example table, one can see that it will add as many columns as found.

txnelson_0-1717183463526.png

Now the additional issue that one might want the binary columns to  line up and have the same number of columns generated.  Here is a modification that accomplishes that

txnelson_1-1717184598897.png

Names Default To Here( 1 );

dt = New Table( "Untitled",
	Add Rows( 1 ),
	Compress File When Saved( 1 ),
	New Column( "Column 1", Character, "Nominal", Set Values( {"1011", "11010110"} ) )
);

max length = Col Max( Length( :Column 1 ) );
fill = Repeat( "0", max length );

For Each Row(
	:Column 1 = Substr( fill, 1, max length - Length( :Column 1 ) ) || :Column 1
);

dt << New Column( "Separated",
	Character,
	"Nominal",
	Formula( Concat Items( Words( :Column 1, "" ), "|" ) )
);

dt << Text to Columns( columns( :Separated ), Delimiters( "|" ) );

 

 

Jim