Author Topic: How to translate MKL$ and CVL into C++  (Read 4454 times)

0 Members and 1 Guest are viewing this topic.

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2003
    • www.zapsolution.com
How to translate MKL$ and CVL into C++
« 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

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

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 674
Re: How to translate MKL$ and CVL into C++
« Reply #1 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);
}

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2003
    • www.zapsolution.com
Re: How to translate MKL$ and CVL into C++
« Reply #2 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.

...
« Last Edit: January 26, 2015, 07:26:33 PM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 674
Re: How to translate MKL$ and CVL into C++
« Reply #3 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

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 674
Re: How to translate MKL$ and CVL into C++
« Reply #4 on: January 26, 2015, 08:59:17 PM »
Patrice,

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

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 674
Re: How to translate MKL$ and CVL into C++
« Reply #5 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;
}


Offline Larry Charlton

  • Newbie
  • *
  • Posts: 18
Re: How to translate MKL$ and CVL into C++
« Reply #6 on: January 28, 2015, 12:09:23 AM »
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).

Offline Frederick J. Harris

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 942
    • Frederick J. Harris
Re: How to translate MKL$ and CVL into C++
« Reply #7 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.