discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Re: [OpenSCAD] How to detect chr(0)

F
Fantome
Sat, Apr 18, 2015 10:58 PM

Wow, I can see how that can reduce the number of lines of code. I will most
likely update my code. Thanks for the tips and example.

nophead wrote

I see now that your input is a string and you basically want to convert it
to binary. Yes it can be done with much less code. Here is a simple
example:

function testbit(n, x) = (floor(x / pow(2,n)) % 2) == 1;

function binary(x, n = 7) = n >= 0 ? str((testbit(n, x) ? "1" : "0"),
binary(x, n - 1)) : "";

function asc(c, n = 0) = c == chr(n) ? n : asc(c, n + 1);

text = "ABCD";

for(i = [0 : len(text)-1])
echo(text[i], binary(asc(text[i])));

ECHO: "A", "01000001"

ECHO: "B", "01000010"

ECHO: "C", "01000011"

ECHO: "D", "01000100"

Not very speed efficient as OpenScad seems to lack asc();

On 18 April 2015 at 18:14, Fantome <

paul@

> wrote:

--
View this message in context: http://forum.openscad.org/How-to-detect-chr-0-tp12351p12420.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Wow, I can see how that can reduce the number of lines of code. I will most likely update my code. Thanks for the tips and example. nophead wrote > I see now that your input is a string and you basically want to convert it > to binary. Yes it can be done with much less code. Here is a simple > example: > > function testbit(n, x) = (floor(x / pow(2,n)) % 2) == 1; > > function binary(x, n = 7) = n >= 0 ? str((testbit(n, x) ? "1" : "0"), > binary(x, n - 1)) : ""; > > function asc(c, n = 0) = c == chr(n) ? n : asc(c, n + 1); > > text = "ABCD"; > > for(i = [0 : len(text)-1]) > echo(text[i], binary(asc(text[i]))); > > > ECHO: "A", "01000001" > > ECHO: "B", "01000010" > > ECHO: "C", "01000011" > > ECHO: "D", "01000100" > > > Not very speed efficient as OpenScad seems to lack asc(); > > > On 18 April 2015 at 18:14, Fantome &lt; > paul@ > &gt; wrote: -- View this message in context: http://forum.openscad.org/How-to-detect-chr-0-tp12351p12420.html Sent from the OpenSCAD mailing list archive at Nabble.com.