[OpenSCAD] How to detect chr(0)

doug moen doug at moens.org
Fri Apr 17 08:09:27 EDT 2015


HI Fantome. In OpenSCAD, a string is an array of Unicode "code points", it
isn't an array of bytes. Each code point in a string has a value between 1
and 0x10FFFF, except that some code point values are invalid. len(string)
is the number of code points in the string, and str[i] is the i'th code
point.

Torsten told you that internally, strings are represented in UTF-8 format.
But there are no operations in OpenSCAD that allow you to access that
internal representation. Numbers are represented internally as floating
point, but you can't access the underlying bit and byte patterns in a
float, either.

Your test program contains the word ASCII several times, but that's
misleading, you don't have access to the underlying byte representation.

On 16 April 2015 at 23:20, Fantome <paul at brownsbrain.com> wrote:

> tp3 wrote
> > The chr() function is supposed to return valid unicode characters in
> utf-8
> > encoding, so for example "echo(chr(128512));" is valid and should give
> the
> > smiley character U+1F600.
> >
> > Invalid input values will return an empty string, chr(0) is always
> treated
> > as invalid.
> >
> > Note: This means that asking for chr(255) will internally result in the
> > bytes 0xc3 0xbf and not 0xff. As len() is also utf-8 aware, len(chr(255))
> > still returns 1 as it's one character.
> >
> > ciao,
> >    Torsten.
> >
> > _______________________________________________
>
> Hello and thanks for the input. I'm not sure what you mean by 'internally
> result' but here's an example that shows that chr(255) does return a hex
> value of 0xFF.
>
>        the_text = chr(255);
>
>        echo("number of characters = ",len(the_text));
>        for (i=[0:len(the_text)-1])
>        {
>            for (ascii_v = [0:255])
>            {
>                if (the_text[i] == chr(ascii_v)) :
>                {
>                        if ((ascii_v) == 255):
>                        {
>                                xFF(count);
>                         }
>                         echo("character: ",the_text[i]);
>                         echo("ASCII value:", ascii_v);  // print the ascii
> value being checked
>                 }
>            }
>        }
>
> ECHO: "number of characters = ", 1
> ECHO: "character: ", "y"
> ECHO: "ASCII value:", 255
>
> Note that the character that prints like a y with two dots above it has the
> hex value of 0xFF, not 0xC3 .0xBF.
>
>
>
>
> --
> View this message in context:
> http://forum.openscad.org/How-to-detect-chr-0-tp12351p12396.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> 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/20150417/4fba1560/attachment-0002.html>


More information about the Discuss mailing list