Solved

EASY: Creating binary from a concatenated string

Posted on 2004-09-20
10
753 Views
Last Modified: 2008-01-09
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
Comment
Question by:jjacksn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12106999
>>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
 
LVL 30

Accepted Solution

by:
Axter earned 168 total points
ID: 12107065
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
 
LVL 5

Author Comment

by:jjacksn
ID: 12107248
I'm confused now, what is the call HexFromBin doing?
0
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!

 
LVL 86

Assisted Solution

by:jkr
jkr earned 166 total points
ID: 12112801
The HexFromBin apparently does the base64 encoding, whereas FBinFromHex does the decoding.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12112835
>>> 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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12112909
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 166 total points
ID: 12113216
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question