0

I have a legacy C++ COM application which writes a BSTR (UTF-16 on Windows) like this.

Say, ☻ (Black Smiley i.e. ALT + Numpad 2) is written like this in HEX - 060000003B260D000A00 by the application. Note that 1st 4 bytes are reserved for BSTR length

Now, how do I display back the black smiley in C# from this HEX string ? In VS debugger, '\u263B' displays the smiley, but here the string is 3B26. This is just an example of a kind of data. Any data can be dumped by that app (like large XSLs, Texts, etc. - all converted in HEX format). Idea is to interpret the HEX correctly in C#.

This link talks something similar, but not very sure. Any pointers ?

enter image description here

Community
  • 1
  • 1
Angshuman Agarwal
  • 4,796
  • 7
  • 41
  • 89
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Apr 15 '13 at 15:35
  • You are just discovering that your computer writes data in little-endian order. The lowest byte comes first. The fact that COM code uses BSTR to store strings is always well hidden by the CLR. It automatically converts them back and forth to System.String. Clearly something you should be aiming for instead of trying to understand the bytes. – Hans Passant Apr 15 '13 at 15:57
  • @HansPassant: What should I aim for then ? I have to interpret that HEX – Angshuman Agarwal Apr 15 '13 at 16:02
  • 1
    Using a COM server in C# typically starts with Project + Add Reference, COM tab or Browse tab. Whether that's appropriate is completely unclear from the question. It is the 98% case. With the remaining 2% invariably a lost cause without help from the vendor or author. – Hans Passant Apr 15 '13 at 16:11
  • How do you get this string (or this byte[]?) in C#? Show some code please. – Simon Mourier Apr 15 '13 at 16:24

1 Answers1

0

Use Marshal.PtrToStringBSTR to get an instance of a managed String from your BSTR.

Note that the IntPtr argument should be a pointer to the start of the string characters themselves, not the start of the 4 bytes which encode the length of the string.

Chris Dickson
  • 11,964
  • 1
  • 39
  • 60
  • Thanks. The BSTR is not in memory. As I already mentioned, the HEX string is being written into a file by the COM app. I read that into a CLR string. – Angshuman Agarwal Apr 16 '13 at 09:01
  • I can't see anywhere you said that the data was being written into a file. Anyway, even if it does start in a file, you can either read the whole data back into memory as a byte array, then do what I suggested; or just read the part after the length prefix and convert to a string using Unicode encoding. – Chris Dickson Apr 28 '13 at 13:24