Solved

EASY: Creating binary from a concatenated string

Posted on 2004-09-20
10
764 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 clear a vector as well as how to detect empty vectors in C++.

615 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