[OpenSCAD] How to detect chr(0)

Andrew Plumb andrew at plumb.org
Sat Apr 18 17:52:18 EDT 2015


Here’s an alternate asc() function which trades off ‘infinite’ recursion range for a faster let/search approach on a (overridable) static array:

function asc(ch,chrint=[for(i=[0:255]) [chr(i),i]]) = let(match=chrint[ search([ch],chrint,1)[0] ] )
    (len(match)==2)?match[1]:undef;


> On Apr 18, 2015, at 1:55 PM, nop head <nop.head at gmail.com> 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 at brownsbrain.com <mailto:paul at brownsbrain.com>> wrote:
> nophead wrote
> > If you just want a string of numbers, why not just use a vector? It won't
> > be 8 bits, it will be integers stored as floats, but as you can see
> > characters aren't really 8 bits either.
> >
> > The only difference I can see would be if you do maths on the values but
> > if
> > do it all modulo 256 with the % operator and floor divisions, etc, you
> > should be able to emulate 8 bits.
> 
> Yup I could most likely do it with the % operator. I'm still new to openSCAd
> so I just chose a way and decided to stick with it until either it worked or
> I decided it couldn't be done.
> 
> I put my code up on thingiverse (http://www.thingiverse.com/thing:779394 <http://www.thingiverse.com/thing:779394>)
> because the end result of it is meant to be a 3D printed object. Anybody
> interested in what I did should take a look.
> 
> I will look into the modulo 256 stuff next. Maybe I'll redo it all and end
> up with a much shorter listing.
> 
> thanks again...
> 
> 
> 
> --
> View this message in context: http://forum.openscad.org/How-to-detect-chr-0-tp12351p12417.html <http://forum.openscad.org/How-to-detect-chr-0-tp12351p12417.html>
> Sent from the OpenSCAD mailing list archive at Nabble.com.
> 
> _______________________________________________
> OpenSCAD mailing list
> Discuss at lists.openscad.org <mailto:Discuss at lists.openscad.org>
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org <http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org>
> 
> _______________________________________________
> OpenSCAD mailing list
> Discuss at lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20150418/e2808be5/attachment-0002.html>


More information about the Discuss mailing list