Help with small Pascal to C++ conversion
Guyz, please, i'm beaten...
i've got the following code that's written in pascal. i need it translated to c++ ... too tired/little knowledge to do this myself...
function HexFromBin(lpb : pointer; cb : DWORD):string;
var HexStr:string;
i:integer;
begin
SetLength(Result,2*cb);
for i:=0 to cb-1 do begin
HexStr:=IntToHex(TByteArray(lpb^)[i], 2);
Result[2*i+1]:=HexStr[1];
Result[2*i+2]:=HexStr[2];
end;
end;
if any of you can't sleep, feel like giving a hand, i'd really appreciate it.
oh, and btw if you're asking yourself what is this, it's a function that returns a MAPI email message ID. and it is not present in the WM5x/6x libraries.. the only implementation i've found is the one above but it's written in pascal :((
Re: Help with small Pascal to C++ conversion
Conversion from a hex string to an int can be done several ways; strtol() is probably the simplest.
Once you have an int, it can be formatted to a binary string using std::bitset.
Re: Help with small Pascal to C++ conversion
Thanks!
however i've done it a bit different:
int i;
char *pszTnefCorrelationKey=NULL;
(pszTnefCorrelationKey= new char[2*((SBinary *)pRef)->cb+1]);
for(i=0; i<((SBinary *)pRef)->cb; sprintf(pszTnefCorrelationKey+2*i, "%02X", ((SBinary *)pRef)->lpb[i]), ++i);
Cheers! :D
Re: Help with small Pascal to C++ conversion
Quote:
Originally Posted by
gciochina
Thanks!
however i've done it a bit different:
Where do you do the "delete" for that "new" you're doing? Here is the same code using vector, where you don't need "new" or "delete"
Code:
#include <vector>
//...
int i;
std::vector<char> pszTnefCorrelationKey(2*((SBinary *)pRef)->cb+1);
for(i=0; i<((SBinary *)pRef)->cb; sprintf(&pszTnefCorrelationKey[2*i], "%02X", ((SBinary *)pRef)->lpb[i]), ++i);
Regards,
Paul McKenzie
Re: Help with small Pascal to C++ conversion
Your original Pascal function appears to be returning a 'string'
Here's a more C++ solution using STL strings
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;
string To_Hex(char *lpb, int cb)
{
ostringstream text;
for (int i = 0; i < cb; ++i)
{
text << hex << setw(2) << setfill('0') << int(lpb[i]);
}
return text.str();
}
int main()
{
char data[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
cout << To_Hex(data, 8);
}
Prints 0001020304050607