String 转Byte[]编码格式出错

忧郁的火龙果 2016-02-29 10:58:49
程序代码片段如下:
    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的都发的不对,猜想是编码格式的问题,可是这个参数该怎么填呢,像上面这样写总是报错误。求指导!
...全文
251 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
忧郁的火龙果 2016-06-27
  • 打赏
  • 举报
回复
后来用ISO-8859-1做就好了,我也没想明白为什么,一个个试出来的。
haha_321 2016-03-03
  • 打赏
  • 举报
回复
用uft-8即可
tcmakebest 2016-03-02
  • 打赏
  • 举报
回复
一般都是用 utf-8 的啊,楼主如何知道要用 GBK 的呢,试一下用 gb2312
oyymiko 2016-03-02
  • 打赏
  • 举报
回复
之前有碰過這樣的問題,記得當時我是填 "UTF-8" !

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