October 09, 2024, 04:35:56 PM

News:

IWBasic runs in Windows 11!


how to return a string bigger than 255 from a sub

Started by ExMember001, October 31, 2007, 09:55:41 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ExMember001

October 31, 2007, 09:55:41 AM Last Edit: October 31, 2007, 10:00:24 AM by KrYpT
hi,
i have this code that use a very simple encryption subroutine to encrypt the text in a richedit control.
it work fine until the text is bigger than 255 characters.

how can return a string bigger than 255 from this sub?


#define RICHEDIT 1
#define LOAD 2
#define ENCRYPT 4

class dlg:CDialog
{
declare OnInitDialog(),int;
declare OnClose(),int;
declare OnControl(int nID, int nNotifyCode, unsigned int hControl),int;

CRichEdit *ReC;
}

global sub main()
{
dlg d1;
d1.Create(0,0,300,217,0x80C80080,0,"Encryption",0);
d1.AddControl(CTRICHEDIT,"",14,19,271,153,0x50B11084,0x200,RICHEDIT);
d1.AddControl(CTDEFBUTTON,"Load",14,183,70,20,0x50010001,0x0,LOAD);
d1.AddControl(CTBUTTON,"Encrypt/Decrypt",185,183,100,20,0x50000000,0x0,ENCRYPT);

d1.DoModal();
return 0;
}

dlg::OnClose(),int
{
CloseDialog(IDCANCEL);
return true;
}

dlg::OnInitDialog(),int
{
/* Initialize any controls here */
CenterWindow();
ReC = GetControl(RICHEDIT);
return true;
}

dlg::OnControl(int nID, int nNotifyCode, unsigned int hControl),int
{
select nID
{
case LOAD:
if(nNotifyCode = 0)
{
STRING filename = FILEREQUEST("Load file",this,1,"All types (*.*) |*.*||","");
ReC->LoadFile(filename, 1);
}
case ENCRYPT:
if(nNotifyCode = 0)
{
ReC->SetSel(0, -1,true);
ReC->SetText(crypttext(ReC->GetSelText(), "thisisakeytobechange"));
}
}
return true;
}

sub crypttext(string text, string key),string
{
int lentext = len(text);
int lenkey = len(key);
int l, val, keyval;
string newletter, letter;
string newtext = "";

//process key
for(l=1;l<lenkey+1;l++)
    {
letter = STRMID(key, l, 1 );
val = ToAscii(letter);
keyval += val;
}

// enum and convert each letters of the string to ascii value
// then convert ascii to char and build the new string
for(l=1;l<lentext+1;l++)
    {
letter = STRMID(text, l, 1 );
val = ToAscii(letter);
newletter = ToChar(val xor keyval);
newtext += newletter;
}

return newtext;
}



i tryed to use dstring instead of string but doesnt want to works ...
anyone can help me with this please.

Ionic Wind Support Team

Use a dstring like it is meant to be used.  It is not a type, it is for defining string larger than 255 characters.  Since it is not a type you don't use it as a parameter, or as a return, only in defining the length of a string.

The size of a string a function can return is unlimited.


sub crypttext(string text, string key),string
{
int lentext = len(text);
int lenkey = len(key);
int l, val, keyval;
string newletter, letter;
dstring newtext[1024] = "";  /* USE A DSTRING ;) */

//process key
for(l=1;l<lenkey+1;l++)
    {
letter = STRMID(key, l, 1 );
val = ToAscii(letter);
keyval += val;
}

// enum and convert each letters of the string to ascii value
// then convert ascii to char and build the new string
for(l=1;l<lentext+1;l++)
    {
letter = STRMID(text, l, 1 );
val = ToAscii(letter);
newletter = ToChar(val xor keyval);
newtext += newletter;
}

return newtext;
}


Of course I would do it differently myself.   Using HEAP as a return type and allocating the returned string based on the size of the input string.


sub crypttext(string text, string key),heap
{
int lentext = len(text);
int lenkey = len(key);
int l, val, keyval;
string newletter, letter;
string *pReturn = AllocHeap(LEN(text)+1);

//process key
for(l=1;l<lenkey+1;l++)
    {
letter = STRMID(key, l, 1 );
val = ToAscii(letter);
keyval += val;
}

// enum and convert each letters of the string to ascii value
// then convert ascii to char and build the new string
for(l=1;l<lentext+1;l++)
    {
letter = STRMID(text, l, 1 );
val = ToAscii(letter);
newletter = ToChar(val xor keyval);
*preturn += newletter;
}

return *(string)pReturn;
}


Both of course typed without testing.  Tweaking may be required.

Paul.
Ionic Wind Support Team

ExMember001

works with the dstring as shown in your example... i don't know what i was doing wrong... so simple ;)
thank you.

ExMember001

interresting also the heap thing ;)
and works like a charm  ;D