String 转Byte[]编码格式出错

Android 码拜 8年前 (2016-03-29) 1410次浏览
程序代码片段如下:

    private void sendMessage(String message) {
        // Check that we"re actually connected before trying anything
        if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
            Toast.makeText(getActivity(), R.string.not_connected, Toast.LENGTH_SHORT).show();
            return;
        }
        // Check that there"s actually something to send
        if (message.length() > 0) {
            // Get the message bytes and tell the BluetoothChatService to write
            byte[] send = message.getBytes("GBK"); /*************error**************/
            mChatService.write(send);
            // Reset out string buffer to zero and clear the edit text field
            mOutStringBuffer.setLength(0);
            mOutEditText.setText(mOutStringBuffer);
        }
    }

byte[] send = message.getBytes(“GBK”); 这一句报错;
原来是不带参数,这样子发出去的内容出错,大于0x7F的都发的不对,猜想是编码格式的问题,可是这个参数该怎么填呢,像上面这样写总是报错误。求指导!

解决方案

15

之前有碰過這樣的問題,記得當時本人是填 “UTF-8” !

10

一般都是用 utf-8 的啊,题主怎么样知道要用 GBK 的呢,试一下用 gb2312

15

用uft-8即可

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明String 转Byte[]编码格式出错
喜欢 (0)
[1034331897@qq.com]
分享 (0)