Theos PowerBasic Museum 2017

IT-Consultant: Patrice Terrier => Discussion => Topic started by: Patrice Terrier on January 26, 2015, 03:13:38 PM

Title: How to translate MKL$ and CVL into C++
Post by: 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

...
Title: Re: How to translate MKL$ and CVL into C++
Post by: James C. Fuller on January 26, 2015, 05:16:59 PM
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);
}
Title: Re: How to translate MKL$ and CVL into C++
Post by: Patrice Terrier on January 26, 2015, 07:20:21 PM
Thank you James.

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

...
Title: Re: How to translate MKL$ and CVL into C++
Post by: James C. Fuller on January 26, 2015, 08:46:53 PM
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
Title: Re: How to translate MKL$ and CVL into C++
Post by: James C. Fuller on January 26, 2015, 08:59:17 PM
Patrice,

std::string ss
ss +=(std::string(MKL$(1234),4))
cout << ss.length() << endl;
James
Title: Re: How to translate MKL$ and CVL into C++
Post by: James C. Fuller on January 27, 2015, 01:28:59 PM
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;
}

Title: Re: How to translate MKL$ and CVL into C++
Post by: Larry Charlton on January 28, 2015, 12:09:23 AM
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).
Title: Re: How to translate MKL$ and CVL into C++
Post by: Frederick J. Harris on January 28, 2015, 11:29:47 AM
In a strongly typed language like C++, that will give you fits trying to do anything with it.