cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar

Trying to Check if a number is in Binary format

Hey all,

 

I'm trying to write a script that takes an input and encrypts it with a cipher. In order to do that, the input text has to be in binary. I asked ChatGPT for help and it recommende

if ( Matches Reg Expr(input, "^[01]+$"),
     Show("Valid"),
     Show("Not Valid"));

That doesn't work, and I'm trying to figure out why. I'm also trying to figure out a way to check if a number is in binary, so any help would be greatly appreciated

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: Trying to Check if a number is in Binary format

Lost a line at the top:

CheckIfBinary = Function( {input},
	{isbinary},
	If( 	//If the string only contains 0 and 1 and is at least 1 character long
		!Is Missing( Regex( input, "^[01]+$" ) )
	, 	//then return 1
		isbinary = 1
	, 	//otherwise return 0
		isbinary = 0
	);
	Return( isbinary );
);

CheckIfBinary( "123" );
CheckIfBinary( "10" );

I've avoided ChatGPT's auto complete until just now. That's interesting...it found the right regex anyway.

 

@LatentHyena653 - JMP's regex returns a missing value for "no match" and the text of the match otherwise. Missing value testing must be done with the isMissing function. Most other languages will have different details for using regex, so it isn't too surprising.

 

Also, JMP has a hex() function that will convert text to a hex string:

hexData = hex("ABC");
binaryData = substitute(hexData,
	"0","0000",
	"1","0001",
	"2","0010",
	"3","0011",
	"4","0100",
	"5","0101",
	"6","0110",
	"7","0111",
	"8","1000",
	"9","1001",
	"A","1010",
	"B","1011",
	"C","1100",
	"D","1101",
	"E","1110",
	"F","1111",
	"????"
);
show(hexData,binaryData);
// hexData = "414243";
// binaryData = "010000010100001001000011";

And, as shown, substitute() can convert the hex string to a binary string.

 

Finally, notice that the binary string of 1s and 0s is actually a character string representation of binary data. JMP also has a blob data type if you need to work with actual binary data. But it is a lot easier to work with printable 1s and 0s.

Craige

View solution in original post

3 REPLIES 3
ih
Super User (Alumni) ih
Super User (Alumni)

Re: Trying to Check if a number is in Binary format

This should work:

	If(	//If the string only contains 0 and 1 and is at least 1 character long
		!is missing(regex( input, "^[01]+$" ))
	,	//then return 1
		isbinary= 1
	,	//otherwise return 0
		isbinary=0
	);
	return(isbinary);
);

CheckIfBinary("123");
CheckIfBinary("10");
Craige_Hales
Super User

Re: Trying to Check if a number is in Binary format

Lost a line at the top:

CheckIfBinary = Function( {input},
	{isbinary},
	If( 	//If the string only contains 0 and 1 and is at least 1 character long
		!Is Missing( Regex( input, "^[01]+$" ) )
	, 	//then return 1
		isbinary = 1
	, 	//otherwise return 0
		isbinary = 0
	);
	Return( isbinary );
);

CheckIfBinary( "123" );
CheckIfBinary( "10" );

I've avoided ChatGPT's auto complete until just now. That's interesting...it found the right regex anyway.

 

@LatentHyena653 - JMP's regex returns a missing value for "no match" and the text of the match otherwise. Missing value testing must be done with the isMissing function. Most other languages will have different details for using regex, so it isn't too surprising.

 

Also, JMP has a hex() function that will convert text to a hex string:

hexData = hex("ABC");
binaryData = substitute(hexData,
	"0","0000",
	"1","0001",
	"2","0010",
	"3","0011",
	"4","0100",
	"5","0101",
	"6","0110",
	"7","0111",
	"8","1000",
	"9","1001",
	"A","1010",
	"B","1011",
	"C","1100",
	"D","1101",
	"E","1110",
	"F","1111",
	"????"
);
show(hexData,binaryData);
// hexData = "414243";
// binaryData = "010000010100001001000011";

And, as shown, substitute() can convert the hex string to a binary string.

 

Finally, notice that the binary string of 1s and 0s is actually a character string representation of binary data. JMP also has a blob data type if you need to work with actual binary data. But it is a lot easier to work with printable 1s and 0s.

Craige
ih
Super User (Alumni) ih
Super User (Alumni)

Re: Trying to Check if a number is in Binary format

Thank you for catching that @Craige_Hales !