cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • JMP will suspend normal business operations for our Winter Holiday beginning on Wednesday, Dec. 24, 2025, at 5:00 p.m. ET (2:00 p.m. ET for JMP Accounts Receivable).
    Regular business hours will resume at 9:00 a.m. EST on Friday, Jan. 2, 2026.
  • We’re retiring the File Exchange at the end of this year. The JMP Marketplace is now your destination for add-ins and extensions.

Discussions

Solve problems, and share tips and tricks with other JMP users.
%3CLINGO-SUB%20id%3D%22lingo-sub-4841%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%B0%86%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%BA%8C%E8%BF%9B%E5%88%B6%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-4841%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CP%3E%E6%88%91%E9%9C%80%E8%A6%81%E5%B0%86%E6%95%B0%E6%8D%AE%E5%88%97%EF%BC%88%E5%8D%81%E8%BF%9B%E5%88%B6%EF%BC%89%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%BA%8C%E8%BF%9B%E5%88%B6%EF%BC%888%20%E4%BD%8D%EF%BC%89%EF%BC%8C%E7%84%B6%E5%90%8E%E5%B0%86%204%20LSB%20%E8%BF%9E%E6%8E%A5%E5%88%B0%E5%85%B7%E6%9C%89%204%20MSB%20%E7%9A%84%E4%B8%8B%E4%B8%80%E5%88%97%E6%95%B0%E6%8D%AE%E3%80%82%E6%9C%89%E6%B2%A1%E6%9C%89%E5%8A%9E%E6%B3%95%E5%9C%A8%20JMP%20%E4%B8%AD%E8%87%B3%E5%B0%91%E5%B0%86%E5%8D%81%E8%BF%9B%E5%88%B6%E6%95%B0%E6%8D%AE%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%BA%8C%E8%BF%9B%E5%88%B6%EF%BC%9F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-50300%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E5%B0%86%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%BA%8C%E8%BF%9B%E5%88%B6%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-50300%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%E5%A6%82%E5%89%8D%E6%89%80%E8%BF%B0%EF%BC%9A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%E5%97%A8%20-%20%E4%BD%A0%E8%83%BD%E8%B0%83%E6%95%B4%E4%BB%A5%E4%B8%8B%E5%86%85%E5%AE%B9%E6%9D%A5%E5%81%9A%E4%BD%A0%E9%9C%80%E8%A6%81%E7%9A%84%E5%90%97%EF%BC%9F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%3CSPAN%3E%2F%2F%20Function%20to%20convert%20an%20integer%20into%20any%20positive%20base%20up%20to%20hexadecimal%3B%3C%2FSPAN%3E%3CBR%20%2F%3Ebase%20%3D%20Function(%20%7Bn%2C%20nbase%7D%2C%20%7Bdefault%20local%7D%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ndash%20%3D%20%22%22%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20digits%3D%20%220123456789ABCDEF%22%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20While(%20n%20%26gt%3B%200%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20remainder%3D%20Mod(%20n%2C%20nbase%20)%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20newdigit%3D%20Substr(%20digits%2C%20(1%20%2B%20remainder)%2C%201%20)%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ndash%3D%20newdigit%20%7C%7C%20ndash%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20n%3D%20(n%20-%20remainder)%2F%20nbase%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20)%3B%0Andash%3B%0A)%3B%0A%26nbsp%3B%0A%2F%2F%20Print%20out%20a%20few%20numbers%20to%20show%20it%20working%20in%20binary%3B%0A%26nbsp%3B%0AFor(%20i%20%3D%201%2C%20i%20%26lt%3B%3D%2032%2C%20i%2B%2B%2C%20Show(%20Char(%20i%20)%20%7C%7C%20%22%20%3D%20%22%20%7C%7C%20base(%20i%2C%202%20)%20)%20)%3B%0A%26nbsp%3B%0A%2F%2F%20Or%20create%20some%20new%20columns%20in%20a%20data%20table%20and%20fill%20them%3B%0A%26nbsp%3B%0Adt%20%3D%20New%20Table(%20%22Number%20Bases%22%2C%20New%20Column(%20%22N%22%2C%20numeric%2C%20values(%20Index(%201%2C%2032%20)%20)%20)%20)%3B%0A%26nbsp%3B%0Anumberbase%20%3D%20%7B2%2C%203%2C%208%2C%2010%2C%2012%2C%2016%7D%3B%20%2F%2F%20Some%20familiar%20number%20bases%3B%0A%26nbsp%3B%0AFor(%20i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Items(%20numberbase%20)%2C%20i%2B%2B%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20k%3D%20numberbase%5Bi%5D%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20dt%26lt%3B%26lt%3B%20New%20Column(%20%22Base%20%22%7C%7C%20Char(%20k%20)%2C%20character%20)%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20For(%20j%20%3D%201%2C%20j%20%26lt%3B%3D%20N%20Row(%20dt%20)%2C%20j%2B%2B%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Column(%20dt%2C%20(%22Base%20%22%7C%7C%20Char(%20k%20))%20)%5Bj%5D%20%3D%20base(%20j%2C%20k%20)%20)%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20)%3B%0A%26nbsp%3B%0A%2F%2F%20End%20of%20program%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%E5%92%8C%EF%BC%9A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%E6%B2%A1%E9%97%AE%E9%A2%98%20-%20%E9%A2%9D%E5%A4%96%E7%9A%84%E2%80%9Cif%E2%80%9D%E5%AD%90%E5%8F%A5%E4%BC%9A%E5%B0%86%20n%3D0%20%E8%A7%86%E4%B8%BA%E7%89%B9%E6%AE%8A%E6%83%85%E5%86%B5%EF%BC%9A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Ebase%20%3D%20function(%7Bn%2C%20base%7D%2C%20%7Bdefault%20local%7D%2C%20ndash%20%3D%20%22%22%3B%20digits%20%3D%20%220123456789ABCDEF%22%3B%0Aif(n%20%26gt%3B%200%2C%20while(n%20%26gt%3B%200%2C%20remainder%20%3D%20mod(n%2C%20base)%3B%20newdigit%20%3D%20substr(digits%2C%20(1%20%2B%20remainder)%2C%201)%3B%0Andash%20%3D%20newdigit%20%7C%7C%20ndash%3B%20n%20%3D%20(n%20-%20remainder)%20%2F%20base)%2C%20ndash%20%3D%20%220%22)%3B%20ndash)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%EF%BC%88%E6%88%91%E5%81%87%E8%AE%BE%20n%20%E6%98%AF%E4%B8%80%E4%B8%AA%E9%9D%9E%E8%B4%9F%E6%95%B4%E6%95%B0%EF%BC%8C%E8%80%8C%20base%20%E6%98%AF%E4%B8%80%E4%B8%AA%E6%AD%A3%E6%95%B4%E6%95%B0%E3%80%82%EF%BC%89%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
justvince
Level III

