CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2007
    Posts
    6

    Unhappy 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 (

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    Oct 2007
    Posts
    6

    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, "&#37;02X", ((SBinary *)pRef)->lpb[i]), ++i);
    Cheers!

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help with small Pascal to C++ conversion

    Quote Originally Posted by gciochina View Post
    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], "&#37;02X", ((SBinary *)pRef)->lpb[i]), ++i);
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 17th, 2010 at 03:07 PM.

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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
  •  





Click Here to Expand Forum to Full Width

Featured