Page 1 of 1

I need help with Unicode converter

Posted: Wed Jul 24, 2013 11:50 pm
by turbolaserguy
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

Re: I need help with Unicode converter

Posted: Sat Jul 27, 2013 1:12 am
by turbolaserguy
Any help, FouthWorld? (or anybody)

Re: I need help with Unicode converter

Posted: Sat Jul 27, 2013 3:39 am
by edgore
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.

Re: I need help with Unicode converter

Posted: Tue Jul 30, 2013 4:35 am
by turbolaserguy
edgore,

Thank you for trying! Somebody, please step in!

Re: I need help with Unicode converter

Posted: Tue Jul 30, 2013 5:25 am
by Simon
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

Re: I need help with Unicode converter

Posted: Tue Jul 30, 2013 6:31 am
by edgore
Hmmm...does this mean the examples in the lessons are wrong?

Re: I need help with Unicode converter

Posted: Tue Jul 30, 2013 6:52 am
by Simon
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

Re: I need help with Unicode converter

Posted: Wed Jul 31, 2013 3:00 pm
by turbolaserguy
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.

Re: I need help with Unicode converter

Posted: Wed Jul 31, 2013 3:07 pm
by turbolaserguy
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. :)