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?
"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?