guangxian2012 发表于 2013-11-25 13:55:51

android端使用透明函数发送消息,c#端接收乱码问题

如题,我在使用透明通道api发送中文数据的时候,android发送,c#端接收乱码。请问这是怎么回事,如何解决呢

廖斌 发表于 2013-11-25 14:00:05

您好。透明通道函数只是提供一个数据通道,需要上层客户端提供统一的编码方式。是不是您android端和c#端采用的编码和解码的格式不一样呢?

guangxian2012 发表于 2013-11-25 14:39:02

廖斌 发表于 2013-11-25 14:00
您好。透明通道函数只是提供一个数据通道,需要上层客户端提供统一的编码方式。是不是您android端和c#端采 ...

哦,我的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(-16, data, data.length);
c#端接收是这样处理的 private void Received_TransBuffer(int userId, IntPtr buf, int len, int userValue)
{
            string m_Command =             Marshal.PtrToStringAnsi(IntPtr);
            MessageBox.Show(m_Command);
帮忙看下,谢谢

廖斌 发表于 2013-11-25 14:43:44

本帖最后由 廖斌 于 2013-11-25 14:45 编辑

guangxian2012 发表于 2013-11-25 14:39
哦,我的android发送这样的,c#端接收是这样处理的帮忙看下,谢谢
你的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;
            Marshal.Copy(buf, DATAS, 0, len);
            string m_Command = Encoding.UTF8.GetString(DATAS);

guangxian2012 发表于 2013-11-25 14:48:54

廖斌 发表于 2013-11-25 14:43
你的android采用的是UTF8编码格式发送,c#的解析却是默认的GB2312格式解析,这样是不对,需要发送端和接 ...

我刚才试了一下,c#端接收改成byte[] DATAS = new byte;

         Marshal.Copy(buf, DATAS, 0, len);

         string m_Command = Encoding.UTF8.GetString(DATAS);
就ok了。谢谢指导
页: [1]
查看完整版本: android端使用透明函数发送消息,c#端接收乱码问题