Convert decimal to Binary

I need to convert Columns of data (decimal) to binary (8 bit) , then concantenate say the 4 LSB to the next column of data with the 4 MSB.  Is there a way to at least convert decimal data to binary in JMP? 

1 ACCEPTED SOLUTION

Accepted Solutions
Duane_Hayes
Staff (Retired)

Re: Convert decimal to Binary

As previously stated:

 

Hi - can you adapt the following to do what you need?

 

 

// Function to convert an integer into any positive base up to hexadecimal;
base = Function( {n, nbase}, {default local},        ndash = "";        digits= "0123456789ABCDEF";      While( n > 0,               remainder= Mod( n, nbase );               newdigit= Substr( digits, (1 + remainder), 1 );               ndash= newdigit || ndash;               n= (n - remainder)/ nbase;        ); ndash; );   // Print out a few numbers to show it working in binary;   For( i = 1, i <= 32, i++, Show( Char( i ) || " = " || base( i, 2 ) ) );   // Or create some new columns in a data table and fill them;   dt = New Table( "Number Bases", New Column( "N", numeric, values( Index( 1, 32 ) ) ) );   numberbase = {2, 3, 8, 10, 12, 16}; // Some familiar number bases;   For( i = 1, i <= N Items( numberbase ), i++,      k= numberbase[i];      dt<< New Column( "Base "|| Char( k ), character );      For( j = 1, j <= N Row( dt ), j++,      Column( dt, ("Base "|| Char( k )) )[j] = base( j, k ) );      );   // End of program;

 

 

