01-13-2014 06:06 PM
Hi,, how can I convert a string constant containing 1 and 0 to 1 and zero
for example if the string is 1010000 then the it should be converted to 1010000 in (0,1) format. Somthing like Boolean array to (0,1) block
Thanks
01-13-2014 06:14 PM
Try the Scan From String function with "%b" as the 'format string' input.
01-13-2014 06:33 PM - edited 01-13-2014 06:38 PM
Hello Tintin_99,
I'd be surprised if there wasn't a function for this in the MGI or OpenG libraries, but as I don't have access to those at the moment to check here's a quick and dirty solution. It's probably not the most efficient way to do it.
-edit-
As Darren indicated, Scan from String with a Boolean format specifier is the straightforward and easy to interpret later way- specifying a width of 1 and building an array out of the resulting outputs will give you the array of zeroes and ones (this is what I did in that screen capture).
Regards,
01-13-2014 06:44 PM - edited 01-13-2014 06:49 PM
tintin_99 wrote:for example if the string is 1010000 then the it should be converted to 1010000 in (0,1) format. Somthing like Boolean array to (0,1) block
You need to be more specific, because (0,1) is not a format.
01-13-2014 06:52 PM - edited 01-13-2014 06:53 PM
If you know the string is clean (only 0 or 1, no weird characters), you can do the following.
01-14-2014 06:40 AM - edited 01-14-2014 06:41 AM
This is a rarity, so I have to jump on it.
Christian's code can be simplified by subtracting 48 instead of comparing. Again, this assumes that your string is nothing but ASCII 0 and 1.
01-14-2014 09:36 AM - edited 01-14-2014 09:38 AM
@crossrulz wrote:
This is a rarity, so I have to jump on it.
Well, let me jump a little higher then. 😄
Why do math at all (yes, subtraction is hardcore math!) if all we really need is some cheap bitwise operation??? 😮