Page 1 of 1
ToBinary string manipulation
Posted: Sat Apr 11, 2026 11:47 am
by RGV250
Hi,
I was looking at ANDing 2 INT's and in the end it was easier than I thought. What would have helped is if I could have seen the values as binary while simulating.
In another compiler I use there is a function where I can print to display or sent to serial port the number in binary which I find very useful.
In FC there seemed to be all sorts of string manipulations apart from the one I wanted IE to see the binary representation of the variable.
Regards,
Bob
Re: ToBinary string manipulation
Posted: Sat Apr 11, 2026 1:38 pm
by mnfisher
Adding a ToBinary function is an interesting programming exercise:
In effect you need (in pseudocode here):
Code: Select all
ToBinary(long unsigned x, byte digits) return string// Take a number (up to 32 bits) and convert to a string
locals byte i = 0, string res[33]
loop while .i < .digits
res[.i] = ((.x & (1 << (31 - .i))) != 0) + '0' // Convert bit to 0 or 1 (+ '0' to convert to ASCII character) or use an if...
.i = .i + 1
end while
res[.i] = '\0' // Mark end of string
.Result = .res
Then you can do .str = ToBinary(123, 8) or ToBinary(7000, 16) or ToBinary(long, 32) (Note .str needs to be large enough to hold the result - or it will be truncated)
Martin
Re: ToBinary string manipulation
Posted: Sat Apr 11, 2026 4:44 pm
by RGV250
Hi Martin,
I do not think I can use pseudocode in my licence. I did find an example that had something similar in FC so I may look at that as a macro for future use if I need it. I still think it would be a nice built in function, I use it a lot in the other compiler.
Regards,
Bob
Re: ToBinary string manipulation
Posted: Sat Apr 11, 2026 5:17 pm
by mnfisher
Pseudocode is just a way to describe an algorithm. Although FC can display code as (a) pseudocode too.
I converted ToBinary to FC - and tested in s8imulation. Here I just pass constants to the macro - although variables could also be used as the arguments.
If we added a BinaryToValue - that takes a string in binary and converts to a value - it would be easily converted to a (small) component...
I changed the code slightly - there is a small gotcha - (1 << n) only returns a 16 bit value - and as I wanted 32 bit capability - I used a separate mask.
I don't restrict the number of digits (eg 12 might be useful with 12bit ADCs)
Martin
Re: ToBinary string manipulation
Posted: Sat Apr 11, 2026 6:00 pm
by mnfisher
I added a binary string (in the form "101010101" with upto 32 characters) to value function too. In simulation I've added the various variables to the viewer so that you can compare them (ie check they are correct

)
Note that it doesn't check the validity of the string (so "10ABC" will give unexpected results. It might be better to stop at the first non-binary-digit (not '0' or '1')?
Martin
Re: ToBinary string manipulation
Posted: Sat Apr 11, 2026 7:40 pm
by RGV250
Hi Martin,
It works as I needed, I only wanted the ToBinary but I am sure the other one will come in handy later.
I wanted to have 2 sets of radio buttons, combine their values into one byte (high/low nibble) and then separate them at the other end rather than sending 2 bytes.

- ToBinary.jpg (14.8 KiB) Viewed 74 times
Regards,
Bob
Re: ToBinary string manipulation
Posted: Sat Apr 11, 2026 8:26 pm
by mnfisher
Hi Bob,
Looks like a neat solution - reduces transmission time by 50%. It's a good optimisation.
I'll have a play at putting it into a component - if you don't use a function the compiler will optimise it away (at least when it's compiled, I'm not sure what the situation with web apps is) - or as another idea, create a ToString(value, digits, base) - so that ToBinary is given by ToString(value, digits, 2) and decimal, hex and octal can all be done easily (i have created a ToHex and a FromHex many times)
In an ideal world - it would possibly check values are in range (0 < digits <= 32 for example) - or have a 'debug' version (with) and a 'production' version (without) - but I think generally I'd leave it as it is.
If you fancy some 'fun' - then creating operations on strings as numbers is an interesting exercise (although very inefficient memory-wise unless using base 256 or BCD) - e.g. "101" + "1" = "110" allowing any length strings

I'm sure it has been done for Roman numerals too!
Martin
Re: ToBinary string manipulation
Posted: Sat Apr 11, 2026 9:34 pm
by RGV250
Hi,
In an ideal world - it would possibly check values are in range (0 < digits <= 32 for example) - or have a 'debug' version (with) and a 'production' version (without) - but I think generally I'd leave it as it is.
I would say leave it as it is otherwise it over complicates the code. Getting it right before the final version is what all the debugging / testing is for anyway.
Bob