|
本帖最后由 廖斌 于 2013-11-25 14:45 编辑
你的android采用的是UTF8编码格式发送,c#的解析却是默认的GB2312格式解析,这样是不对,需要发送端和接收端采用统一的编码格式。下面提供两种比较常见的编码格式在anychat透明通道函数中的处理,请参考:
1、GB2312编码格式
android端发送代码:- String str = "透明通道测试";
- byte[] data = null;
- try {
- data = str.getBytes("GB2312");
- Log.i("ANYCHAT", "TRANSBUFFERSEND:"+str);
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- anychat.TransBuffer(-16, data, data.length);
复制代码 android端接收:- try {
- String str=new String(lpBuf, "GB2312");
- Log.i("ANYCHAT", "OnAnyChatTransBuffer:"+str);
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
复制代码 c#端发送:- byte[] buffesr = Encoding.Default.GetBytes("透明通道测试");
- AnyChatCoreSDK.TransBuffer(userId, buffesr, buffesr.Length);
复制代码 c#端接收- private void Received_TransBuffer(int userId, IntPtr buf, int len, int userValue)
- {
- string m_Command = Marshal.PtrToStringAnsi(IntPtr);
- MessageBox.Show(m_Command);
复制代码 2、UTF8编码格式
android端发送代码:- String str = "透明通道测试";
- byte[] data = null;
- try {
- data = str.getBytes("UTF8");
- Log.i("ANYCHAT", "TRANSBUFFERSEND:"+str);
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- anychat.TransBuffer(-15, data, data.length);
复制代码 android端接收代码:- public void OnAnyChatTransBuffer(int dwUserid, byte[] lpBuf, int dwLen) {
- try {
- String str=new String(lpBuf, "UTF8");
- Log.i("ANYCHAT", "OnAnyChatTransBuffer:"+str);
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
复制代码 c#端发送代码:- byte[] buffesr = Encoding.UTF8.GetBytes("透明通道测试");
- AnyChatCoreSDK.TransBuffer(userId, buffesr, buffesr.Length);
复制代码 c#端接收代码- private void Received_TransBuffer(int userId, IntPtr buf, int len, int userValue)
- {
- byte[] DATAS = new byte[len];
- Marshal.Copy(buf, DATAS, 0, len);
- string m_Command = Encoding.UTF8.GetString(DATAS);
复制代码 |
|