Author Topic: Prettify string (superscript etc.)  (Read 6229 times)

0 Members and 1 Guest are viewing this topic.

Offline Frog

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +0/-0
    • View Profile
Prettify string (superscript etc.)
« on: September 23, 2011, 02:02:15 pm »
I'm trying to prettify a string, so that for example ^2 becomes 2 and sqrt(x) becomes the true square root sign. How can I do this in Lua? I've been trying to find out the correct codes using string.byte(), but somehow superscript 1 to 3 all return 194. And when I try to insert that character using string.char(194) I get a lot of strange signs. How can I do this?

Thanks!

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Prettify string (superscript etc.)
« Reply #1 on: September 23, 2011, 03:43:06 pm »
Well, string.byte looks at the string as if it would be ascii, but in fact the nspire uses utf-8. UTF-8 uses multiple bytes, unlike ascii. And since string.byte is for ascii, it takes only the first byte, 194.
To make a story short, you need string.char(194, 178) for ². 194 is the first byte, 178 the second.
Now, to find the bytes of a utf-8 char first use string.len, to see how many bytes there are. Then use string.byte to fetch the chars:
Code: (Lua) [Select]
ch="²"
string.len(ch)  --returns 2
b1=ch:byte(1) --returns 194
b2=ch:byte(2) -- returns 178

chr = string.byte(194, 178) --returns ²

Offline Frog

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +0/-0
    • View Profile
Re: Prettify string (superscript etc.)
« Reply #2 on: September 23, 2011, 04:46:10 pm »
Man, that's really quite hard. Thanks a lot again!

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Prettify string (superscript etc.)
« Reply #3 on: September 24, 2011, 05:35:33 am »
No problem, glad I could be a help :D

Offline Frog

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +0/-0
    • View Profile
Re: Prettify string (superscript etc.)
« Reply #4 on: September 24, 2011, 04:17:57 pm »
Mmm, I have another problem. :P In my problem users can enter a string and I calculate the derivative. The problem is it's quite hard to read, so I decided to make powers in superscript. I now have all numbers in superscript, but I can't find a way to do this for alphabetic characters. I also looked at the UTF-16 specification (which the TI-NSpire uses I believe), and those characters don't exist! How can I do this? Or do I have to manually draw the powers in a smaller font?!

Offline Loulou 54

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 165
  • Rating: +39/-0
    • View Profile
    • Mes programmes sur TI bank.
Re: Prettify string (superscript etc.)
« Reply #5 on: September 24, 2011, 05:02:38 pm »
Well, string.byte looks at the string as if it would be ascii, but in fact the nspire uses utf-8. UTF-8 uses multiple bytes, unlike ascii. And since string.byte is for ascii, it takes only the first byte, 194.
To make a story short, you need string.char(194, 178) for ². 194 is the first byte, 178 the second.
Now, to find the bytes of a utf-8 char first use string.len, to see how many bytes there are. Then use string.byte to fetch the chars:
Code: (Lua) [Select]
ch="²"
string.len(ch)  --returns 2
b1=ch:byte(1) --returns 194
b2=ch:byte(2) -- returns 178

chr = string.byte(194, 178) --returns ²
Isn't string.uchar function made for that ?
Some of my program available here.. :)
http://ti.bank.free.fr/index.php?mod=archives&ac=voir2&id=1471

     

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Prettify string (superscript etc.)
« Reply #6 on: September 24, 2011, 05:09:43 pm »
Hmm, probably. Thanks for noting, didn't think about it :)

@Frog, I don't know immediately, will check tomorrow.
« Last Edit: September 24, 2011, 05:11:28 pm by jimbauwens »

Offline Frog

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +0/-0
    • View Profile
Re: Prettify string (superscript etc.)
« Reply #7 on: October 01, 2011, 10:37:56 am »
Man, I just realized I didn't reply to this post! Sorry!

@Loulou: Yeah, I already realized that. I changed that in my code as well.

@jimbauwens: Have you already thought of a way to do that? I've been thinking about it as well, and the only method I can think of is a manual draw method... Do you agree with me?

Thanks for all the answers!

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Prettify string (superscript etc.)
« Reply #8 on: October 01, 2011, 01:28:06 pm »
I'm terribly sorry, totally forgot about this!

I think the easiest would be to use a smaller font, because I only find a few characters, not all of them.

Offline Frog

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +0/-0
    • View Profile
Re: Prettify string (superscript etc.)
« Reply #9 on: October 01, 2011, 02:23:07 pm »
@Jim: No problem, I also forgot about it myself. :P

I figured the same, I think I'll have to do that.
« Last Edit: October 01, 2011, 02:23:29 pm by Frog »