# [OpenSCAD] How to detect chr(0)

Sat Apr 18 13:55:41 EDT 2015

```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> 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
> 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)
> 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:
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________