• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

How to translate MKL$ and CVL into C++

Started by Patrice Terrier, January 26, 2015, 03:13:38 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Patrice Terrier

In C++, i would like to mimic MKL$ and CVL, to store/retrieve exactly from a string size of 4-byte ?

To do something like this:

long nIndex = 123456789;
string sBuffer = "";
sBuffer += MKL$(nIndex)

and i would like to have strlen(sBuffer) returning 4
nIndex = 95684321;
sBuffer += MKL$(nIndex)

and now i would like to have strlen(sBuffer) returning 8

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller

Patrice,
  This is what BCX uses for normal char buffers.
James

long CVL (char *s)
{
    return ((long*)s)[0];
}


char *MKL (int cvt)
{
    static char temp[5];
    return (char *) memmove(temp, &cvt, 4);
}

Patrice Terrier

#2
Thank you James.

unfortunatly that doesn't work the same than in PB, because of the trailing null char.

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller

Yeah that's right PowerBASIC uses BSTR's.
Maybe you could adapt something from here:
https://msdn.microsoft.com/en-us/library/zthfhkd6.aspx

James

James C. Fuller

Patrice,

std::string ss
ss +=(std::string(MKL$(1234),4))
cout << ss.length() << endl;
James

James C. Fuller

Patrice,
  How about this?
James


long CVL (std::string  s)
{
    long     l = {0};
    memmove( &l, s.data(), 4);
    return l;
}


std::string MKL (long cvt)
{
    char temp[5];
    memmove(temp, &cvt, 4);
    return std::string(temp, 4);
}


int main (int argc, PCHAR* argv)
{
    std::string  ss;
    long     l = {0};
    ss = MKL( 1234);
    l = CVL( ss);
    cout << ss.length() << endl;
    cout << l << endl;
}


Larry Charlton

Quote from: Patrice Terrier on January 26, 2015, 03:13:38 PM
In C++, i would like to mimic MKL$ and CVL, to store/retrieve exactly from a string size of 4-byte ?

To do something like this:

long nIndex = 123456789;
string sBuffer = "";
sBuffer += MKL$(nIndex)

and i would like to have strlen(sBuffer) returning 4
nIndex = 95684321;
sBuffer += MKL$(nIndex)

and now i would like to have strlen(sBuffer) returning 8

...
You can't.  You can create new string routines that track the length of the buffer though (just like PB).  You can't MKL$(var) because there are an enormous number of numbers that will have 0 bytes in them frying the way strlen works (looking for a trailing 0).

Frederick J. Harris

In a strongly typed language like C++, that will give you fits trying to do anything with it.