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,
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);
}
Thank you James.
unfortunatly that doesn't work the same than in PB, because of the trailing null char.
...
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
Patrice,
std::string ss
ss +=(std::string(MKL$(1234),4))
cout << ss.length() << endl;
James
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;
}
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).
In a strongly typed language like C++, that will give you fits trying to do anything with it.