DM
doug moen
Fri, Apr 17, 2015 6:04 PM
The reason you see ASCII values is that ASCII is a proper subset of
unicode. The letter 'A' has value 65 in ASCII and also value 65 as a
Unicode code point. But OpenSCAD is still a Unicode system, not an ASCII
system, because the entire Unicode range is supported. So there's no need
to stop at chr(255), when you also have chr(256), chr(257), and so on.
If you want to represent an array of values between 0 and 255 using a
string, you can still do that, you'll just have represent 0 in a different
way: pick a different code point. Or you could add an offset, and use the
range chr(1) to chr(256) to represent the values 0 to 255. Of course, I'm
not really sure what you're trying to do, but I hope this has helped.
On 17 April 2015 at 12:29, Fantome paul@brownsbrain.com wrote:
HI Fantome. In OpenSCAD, a string is an array of Unicode "code points",
isn't an array of bytes. Each code point in a string has a value between
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
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.
Hello.
It's interesting to hear about the inner working of this. If OpenSCAD had
the ability to let users manipulate the bits the possibilities for
modelling
objects and systems would be increased. Just my opinion of course.
When OpenScad echos/prints the chr(value), what it prints matches with the
ASCII chart. This is why I named the variable ASCII_value. I don't think
it's misleading. It is the ASCII value. It works for all 255 values, value
0
is the only one that is ignored by OpenSCAD.
For my specific application I guess I'll just figure out a work around
since
it seems obvious now that OpenSCAD is treating chr(0) as a 'something' that
is not used in any comparison. The inner workings of OpenSCAD must have
checks for a NULL character or chr(0) that equates to 'jump to the next
character'. Oh well. One disappointment out of 256 values, still 99.61%
satisfaction.
--
View this message in context:
http://forum.openscad.org/How-to-detect-chr-0-tp12351p12405.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
The reason you see ASCII values is that ASCII is a proper subset of
unicode. The letter 'A' has value 65 in ASCII and also value 65 as a
Unicode code point. But OpenSCAD is still a Unicode system, not an ASCII
system, because the entire Unicode range is supported. So there's no need
to stop at chr(255), when you also have chr(256), chr(257), and so on.
If you want to represent an array of values between 0 and 255 using a
string, you can still do that, you'll just have represent 0 in a different
way: pick a different code point. Or you could add an offset, and use the
range chr(1) to chr(256) to represent the values 0 to 255. Of course, I'm
not really sure what you're trying to do, but I hope this has helped.
On 17 April 2015 at 12:29, Fantome <paul@brownsbrain.com> wrote:
> doug.moen wrote
> > 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.
>
> Hello.
> It's interesting to hear about the inner working of this. If OpenSCAD had
> the ability to let users manipulate the bits the possibilities for
> modelling
> objects and systems would be increased. Just my opinion of course.
>
> When OpenScad echos/prints the chr(value), what it prints matches with the
> ASCII chart. This is why I named the variable ASCII_value. I don't think
> it's misleading. It is the ASCII value. It works for all 255 values, value
> 0
> is the only one that is ignored by OpenSCAD.
>
> For my specific application I guess I'll just figure out a work around
> since
> it seems obvious now that OpenSCAD is treating chr(0) as a 'something' that
> is not used in any comparison. The inner workings of OpenSCAD must have
> checks for a NULL character or chr(0) that equates to 'jump to the next
> character'. Oh well. One disappointment out of 256 values, still 99.61%
> satisfaction.
>
>
>
>
> --
> View this message in context:
> http://forum.openscad.org/How-to-detect-chr-0-tp12351p12405.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
F
Fantome
Fri, Apr 17, 2015 9:37 PM
The reason you see ASCII values is that ASCII is a proper subset of
unicode. The letter 'A' has value 65 in ASCII and also value 65 as a
Unicode code point. But OpenSCAD is still a Unicode system, not an ASCII
system, because the entire Unicode range is supported. So there's no need
to stop at chr(255), when you also have chr(256), chr(257), and so on.
If you want to represent an array of values between 0 and 255 using a
string, you can still do that, you'll just have represent 0 in a different
way: pick a different code point. Or you could add an offset, and use the
range chr(1) to chr(256) to represent the values 0 to 255. Of course, I'm
not really sure what you're trying to do, but I hope this has helped.
Hey thanks! Sometimes the simple answers are the best. Since OpenSCAD is
using Unicode then yes, I can simply use chr(256) to represent a zero. I
like it.
Thanks again. Satisfaction has just increased to 100%.
--
View this message in context: http://forum.openscad.org/How-to-detect-chr-0-tp12351p12409.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
doug.moen wrote
> The reason you see ASCII values is that ASCII is a proper subset of
> unicode. The letter 'A' has value 65 in ASCII and also value 65 as a
> Unicode code point. But OpenSCAD is still a Unicode system, not an ASCII
> system, because the entire Unicode range is supported. So there's no need
> to stop at chr(255), when you also have chr(256), chr(257), and so on.
>
> If you want to represent an array of values between 0 and 255 using a
> string, you can still do that, you'll just have represent 0 in a different
> way: pick a different code point. Or you could add an offset, and use the
> range chr(1) to chr(256) to represent the values 0 to 255. Of course, I'm
> not really sure what you're trying to do, but I hope this has helped.
Hey thanks! Sometimes the simple answers are the best. Since OpenSCAD is
using Unicode then yes, I can simply use chr(256) to represent a zero. I
like it.
Thanks again. Satisfaction has just increased to 100%.
--
View this message in context: http://forum.openscad.org/How-to-detect-chr-0-tp12351p12409.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Fri, Apr 17, 2015 9:52 PM
When OpenScad echos/prints the chr(value), what it prints matches with the
ASCII chart. This is why I named the variable ASCII_value. I don't think
it's misleading. It is the ASCII value. It works for all 255 values, value
0 is the only one that is ignored by OpenSCAD.
Note this line in the wiki re chr():
Note: When used with echo() the output to the console for character codes
greater than 127 is platform dependent.
Also code-points above 0x7F are actually stored as 2 bytes
U+0080 U+07FF 2 bytes 110xxxxx 10xxxxxx wikipedia
http://en.wikipedia.org/wiki/UTF-8
The UTF-8 ASCII 'compatability' in terms if byte code is only for 00-7F.
@Torsten, according to Wikipedia, 0x00 is NULL and is a valid code point.
???
Unless specifically shown otherwise above, my contribution is in the Public Domain; To the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. This work is published globally via the internet. :) Inclusion of works of previous authors is not included in the above.
View this message in context: http://forum.openscad.org/How-to-detect-chr-0-tp12351p12410.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Fantome wrote
> When OpenScad echos/prints the chr(value), what it prints matches with the
> ASCII chart. This is why I named the variable ASCII_value. I don't think
> it's misleading. It is the ASCII value. It works for all 255 values, value
> 0 is the only one that is ignored by OpenSCAD.
Note this line in the wiki re chr():
Note: When used with echo() the output to the console for character codes
greater than 127 is platform dependent.
Also code-points above 0x7F are actually stored as 2 bytes
U+0080 U+07FF 2 bytes 110xxxxx 10xxxxxx wikipedia
<http://en.wikipedia.org/wiki/UTF-8>
The UTF-8 ASCII 'compatability' in terms if byte code is only for 00-7F.
@Torsten, according to Wikipedia, 0x00 is NULL and is a valid code point.
???
-----
Unless specifically shown otherwise above, my contribution is in the Public Domain; To the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. This work is published globally via the internet. :) Inclusion of works of previous authors is not included in the above.
The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/
--
View this message in context: http://forum.openscad.org/How-to-detect-chr-0-tp12351p12410.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Fri, Apr 17, 2015 10:12 PM
Also, NULL can be represented as "\u0000"
t=chr(0);
u="\u0000";
u2="\u0000";
echo(t,u); //ECHO: ""," "
if (t==u)
echo("t==u"); // no
if (u==u2)
echo("u==u2"); // yes
text(u); // blank
You should read the 'invalid characters' part in the Wikipedia article.
There a a number of Unicode chars in the range 0x7F-0xFF that are invalid.
It is just luck that they seem to work.
Unless specifically shown otherwise above, my contribution is in the Public Domain; To the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. This work is published globally via the internet. :) Inclusion of works of previous authors is not included in the above.
View this message in context: http://forum.openscad.org/How-to-detect-chr-0-tp12351p12411.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Also, NULL can be represented as "\u0000"
t=chr(0);
u="\u0000";
u2="\u0000";
echo(t,u); //ECHO: ""," "
if (t==u)
echo("t==u"); // no
if (u==u2)
echo("u==u2"); // yes
text(u); // blank
You should read the 'invalid characters' part in the Wikipedia article.
There a a number of Unicode chars in the range 0x7F-0xFF that are invalid.
It is just luck that they seem to work.
-----
Unless specifically shown otherwise above, my contribution is in the Public Domain; To the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. This work is published globally via the internet. :) Inclusion of works of previous authors is not included in the above.
The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/
--
View this message in context: http://forum.openscad.org/How-to-detect-chr-0-tp12351p12411.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
DM
doug moen
Sat, Apr 18, 2015 2:12 AM
Also, NULL can be represented as "\u0000"
echo("\u0000" == "\u0020");
ECHO: true
So \u0000 is converted to a space character.
@Torsten, according to Wikipedia, 0x00 is NULL and is a valid code point.
???
There are a couple of good reasons to not support NUL.
They all stem from the fact that C programs use the NUL character to
terminate a character string.
- One reason might be implementation reasons: a lot of the string APIs
available to C++ programs use NUL terminated strings.
- Another reason is security related: there are a number of network
security exploits that are involve the insertion of NUL characters into
network traffic. There are a couple of projects that build web interfaces
on top of OpenSCAD, so if a malicious OpenSCAD script has the ability to
insert NUL characters into these project's web traffic, it might be
possible to build an exploit.
On 17 April 2015 at 18:12, MichaelAtOz oz.at.michael@gmail.com wrote:
Also, NULL can be represented as "\u0000"
t=chr(0);
u="\u0000";
u2="\u0000";
echo(t,u); //ECHO: ""," "
if (t==u)
echo("t==u"); // no
if (u==u2)
echo("u==u2"); // yes
text(u); // blank
You should read the 'invalid characters' part in the Wikipedia article.
There a a number of Unicode chars in the range 0x7F-0xFF that are invalid.
It is just luck that they seem to work.
Unless specifically shown otherwise above, my contribution is in the
Public Domain; To the extent possible under law, I have waived all
copyright and related or neighbouring rights to this work. This work is
published globally via the internet. :) Inclusion of works of previous
authors is not included in the above.
View this message in context:
http://forum.openscad.org/How-to-detect-chr-0-tp12351p12411.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
> Also, NULL can be represented as "\u0000"
echo("\u0000" == "\u0020");
ECHO: true
So \u0000 is converted to a space character.
>@Torsten, according to Wikipedia, 0x00 is NULL and is a valid code point.
>???
There are a couple of good reasons to not support NUL.
They all stem from the fact that C programs use the NUL character to
terminate a character string.
- One reason might be implementation reasons: a lot of the string APIs
available to C++ programs use NUL terminated strings.
- Another reason is security related: there are a number of network
security exploits that are involve the insertion of NUL characters into
network traffic. There are a couple of projects that build web interfaces
on top of OpenSCAD, so if a malicious OpenSCAD script has the ability to
insert NUL characters into these project's web traffic, it might be
possible to build an exploit.
On 17 April 2015 at 18:12, MichaelAtOz <oz.at.michael@gmail.com> wrote:
> Also, NULL can be represented as "\u0000"
>
> t=chr(0);
> u="\u0000";
> u2="\u0000";
> echo(t,u); //ECHO: ""," "
> if (t==u)
> echo("t==u"); // no
> if (u==u2)
> echo("u==u2"); // yes
> text(u); // blank
>
> You should read the 'invalid characters' part in the Wikipedia article.
> There a a number of Unicode chars in the range 0x7F-0xFF that are invalid.
> It is just luck that they seem to work.
>
>
>
> -----
> Unless specifically shown otherwise above, my contribution is in the
> Public Domain; To the extent possible under law, I have waived all
> copyright and related or neighbouring rights to this work. This work is
> published globally via the internet. :) Inclusion of works of previous
> authors is not included in the above.
>
> The TPP is no simple “trade agreement.” Fight it!
> http://www.ourfairdeal.org/
> --
> View this message in context:
> http://forum.openscad.org/How-to-detect-chr-0-tp12351p12411.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
F
Fantome
Sat, Apr 18, 2015 3:26 AM
You should read the 'invalid characters' part in the Wikipedia article.
There a a number of Unicode chars in the range 0x7F-0xFF that are invalid.
It is just luck that they seem to work.
Hmm, I went to a better source, Unicode.org. I couldn't find anything
stating that any values are invalid but I did find this:
*Q: Are noncharacters invalid in Unicode strings and UTFs?
A: Absolutely not. Noncharacters do not cause a Unicode string to be
ill-formed in any UTF. This can be seen explicitly in the table above, where
every noncharacter code point has a well-formed representation in UTF-32, in
UTF-16, and in UTF-8. An implementation which converts noncharacter code
points between one UTF representation and another must preserve these values
correctly. The fact that they are called "noncharacters" and are not
intended for open interchange does not mean that they are somehow illegal or
invalid code points which make strings containing them invalid.
*
Maybe there is some confusion with what I am doing. I'm not looking to print
anything out as in text. I am only interested in the 8bit values. Wether
it's Unicode, ASCII or whatever doesn't really matter because 8 bits will
always be 8 bits, even if they are taken from a 32 bit value.
I looked at the chart on Unicode.org for the values from 0x80 to 0xFF and
didn't see anything unexpected. I'll post my code this weekend. It seems
that I always have a hard time explaining my thoughts and methods. (This is
on me, nobody else.) Perhaps seeing the code will make it clear.
In any case, my original question has been answered and I now have a way to
use chr() to represent 0x00. Thanks for that.
--
View this message in context: http://forum.openscad.org/How-to-detect-chr-0-tp12351p12415.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
MichaelAtOz wrote
> You should read the 'invalid characters' part in the Wikipedia article.
> There a a number of Unicode chars in the range 0x7F-0xFF that are invalid.
> It is just luck that they seem to work.
Hmm, I went to a better source, Unicode.org. I couldn't find anything
stating that any values are invalid but I did find this:
*Q: Are noncharacters invalid in Unicode strings and UTFs?
A: Absolutely not. Noncharacters do not cause a Unicode string to be
ill-formed in any UTF. This can be seen explicitly in the table above, where
every noncharacter code point has a well-formed representation in UTF-32, in
UTF-16, and in UTF-8. An implementation which converts noncharacter code
points between one UTF representation and another must preserve these values
correctly. The fact that they are called "noncharacters" and are not
intended for open interchange does not mean that they are somehow illegal or
invalid code points which make strings containing them invalid.
*
Maybe there is some confusion with what I am doing. I'm not looking to print
anything out as in text. I am only interested in the 8bit values. Wether
it's Unicode, ASCII or whatever doesn't really matter because 8 bits will
always be 8 bits, even if they are taken from a 32 bit value.
I looked at the chart on Unicode.org for the values from 0x80 to 0xFF and
didn't see anything unexpected. I'll post my code this weekend. It seems
that I always have a hard time explaining my thoughts and methods. (This is
on me, nobody else.) Perhaps seeing the code will make it clear.
In any case, my original question has been answered and I now have a way to
use chr() to represent 0x00. Thanks for that.
--
View this message in context: http://forum.openscad.org/How-to-detect-chr-0-tp12351p12415.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
NH
nop head
Sat, Apr 18, 2015 11:46 AM
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.
On 18 April 2015 at 04:26, Fantome paul@brownsbrain.com wrote:
You should read the 'invalid characters' part in the Wikipedia article.
There a a number of Unicode chars in the range 0x7F-0xFF that are
It is just luck that they seem to work.
Hmm, I went to a better source, Unicode.org. I couldn't find anything
stating that any values are invalid but I did find this:
*Q: Are noncharacters invalid in Unicode strings and UTFs?
A: Absolutely not. Noncharacters do not cause a Unicode string to be
ill-formed in any UTF. This can be seen explicitly in the table above,
where
every noncharacter code point has a well-formed representation in UTF-32,
in
UTF-16, and in UTF-8. An implementation which converts noncharacter code
points between one UTF representation and another must preserve these
values
correctly. The fact that they are called "noncharacters" and are not
intended for open interchange does not mean that they are somehow illegal
or
invalid code points which make strings containing them invalid.
*
Maybe there is some confusion with what I am doing. I'm not looking to
print
anything out as in text. I am only interested in the 8bit values. Wether
it's Unicode, ASCII or whatever doesn't really matter because 8 bits will
always be 8 bits, even if they are taken from a 32 bit value.
I looked at the chart on Unicode.org for the values from 0x80 to 0xFF and
didn't see anything unexpected. I'll post my code this weekend. It seems
that I always have a hard time explaining my thoughts and methods. (This is
on me, nobody else.) Perhaps seeing the code will make it clear.
In any case, my original question has been answered and I now have a way to
use chr() to represent 0x00. Thanks for that.
--
View this message in context:
http://forum.openscad.org/How-to-detect-chr-0-tp12351p12415.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
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.
On 18 April 2015 at 04:26, Fantome <paul@brownsbrain.com> wrote:
> MichaelAtOz wrote
> > You should read the 'invalid characters' part in the Wikipedia article.
> > There a a number of Unicode chars in the range 0x7F-0xFF that are
> invalid.
> > It is just luck that they seem to work.
>
> Hmm, I went to a better source, Unicode.org. I couldn't find anything
> stating that any values are invalid but I did find this:
> *Q: Are noncharacters invalid in Unicode strings and UTFs?
>
> A: Absolutely not. Noncharacters do not cause a Unicode string to be
> ill-formed in any UTF. This can be seen explicitly in the table above,
> where
> every noncharacter code point has a well-formed representation in UTF-32,
> in
> UTF-16, and in UTF-8. An implementation which converts noncharacter code
> points between one UTF representation and another must preserve these
> values
> correctly. The fact that they are called "noncharacters" and are not
> intended for open interchange does not mean that they are somehow illegal
> or
> invalid code points which make strings containing them invalid.
> *
> Maybe there is some confusion with what I am doing. I'm not looking to
> print
> anything out as in text. I am only interested in the 8bit values. Wether
> it's Unicode, ASCII or whatever doesn't really matter because 8 bits will
> always be 8 bits, even if they are taken from a 32 bit value.
>
> I looked at the chart on Unicode.org for the values from 0x80 to 0xFF and
> didn't see anything unexpected. I'll post my code this weekend. It seems
> that I always have a hard time explaining my thoughts and methods. (This is
> on me, nobody else.) Perhaps seeing the code will make it clear.
>
> In any case, my original question has been answered and I now have a way to
> use chr() to represent 0x00. Thanks for that.
>
>
>
>
> --
> View this message in context:
> http://forum.openscad.org/How-to-detect-chr-0-tp12351p12415.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
F
Fantome
Sat, Apr 18, 2015 5:14 PM
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)
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
Sent from the OpenSCAD mailing list archive at Nabble.com.
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)
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
Sent from the OpenSCAD mailing list archive at Nabble.com.
NH
nop head
Sat, Apr 18, 2015 5:55 PM
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@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.
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@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)
> 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
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
AP
Andrew Plumb
Sat, Apr 18, 2015 9:52 PM
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@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@brownsbrain.com mailto:paul@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.
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@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@brownsbrain.com <mailto:paul@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@lists.openscad.org <mailto:Discuss@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@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org