And:

 

No problem - an extra "if" clause will treat n=0 as a special case:

 

 

base = function({n, base}, {default local}, ndash = ""; digits = "0123456789ABCDEF";
if(n > 0, while(n > 0, remainder = mod(n, base); newdigit = substr(digits, (1 + remainder), 1);
ndash = newdigit || ndash; n = (n - remainder) / base), ndash = "0"); ndash);

 

 

(I'm assuming that n is a non-negative integer, and that base is a positive integer.)

Duane Hayes

View solution in original post

4 REPLIES 4

Re: Convert decimal to Binary

Hi - can you adapt the following to do what you need?

// Function to convert an integer into any positive base up to hexadecimal;

base = Function( {n, nbase}, {default local},

       ndash = "";

       digits= "0123456789ABCDEF";

     While( n > 0,

              remainder= Mod( n, nbase );

              newdigit= Substr( digits, (1 + remainder), 1 );

              ndash= newdigit || ndash;

              n= (n - remainder)/ nbase;

       );

ndash;

);

// Print out a few numbers to show it working in binary;

For( i = 1, i <= 32, i++, Show( Char( i ) || " = " || base( i, 2 ) ) );

// Or create some new columns in a data table and fill them;

dt = New Table( "Number Bases", New Column( "N", numeric, values( Index( 1, 32 ) ) ) );

numberbase = {2, 3, 8, 10, 12, 16}; // Some familiar number bases;

For( i = 1, i <= N Items( numberbase ), i++,

     k= numberbase[i];

     dt<< New Column( "Base "|| Char( k ), character );

     For( j = 1, j <= N Row( dt ), j++,

     Column( dt, ("Base "|| Char( k )) )[j] = base( j, k ) );

     );

// End of program;

justvince
Level III

Re: Convert decimal to Binary

It is a start.  But it appears the function base will not convert a zero to binary 0,

Re: Convert decimal to Binary

No problem - an extra "if" clause will treat n=0 as a special case:

base = function({n, base}, {default local}, ndash = ""; digits = "0123456789ABCDEF";

if(n > 0, while(n > 0, remainder = mod(n, base); newdigit = substr(digits, (1 + remainder), 1);

ndash = newdigit || ndash; n = (n - remainder) / base), ndash = "0"); ndash);

(I'm assuming that n is a non-negative integer, and that base is a positive integer.)

Duane_Hayes
Staff (Retired)

Re: Convert decimal to Binary

As previously stated:

 

Hi - can you adapt the following to do what you need?

 

 

// Function to convert an integer into any positive base up to hexadecimal;
base = Function( {n, nbase}, {default local},        ndash = "";        digits= "0123456789ABCDEF";      While( n > 0,               remainder= Mod( n, nbase );               newdigit= Substr( digits, (1 + remainder), 1 );               ndash= newdigit || ndash;               n= (n - remainder)/ nbase;        ); ndash; );   // Print out a few numbers to show it working in binary;   For( i = 1, i <= 32, i++, Show( Char( i ) || " = " || base( i, 2 ) ) );   // Or create some new columns in a data table and fill them;   dt = New Table( "Number Bases", New Column( "N", numeric, values( Index( 1, 32 ) ) ) );   numberbase = {2, 3, 8, 10, 12, 16}; // Some familiar number bases;   For( i = 1, i <= N Items( numberbase ), i++,      k= numberbase[i];      dt<< New Column( "Base "|| Char( k ), character );      For( j = 1, j <= N Row( dt ), j++,      Column( dt, ("Base "|| Char( k )) )[j] = base( j, k ) );      );   // End of program;

 

 

And:

 

No problem - an extra "if" clause will treat n=0 as a special case:

 

 

base = function({n, base}, {default local}, ndash = ""; digits = "0123456789ABCDEF";
if(n > 0, while(n > 0, remainder = mod(n, base); newdigit = substr(digits, (1 + remainder), 1);
ndash = newdigit || ndash; n = (n - remainder) / base), ndash = "0"); ndash);

 

 

(I'm assuming that n is a non-negative integer, and that base is a positive integer.)

Duane Hayes

Recommended Articles