• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 787
  • Last Modified:

EASY: Creating binary from a concatenated string

I am tryign to create a one-off address book entry as per:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/mapi/html/_mapi1book_address_book_identifiers.asp

I have the following sample code:

_bstr_t bsEmail = "test@test.com"; // input
_bstr_t bsDisplayEmail = "joe smith"; // input

// convert email address (BSTR) to hex string
CString csEmailHex;
int nLength = bsEmail.length();
char* pszx = csEmailHex.GetBufferSetLength(nLength*4);
HexFromBin((LPBYTE)(BSTR)bsEmail, nLength*2, pszx );
csEmailHex.ReleaseBuffer(nLength*4);

// convert email display (BSTR) to hex string
CString csDisplayEmailHex;
nLength = bsDisplayEmail.length();
pszx = csDisplayEmailHex.GetBufferSetLength(nLength*4);
HexFromBin((LPBYTE)(BSTR)bsDisplayEmail, nLength*2, pszx );
csDisplayEmailHex.ReleaseBuffer(nLength*4);

// convert SMTP address type (BSTR) to hex string
CString csSMTPHex;
_bstr_t bsSMTP("SMTP");
nLength = bsSMTP.length();
pszx = csSMTPHex.GetBufferSetLength(nLength*4);
HexFromBin((LPBYTE)(BSTR)bsSMTP, nLength*2, pszx );
csSMTPHex.ReleaseBuffer(nLength*4);

// csEID will be the hex string of our EmailEntryID
// first up would be the MAPUID One-off:
CString csEID("00000000812B1FA4BEA310199D6E00DD010F540200000180");

// followed by the Display Email Address (BSTR)
csEID += csDisplayEmailHex;

// NULL separator
csEID += "00";

// followed by the Addres Type, "SMTP" (BSTR)
csEID += csSMTPHex;

// NULL separator
csEID += "00";

// lastly, add the Email Address (BSTR)
csEID += csEmailHex;

// Convert the Hexstring to a binary ENTRYID
ULONG cbSize = (ULONG)csEID.GetLength()/2;
LPENTRYID pEntryID = NULL;
MAPIAllocateBuffer( cbSize, (LPVOID*)&pEntryID );

FBinFromHex( (LPTSTR)csEmailHex, (LPBYTE)pEntryID );

This code does not seem effecient to me and I have some questions (I can't reach the original author):

Why convert everything to Hex before going to binary?  Can you just concatenate everything and go directly to binary?  

Can someone translate this into code that doesn't using CStrings? (I'm not using MFC)

0
jjacksn
Asked:
jjacksn
3 Solutions
 
jkrCommented:
>>Why convert everything to Hex before going to binary?

This is not hex, but seems to be base64-encoded. It's simply easier to concatenate base64-strings than binary buffers, and the overhead is neglible...
0
 
AxterCommented:
When emailing, data is usually converted to base64-encoding to make sure you don't have special characters that will throw of the SMTP protocal.
0
 
jjacksnAuthor Commented:
I'm confused now, what is the call HexFromBin doing?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jkrCommented:
The HexFromBin apparently does the base64 encoding, whereas FBinFromHex does the decoding.
0
 
itsmeandnobodyelseCommented:
>>> Can someone translate this into code that doesn't using CStrings?

    _bstr_t bsEmail = "test@test.com"; // input
    _bstr_t bsDisplayEmail = "joe smith"; // input

    // convert email address (BSTR) to hex string
    TCHAR csEmailHex[256] = { _T('\0') };
    int nLength = bsEmail.length();
    HexFromBin((LPBYTE)(BSTR)bsEmail, nLength*2, csEmailHex );

    // convert email display (BSTR) to hex string
    TCHAR csDisplayEmailHex[256] = { _T('\0') };
    nLength = bsDisplayEmail.length();
    HexFromBin((LPBYTE)(BSTR)bsDisplayEmail, nLength*2, csDisplayEmailHex );

    // convert SMTP address type (BSTR) to hex string
    TCHAR csSMTPHex[256] = { _T('\0') };
    _bstr_t bsSMTP("SMTP");
    nLength = bsSMTP.length();
    HexFromBin((LPBYTE)(BSTR)bsSMTP, nLength*2, csSMTPHex );

    // csEID will be the hex string of our EmailEntryID
    // first up would be the MAPUID One-off:
    TCHAR csEID[1024] = _T("00000000812B1FA4BEA310199D6E00DD010F540200000180");

    // followed by the Display Email Address (BSTR)
    _tcscat(csEID, csDisplayEmailHex);

    // NULL separator
    _tcscat(csEID, _T("00"));

    // followed by the Addres Type, "SMTP" (BSTR)
    _tcscat(csEID,csSMTPHex );

    // NULL separator
    _tcscat(csEID, _T("00"));

    // lastly, add the Email Address (BSTR)
    _tcscat(csEID, csEmailHex );

    // Convert the Hexstring to a binary ENTRYID
    ULONG cbSize = (ULONG)(_tcslen(csEID)/2);
    LPENTRYID pEntryID = NULL;
    MAPIAllocateBuffer( cbSize, (LPVOID*)&pEntryID );

    FBinFromHex( (LPTSTR)csEmailHex, (LPBYTE)pEntryID );


>>> Why convert everything to Hex before going to binary

It's not really 'Hex' (what is the same as binary) but converting the UNICODE textstrings to another textstring that contains only digits 0 - 9 and characters 'A' - 'F', what is the base of a hexidecimal number system. For example

 UNICODE string(6 bytes):   L"ABC"
 Binary hex(6 bytes)            41 00 42 00 43 00     // 0x41 == 65 == 'A'
 String hex(12 bytes)           "410042004300"

As UNICODE strings already have 2 bytes per character we got 4 bytes per character when converting it to hexadecimal text strings. All convertion is done because of string concatination. Finally, FBinFromHex simply reconverts the concatinated hex string to a 'normal' wide string (accessed as byte buffer by cast to LPBYTE). The reason for hexconverting is the concatination of "00" as a separator between csDisplayEmailHex, csSMTPHex and csEmailHex, what gives a single binary 0 char when reconverted to binary. You can't concatinate a single char to UNICODE wchar characters using standard string functions. (but by using memcpy).

>>>> Can you just concatenate everything and go directly to binary?  

Yes, by using memcpy. But that approach isn't portable and wouldn't work maybe on a 64Bit OS or with future libraries.

Regards, Alex


 
0
 
itsmeandnobodyelseCommented:
My assumption was that your code is working. As it looks strangely that the receiving prog is able to handle wide character strings but needs a single char as a separator.

Regards, Alex

0
 
itsmeandnobodyelseCommented:
Forget my last comment.

_T("00")); is also a wide character string and turns to  

     30 00 30 00  (hex binary)

With FBinToHex it gives  0x0000 what is a wide char terminating zero. So, the (wide) string delivered to MAPI is a wide string that contains substrings terminated with a zero wide character (you'll see only the first substring because of this in a debugger or textfile editor). So its a string array rather than a string that was built. However, normally such string arrays will be closed by two terminating zero characters.

Regards, Alex




   
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now