ToBinary string manipulation

Post here to discuss any new features, components, chips, etc, that you would like to see in Flowcode.
Post Reply
RGV250
Posts: 432
http://meble-kuchenne.info.pl
Joined: Sat Mar 19, 2022 4:53 pm
Has thanked: 43 times
Been thanked: 44 times

ToBinary string manipulation

Post 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

mnfisher
Valued Contributor
Posts: 1912
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 155 times
Been thanked: 899 times

Re: ToBinary string manipulation

Post 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

RGV250
Posts: 432
Joined: Sat Mar 19, 2022 4:53 pm
Has thanked: 43 times
Been thanked: 44 times

Re: ToBinary string manipulation

Post 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

mnfisher
Valued Contributor
Posts: 1912
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 155 times
Been thanked: 899 times

Re: ToBinary string manipulation

Post 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
Attachments
ToBinary.fcfx
(9.42 KiB) Downloaded 13 times

mnfisher
Valued Contributor
Posts: 1912
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 155 times
Been thanked: 899 times

Re: ToBinary string manipulation

Post 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
Attachments
ToBinary.fcfx
(11.6 KiB) Downloaded 15 times

RGV250
Posts: 432
Joined: Sat Mar 19, 2022 4:53 pm
Has thanked: 43 times
Been thanked: 44 times

Re: ToBinary string manipulation

Post 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
ToBinary.jpg (14.8 KiB) Viewed 73 times
Regards,
Bob

mnfisher
Valued Contributor
Posts: 1912
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 155 times
Been thanked: 899 times

Re: ToBinary string manipulation

Post 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

RGV250
Posts: 432
Joined: Sat Mar 19, 2022 4:53 pm
Has thanked: 43 times
Been thanked: 44 times

Re: ToBinary string manipulation

Post 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

Post Reply