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?