views:

136

answers:

2

Hi,
I am trying to read an SMS message that I receive. The call to SmsReadMessage completes successfully and I get the message in a byte array. Here is the declaration for SmsReadMessage:

    [DllImport("sms.dll", SetLastError=true)]
    public static extern int SmsReadMessage(
        IntPtr smshHandle,
        byte[] psmsaSMSCAddress,
        byte[] psmsaSourceAddress,
        byte[] pstReceiveTime,
        byte[] pbBuffer,
        int dwBufferSize,
        byte[] pbProviderSpecificBuffer,
        int dwProviderSpecificDataBuffer,
        ref int pdwBytesRead);

Now I would like to convert the byte array to a string.
The following did not work for me (returns null):

Encoding.Unicode.GetString(pMessage, 0, Size);

How do I convert the byte array to string?
Should I change the declaration to something else that will be converted more easily? This is the function's signature.


Update: The following succeeds:

string test = "";
foreach (byte b in pMessage)
{
    if (b != 0)
    {
        test += Convert.ToChar(b);
    }
}

The dwSize parameter for the text message is 320 (the originally sent message is less than 160 characters).
This might be caused because of an incorrect reporting by the underlying RIL driver. This also could be the source of my original conversion problem.
I am not sure there is a terminating character at the end.
How can I confirm this?

+1  A: 

This question should be helpful: you can either use Microsoft.WindowsMobile.PocketOutlook namespace if you are targeting WM6 or you can use this library.

Since the Convert.ToChar(Byte) "succeeds", probably the message isn't Unicode (UTF-16) encoded. In UTF-16 you have two bytes per character. English characters will be mapped as '00 XX', that is a zero byte followed by the character value.

Probably UTF8 encoding is used. Try some other encodings to see what happens.

kgiannakakis
I removed the windows-mobile tag. It was there because I thought maybe somebody did it in mobile. Both links you gave are specific to WinMo.Thanks for the answer anyway.
Shaihi
The coding is Unicode. I had a (very) stupid mistake and that is why I thought I was receiving null as the result of the decoding. Thank you very much for the help.
Shaihi
A: 

I had a stupid mistake in assigning the returned string from the Decoding. It's too embarrassing to even post it here.
The above code in the question works properly.
Thanks for the support!

Shaihi

related questions