|
-
February 16th, 2010, 06:50 PM
#1
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 (
-
February 16th, 2010, 07:19 PM
#2
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.
-
February 17th, 2010, 02:19 PM
#3
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!
-
February 17th, 2010, 03:02 PM
#4
Re: Help with small Pascal to C++ conversion
 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
Last edited by Paul McKenzie; February 17th, 2010 at 03:07 PM.
-
February 18th, 2010, 10:58 AM
#5
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
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|