Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

EASY: Creating binary from a concatenated string

Posted on 2004-09-20
10
Medium Priority
?
786 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 672 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
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.

 
LVL 86

Assisted Solution

by:jkr
jkr earned 664 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 664 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

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.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

972 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