I need help with Unicode converter

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
turbolaserguy
Posts: 38
Joined: Thu Jul 04, 2013 1:29 pm

I need help with Unicode converter

Post by turbolaserguy » Wed Jul 24, 2013 11:50 pm

I'm trying to make something like this:
http://www.branah.com/unicode-converter

And I created a stack that doesn't work properly yet that you can download the zip file at:
http://turbolaserguy.jimdo.com/livecode/

When I click the CharToNum button, nothing but spaces appear.

The following are the script of CharToNum button and numToChar button:

CharToNum:

on mouseUp
set the useUnicode to true
put empty into card field "Unicode Numbers"
put the number of chars of card field "Unicode Text" into numOfChars
repeat with i=1 to numOfChars
put char i of card field "Unicode Text" into theChar
put charToNum(theChar) into theNumber
put theNumber after card field "Unicode Numbers"
if i is not numOfChars then put space after card field "Unicode Numbers"
end repeat
end mouseUp

NumToChar:

on mouseUp
if card field "Unicode Numbers" is not empty then put empty into card field "Unicode Text"
set the useUnicode to true
put the number of words of card field "Unicode Numbers" into numOfNumbers
repeat with i=1 to numOfNumbers
set the unicodeText of card field "Unicode Text" to the unicodeText of card field "Unicode Text" & uniEncode(numToChar(word i of card field "Unicode Numbers"),"UTF8")
end repeat
end mouseUp

turbolaserguy
Posts: 38
Joined: Thu Jul 04, 2013 1:29 pm

Re: I need help with Unicode converter

Post by turbolaserguy » Sat Jul 27, 2013 1:12 am

Any help, FouthWorld? (or anybody)

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Re: I need help with Unicode converter

Post by edgore » Sat Jul 27, 2013 3:39 am

I thought I knew the answer to this, but apparently, I don't.

According to the tutorial at http://lessons.runrev.com/buckets/1228/lessons/20441 this should come down to the fact that unicode characters are 16 bits rather than 8 bits long (or double-byte). However, only the charToNum and the NumToChar functions know this. This causes, I think, for you to get an incorrect results when you go by what is reported by the number of characters in a line - it's reporting the number of visible characters, not the underlying double-byte characters that you need. The example shows reading the first visible unicode char of a field by using :

Code: Select all

put charToNum(char 1 to 2 of field "theField") into variable
Which makes sense - each unicode character that you see on screen is really two 8-bit "sections" that added together make a single 16 bit character.

I rewrote your chartoNum script so it would make a two-character "window" that would go through your field and get the charToNum in 2 character segments:

Code: Select all

on mouseUp
   set the useUnicode to true
   put empty into field "Unicode Numbers"
   put the number of chars in field "Unicode Text" into y
   repeat with x = 0 to y-1
      put "char " & X+(X+1) & " to " & X+(X+2) --this line is just to display the values in the line below in the msg
      put charToNum(char X+(X+1) to X+(X+2) of field "Unicode Text") & space after field "Unicode Numbers"
   end repeat
   delete the last char of field "Unicode Numbers"
end mouseUp
From my testing this looks like it's getting double-byte characters and processing them and I am getting a result in the output field, but I am sure it's not the right one.

I am not sure what it's still doing wrong though. Hopefully this might get you a little closer though, and maybe someone can step in and fix my version, assuming it's closer to correct.

turbolaserguy
Posts: 38
Joined: Thu Jul 04, 2013 1:29 pm

Re: I need help with Unicode converter

Post by turbolaserguy » Tue Jul 30, 2013 4:35 am

edgore,

Thank you for trying! Somebody, please step in!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: I need help with Unicode converter

Post by Simon » Tue Jul 30, 2013 5:25 am

Hi turbolaserguy,
Here try this:

Code: Select all

on mouseUp
   set the useUnicode to true
   put empty into card field "Unicode Numbers"
   put the number of chars of card field "Unicode Text" into numOfChars
   repeat with i=1 to numOfChars
      put the unicodeText of char i of field "Unicode Text" into theChar
      put charToNum(theChar) into theNumber
      put theNumber after field "Unicode Numbers"
      if i is not numOfChars then put space after field "Unicode Numbers" 
   end repeat
end mouseUp
and

Code: Select all

on mouseUp
   if card field "Unicode Numbers" is not empty then put empty into field "Unicode Text"
   set the useUnicode to true
   put the number of words of card field "Unicode Numbers" into numOfNumbers
   repeat with i=1 to numOfNumbers
      set the unicodeText of field "Unicode Text" to numToChar(word i of field "Unicode Numbers")
      if i is not numOfChars then put space after field "Unicode Text" 
      
   end repeat
end mouseUp
You are going to have to mess around with "after" for that numToChar (you will see)
I also removed a bunch of "...card field...", you don't need the "card" part if you are talking about the current card.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Re: I need help with Unicode converter

Post by edgore » Tue Jul 30, 2013 6:31 am

Hmmm...does this mean the examples in the lessons are wrong?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: I need help with Unicode converter

Post by Simon » Tue Jul 30, 2013 6:52 am

No, I don't think so.
In Step 4 it defines using the "unicodeText" prior to any conversion or transfer.

But we've yet to hear back from the OP, maybe I'm all wrong. :)
Hey turbolaserguy, is it working for you?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

turbolaserguy
Posts: 38
Joined: Thu Jul 04, 2013 1:29 pm

Re: I need help with Unicode converter

Post by turbolaserguy » Wed Jul 31, 2013 3:00 pm

Simon,
The CharToNum button is now working!! Thank you very much.
However, when I click the NumToChar button, only the last character (which is correctly converted) is displayed in the Unicode Text field. Hmm.

turbolaserguy
Posts: 38
Joined: Thu Jul 04, 2013 1:29 pm

Re: I need help with Unicode converter

Post by turbolaserguy » Wed Jul 31, 2013 3:07 pm

Problem Solved!
The final NumToChar button script is:

on mouseUp
   if card field "Unicode Numbers" is not empty then put empty into field "Unicode Text"
   set the useUnicode to true
   put the number of words of card field "Unicode Numbers" into numOfNumbers
   repeat with i=1 to numOfNumbers
      set the unicodeText of field "Unicode Text" to the unicodeText of field "Unicode Text" & numToChar(word i of field "Unicode Numbers")
   end repeat
end mouseUp

Thank you very much, two of you. :)

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”