Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Determine If Variable Is a Binary String 1

Status
Not open for further replies.

bgong

Programmer
May 8, 2006
3
US
Does anyone know how to determine if a variable that is passed in as an argument in a procedure is a binary string? The [string is .. ] command does not seem to cover this data type.
 
Do you mean "does the input string contain only 1's and 0's"?
If that's what you mean, regexp is probably what you want:
regexp {^[0-1]} <your string>

_________________
Bob Rashkin
 
No, I'm referring to a binary string that's created with the 'binary format' command, which creates an array of binary data.
 
Then you can use the binary scan function and catch the return. If it's 0, you're good.

_________________
Bob Rashkin
 
Maybe it's 1 that means you're good, as a return from catch. I guess it depends on what you want to see.

_________________
Bob Rashkin
 
It doesn't look like 'binary scan' will do the trick, as the
following example shows that binary scan will return 1
whether the variable is a binary string or a list of integers.

% binary scan [binary format c* {0 1 2}] c* a
1
% set a
0 1 2
% binary scan {0 1 2} c* a
1
% set a
48 32 49 32 50

But you've given me an idea. All I really want to do is
differentiate between a binary string and a list of integers,
so I can catch the return on format %d:

% format %d [lindex {0 1 2} 0]
0
%format %d [lindex [binary format c* {0 1 2}] 0]
expected integer but got "??"

Thanks for the input.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top