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
ToBinary string manipulation
-
RGV250
- Posts: 433
- http://meble-kuchenne.info.pl
- Joined: Sat Mar 19, 2022 4:53 pm
- Has thanked: 43 times
- Been thanked: 44 times
-
mnfisher
- Valued Contributor
- Posts: 1913
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 155 times
- Been thanked: 900 times
Re: ToBinary string manipulation
Adding a ToBinary function is an interesting programming exercise:
In effect you need (in pseudocode here):
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
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 = .resMartin
Re: ToBinary string manipulation
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
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: 1913
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 155 times
- Been thanked: 900 times
Re: ToBinary string manipulation
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
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 22 times
-
mnfisher
- Valued Contributor
- Posts: 1913
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 155 times
- Been thanked: 900 times
Re: ToBinary string manipulation
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
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 24 times
Re: ToBinary string manipulation
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.
Regards,
Bob
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.
Regards,
Bob
-
mnfisher
- Valued Contributor
- Posts: 1913
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 155 times
- Been thanked: 900 times
Re: ToBinary string manipulation
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
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
Martin
Re: ToBinary string manipulation
Hi,
Bob
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.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.
Bob
-
mnfisher
- Valued Contributor
- Posts: 1913
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 155 times
- Been thanked: 900 times
Re: ToBinary string manipulation
I moved this into a small component (I called it NumberUtilities - maybe more routines to come?) The source is still called ToBinary 
I added a Value and ToString macros that take either a string and a base or a value and a base. Base can be from 2 (binary) up to 16 (hex) - although could be extended. If adding bases (base 60 anyone) - then add the appropriate characters to digits (and amend Value)
These follow the more usual convention of removing the leading 0s (I assumed they were required for the binary macros). There is a convention with numbers and leading 0s - in C - would imply an octal constant. Hex would have 0x and binary 0b as prefixes. I haven't handled them here. Any thoughts - could pass a parameter 'add prefix' or have a macro UsePrefix(true/false).
Negative numbers (and floats) - are not handled as such.
I notice that Flowcode displays hex in lower case (in - change the constant 'digits' - to reflect this if required..
Martin
I added a Value and ToString macros that take either a string and a base or a value and a base. Base can be from 2 (binary) up to 16 (hex) - although could be extended. If adding bases (base 60 anyone) - then add the appropriate characters to digits (and amend Value)
These follow the more usual convention of removing the leading 0s (I assumed they were required for the binary macros). There is a convention with numbers and leading 0s - in C - would imply an octal constant. Hex would have 0x and binary 0b as prefixes. I haven't handled them here. Any thoughts - could pass a parameter 'add prefix' or have a macro UsePrefix(true/false).
Negative numbers (and floats) - are not handled as such.
I notice that Flowcode displays hex in lower case (in - change the constant 'digits' - to reflect this if required..
Martin
- Attachments
-
- ToBinary.fcfx
- (17.87 KiB) Downloaded 10 times
-
- NumberUtilities.fcpx
- (2.16 KiB) Downloaded 11 times