Chr(), ASCII and NXT-G

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
peter-cocteau
Posts: 26
Joined: 04 Dec 2011, 12:12

Chr(), ASCII and NXT-G

Post by peter-cocteau »

Hi,

I'm a happy NXT-G user and I know "Chr()" block to transform decimal to ASCII character.

But is there a simple way to convert ASCII to decimal ??

I explain: I think ASCII is very useful to store values in minimized .txt file.
One decimal character propose 10 values (0...9) only when ASCII character propose 94 values (#@123ADERgsf!...etc)
"Chr()" is useful to convert decimal to ASCII but I wonder what tool you would use to make ASCII to decimal conversion.

The only solution I see is to check if ASCII=character and then associate a number to this charcater:

If ASCII value= t then value= 86
If ASCII value= ! then value= 54

...it's only example.

What do you think about that?
I also wonder what people use the block"Chr()" for.

Thanks.

Peter.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Chr(), ASCII and NXT-G

Post by mattallen37 »

Well, you couldn't you compere the value of the character to all the decimal numbers' ASCII values? I don't know the limits of strings, chars, and number conversions presented by NXT-G, but in NXC you should be able to subtract 0x30 from each of the chars.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: Chr(), ASCII and NXT-G

Post by spillerrec »

I'm not a NXT-G user, but I don't quite understand, why do you need a decimal to/from ASCII conversion? ASCII is not a data type, but a number which is converted to a image when shown on a screen. A string is similarly just an array of numbers. (And this is how it works behind the scenes in the firmware too.)

So what are you actually trying to do? I get the feeling that Chr() returns the value from a string at position , and you want to do the reverse, setting the value at position in a string to a specific number?
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Chr(), ASCII and NXT-G

Post by HaWe »

in that case I would recommend to preferably learn C ;)
peter-cocteau
Posts: 26
Joined: 04 Dec 2011, 12:12

Re: Chr(), ASCII and NXT-G

Post by peter-cocteau »

Hi,

I'm using ASCII to store ""zipped"" files and save some NXT memory, I'm working on it for my NXT-606 drum box.
(if you don't know NXT-606 you should take a look to this Mindboard link: https://sourceforge.net/apps/phpbb/mind ... f=3&t=1178 )

I save NXT-606 beats in .txt format. This .txt looks like this: 01121622.... 01 is sound selected on step1, 12 on step2, 16 on step3, 22 on step4, etc...

Well, as you see, 01 or 12 or 16 .... takes twice more memory than "!" or "P" or "@"...
I'm using ASCII to "zip" values and found a way to "unzip" it with NXT-G:
Image


The "reference ASCII" text table is:
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijkmnopqrstuvwxyz{|}~

Peter.
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: Chr(), ASCII and NXT-G

Post by spillerrec »

I tried installing NXT-G and I see that this is a non-trivial task, which I as a programmer find very surprising. In pretty much every programming language out there you can access and manipulate a character in a string very easily. In NXC it looks like this:

Code: Select all

string str = "abcd";
char decimal = str[ 0 ]; //'decimal' now contains the first letter of 'str', meaning 'a' which equals to 97.
str[ 1 ] = decimal + 5; //The second letter ('b') is now overwritten by 97 + 5 = 102 which equals 'f'. str therefore contains "afcd" now.
A see a long list of string functions in the standard Lego firmware, however almost none are included in NXT-G? I don't get it...
Using NXC you can ignore ASCII completely and store it as RAW data. This allows you to store a number in the range of 0-255 using a single byte. This is also part of the standard support in the firmware, so I guess it should be possible to make a custom block to NXT-G for this... Or at least the most important string functions, I believe that they would be useful for a lot of people...

Anyway, you can pack files even more densely if you encode several values into a single number. Consider that you want to store the volume for each sound on a step. I spot in your video you uses 4 levels, [0, 25, 50, 75, 100]. That could be stored as the numbers [0,1,2,3, 4] by dividing with 25. If you have up to 16 samples, you can create an encoded number by

Code: Select all

byte encoded = (volume/25) * 16 + sample;
In order to extract it afterwards, you would do:

Code: Select all

byte volume = encoded / 16;
byte sample = encoded - volume * 16;
volume = volume * 25; //Get the original range
Notice that you would need 5 * 16 = 80 numbers/characters to store it.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
bungeshea
Posts: 207
Joined: 14 Aug 2011, 08:45
Location: Australia
Contact:

Re: Chr(), ASCII and NXT-G

Post by bungeshea »

I don't know if this would help (or is even remotely related) but there is a nice collection of text manipulation blocks for NXT-G at the Mindboards Repository
peter-cocteau
Posts: 26
Joined: 04 Dec 2011, 12:12

Re: Chr(), ASCII and NXT-G

Post by peter-cocteau »

Yes I use a lot of third party block in NXT-G, it seems there is less blocks in mindboard repository than in "old" nxtasy page.

Spillerrec, you are totally right, in my first NXT606 each measure took 51 characters:

tempo: 40-160= 120 120/2=60 => 2characters (tempo resolution is 2 : 40-42-44-46-48-50-52-.....)
swing: 0-9 => 1character
sounds:1-24=>2character*16steps =32 characters
level: 0-4=>1character*16steps= 16characters

As you understood I want to reduce it to 18 characters with ASCII help:
tempo: 1ASCII character
swing: 1 ASCII character
level+sound: 16 ASCII characters.

51 to 18 is a good ratio compression. :)

Peter.
bungeshea
Posts: 207
Joined: 14 Aug 2011, 08:45
Location: Australia
Contact:

Re: Chr(), ASCII and NXT-G

Post by bungeshea »

peter-cocteau wrote:Yes I use a lot of third party block in NXT-G, it seems there is less blocks in mindboards repository than in "old" nxtasy page.
That's mostly because when NXTasy went AWOL, a lot of the content and resources were lost. However, the folder I linked to was only a very small potion of the Mindboards Repository. All of the files can be accessed from http://box.net/mindboards. If you have any NXT-G blocks not listed, we'd be very pleased to have them. https://sourceforge.net/apps/phpbb/mind ... ?f=6&t=678
peter-cocteau
Posts: 26
Joined: 04 Dec 2011, 12:12

Re: Chr(), ASCII and NXT-G

Post by peter-cocteau »

Well, by looking closer it seems I was wrong. I found no block in my own library that isn't in mindboard repository.
But it's good to know there is still a place to download it. :)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests