PDA

View Full Version : Problems with code :(



clueless
07-14-2006, 07:14 PM
Hi, im trying to write an app that converts short amounts of text into RTF (rich text format} and back again. For example ;
"Hello" would = {\rtf0\fonttbl{\f0 Comic Sans MS;}\fs22\cf1{\u72}{\u101}{\u108}{\u108}{\u111}}

for my uses the code breaks up like
{
\rtf0 - never changes
\fonttbl - never changes
{\f0 Comic Sans MS;} - never changes
\fs22 - can be a 2 digit number (22 min) or 4 (1024max)
\cf1 - never changes
{\u72} - H
{\u101} - e
{\u108} - l
{\u108} - l
{\u111} - o
}

Using the String.Char() and String.Asc() functions it should be quite easy converting back and forth between RTF and normal charecters .got it converting from text to rtf no problems using ;-


function MakePacket(TheChar)
local ThePacket="";
ThePacket= "{\U"..String.Asc(TheChar);
return ThePacket.."}"
end

to do the conversion bit but i cant seem to get it to change back to normal text ok.

The way i decidec to convert it back to text is
{\rtf0\fonttbl{\f0 Comic Sans MS;}\fs22\cf1{\u72}{\u101}{\u108}{\u108}{\u111}}
1-starting at charecter pos 37 find the first "U"
2-chop the string to that point
eg. 72}{\u101}{\u108}{\u108}{\u111}}
3- then using a for loop based on the strings length i look at each charecter at a time to see if its a number.
4 - if it is a number its added as a charecter to a variable tBuffer.
if the charecter turns out to be a "U" the tBuffer string variable is converted to a number and then to a charecter.
5- then it gets added to the final translated string

this is what i came up with ;-


local TotalTranslated = "";
local tBuffer = "";
local ClipB_Txt = "";
local tBufferNumb = 0;
local tStart = 0;
local tInc = 0;

ClipB_Txt = Clipboard.GetText();

if ClipB_Txt then
tStart = String.Find(ClipB_Txt, "U", 37, false);
tbResult = String.Right(ClipB_Txt, String.Length(ClipB_Txt)-tStart);

for tInc=1,String.Length(ClipB_Txt) do
--check for number charecter
if String.Asc(String.Mid(ClipB_Txt, tInc, 1))>47 and String.Asc(String.Mid(ClipB_Txt, tInc, 1))<58 then
tBuffer=tBuffer..String.Mid(ClipB_Txt, tInc, 1);
else
if String.Mid(ClipB_Txt, tInc, 1)=="U" then
tBufferNumb = String.ToNumber(tBuffer);
TotalTranslated = TotalTranslated..String.Char(tBufferNumb);
tBuffer = "";
tBufferNumb = 0;
end
end
end
end
Paragraph.SetText("Paragraph1", TotalTranslated);


can anyone see where im going wrong?

TJ_Tigger
07-14-2006, 07:52 PM
Give this a shot.


strRTF = "{\rtf0\fonttbl{\f0 Comic Sans MS;}\fs22\cf1{\u72}{\u101}{\u108}{\u108}{\u111}}"

function RTF2Text(sRTF)
local sRTF = sRTF
local sOutText = ""
local nBPos = String.Find(sRTF, "{u", 1, true);
while nBPos ~= -1 do
local nEPos = String.Find(sRTF, "}", nBPos, true);
sOutText = sOutText .. String.Char(String.Mid(sRTF, nBPos+2, nEPos - nBPos -2));
nBPos = String.Find(strRTF, "{u", nEPos, true);
end
return sOutText ;
end

Dialog.Message("", RTF2Text(strRTF));

clueless
07-15-2006, 03:29 AM
That works fine. :) thanks.
But i have no idea how its working or why mine wasnt. lol. In your code i dont understand how the variables are being passed around. Im used to writting with object code where a 'return' would pass some data to the point at which the function was called. The variables used to recieve the passed data are local to that function only .


Result=addTwoNumbs(2,3);

Function addTwoNumbs(int NumbA, int NumbB)
{
return NumbA+NumbB;
}

it looks like the 'return' is working in some other way here?
Thanks again for the ready made solution. :yes

clueless
07-15-2006, 04:00 AM
Hmmm.. Im not sure why this isnt working?



strRTF = Clipboard.GetText();
--strRTF = "{\rtf0\fonttbl{\f0 Comic Sans MS;}\fs22\cf1{\u72}{\u101}{\u108}{\u108}{\u111}}"

function RTF2Text(sRTF)
local sRTF = sRTF
local sOutText = ""
local nBPos = String.Find(sRTF, "{u", 1, true);
while nBPos ~= -1 do
local nEPos = String.Find(sRTF, "}", nBPos, true);
sOutText = sOutText .. String.Char(String.Mid(sRTF, nBPos+2, nEPos - nBPos -2));
nBPos = String.Find(strRTF, "{u", nEPos, true);
end
return sOutText ;
end

Dialog.Message("", RTF2Text(strRTF));

For the program im making the RTF code will be grabbed from clipboard memory each time.

TJ_Tigger
07-15-2006, 10:55 AM
This works for me.


strRTF = "{\rtf0\fonttbl{\f0 Comic Sans MS;}\fs22\cf1{\u72}{\u101}{\u108}{\u108}{\u111}}"
Clipboard.CopyText(strRTF);
strRTF = Clipboard.GetText()


function RTF2Text(sRTF)
--Set Local variables
local sRTF = sRTF
local sOutText = "" --This formats the output text returned by this function
--find the starting position of the first {u. I think the \ is interpreted as a special character when read in the string.
local nBPos = String.Find(sRTF, "{u", 1, true);
--Dialog.Message(""..nBPos, sOutText);
--Whil the nBPos is not -1 meaning it is not at the end of the string the while loop will continue to loop
while nBPos ~= -1 do
--Look for the end position of the character to calculate the length of the numerical value of the character
local nEPos = String.Find(sRTF, "}", nBPos, true);
--Dialog.Message(nBPos.."-"..nEPos, String.Mid(sRTF, nBPos+2, nEPos - nBPos -2));
--Set the output text equal to the outputtext plus the translation of the numerical value of the character
sOutText = sOutText .. String.Char(String.Mid(sRTF, nBPos+2, nEPos - nBPos -2));
--Dialog.Message(nEPos.."-"..nBPos, sOutText);
--Find the start of the next character, if found this will keep looping, if not found then it end the loop
nBPos = String.Find(strRTF, "{u", nEPos, true);
end
--return the output string
return sOutText ;
end

Dialog.Message("", RTF2Text(strRTF));

clueless
07-15-2006, 04:29 PM
thanks for the help. I see the problem i was having. It was the U's. In my function MakePacket it was using capital U's not lower case.