Solved

EASY: Creating binary from a concatenated string

Posted on 2004-09-20
10
740 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
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now