当前位置:首页 » 安卓系统 » androidstringbyte

androidstringbyte

发布时间: 2022-05-20 01:40:52

① Android蓝牙通信字节数组的数据类型转换求教!

觉得你这几个方法都要改写吧。
通常协议操作绝不能用String作为交换格式。
多次转码。导致数据变形,
特别是“同步头(2B) 包类型(1B) 数据长度(2B) ”
这个数据从byte[] ->String->byte[]多次转换,100%会导致数据变化。
通常只在byte[]上操作,改成
private byte[]getPackage();
private byte[] getHead(byte []);
sendMessage(byte[]);
这几个方法都改成byte[],不然即使强调硬扭弄对也有运气成分。

System.out.println("原head:"+Arrays.toString(head));
String t=new String(head,"GB2312")+"hello world";
System.out.println("合并gb文本:"+t);
System.out.println("还原的head:"+Arrays.toString(t.getBytes("gb2312")));
=========

② String转byte android

public void Stringtobyte(String a, byte[] bytes){
bytes=a.getBytes();
System.out.print(bytes);
}

③ Android 怎样用sqlite储存byte数组

在进行Android开发过程中,我们经常会接触到Drawable对象(官方开发文档:A Drawable is a general abstraction for "something that can be drawn."),那么,若要使用数据库来进行存储及读取.

@Override
public void onCreate(SQLiteDatabase database) {
executeSQLScript(database, "create.sql");
}

private void executeSQLScript(SQLiteDatabase database, string dbname){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
AssetManager assetManager = context.getAssets();
InputStream inputStream = null;

try{
inputStream = assetManager.open(dbname);
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
String[] createScript = outputStream.toString().split(";");
for (int i = 0; i < createScript.length; i++) {
String sqlStatement = createScript[i].trim();
// TODO You may want to parse out comments here
if (sqlStatement.length() > 0) {
database.execSQL(sqlStatement + ";");
}
}
} catch (IOException e){
// TODO Handle Script Failed to Load
} catch (SQLException e) {
// TODO Handle Script Failed to Execute
}
}

④ Android字节到字符串、字符串到字节、字节到流、流到字节字符串到流、流到字符串的转换过程,方法,技巧

挺多东西的, 三言两语真的讲不清楚, 500块跟你从头到尾的用代码演示一遍, 字符及各种流的用法使用使用场景, 字符方面的转换及使用方法技巧!

⑤ WebService返回byte[],在Android怎么才能接收byte[]啊,跪求大神

1、使用ISO 8859-1编码将字节数组转为字符串;

android接收到之后使用byte[] receive = String.getBytes("ISO 8859-1");


2、还可以将字节数组拼成字符串,用,符号隔开每个值。

Arrays.toString(byte[] b);可以将字节数组转成这种格式。

?

其实字节也是数值型。


java">importjava.io.UnsupportedEncodingException;
importjava.util.Arrays;
importjava.util.Random;
publicclassMainClass{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODO自动生成的方法存根
byte[]rand=newbyte[256];
newRandom().nextBytes(rand);
Stringtranslation=null;
try{
translation=newString(rand,"ISO-8859-1");
}catch(UnsupportedEncodingExceptione){
//TODO自动生成的catch块
e.printStackTrace();
}
System.out.println("要传输的内容:"+translation);
System.out.println("模拟传输过去...");
byte[]receive=null;
try{
receive=translation.getBytes("ISO-8859-1");
}catch(UnsupportedEncodingExceptione){
//TODO自动生成的catch块
e.printStackTrace();
}
System.out.println("传输前后的内容是否相同:"+Arrays.equals(rand,receive));
}
}

⑥ Android蓝牙通信字节数组的数据类型转换求教

觉得你这几个方法都要改写吧。通常协议操作绝不能用String作为交换格式。多次转码。导致数据变形,特别是“同步头(2B) 包类型(1B) 数据长度(2B) ”这个数据从byte[] ->String->byte[]多次转换,100%会导致数据变化。通常只在byte[]上操作,改成private byte[]getPackage();private byte[] getHead(byte []);sendMessage(byte[]);这几个方法都改成byte[],不然即使强调硬扭弄对也有运气成分。 System.out.println("原head:"+Arrays.toString(head));String t=new String(head,"GB2312")+"hello world";System.out.println("合并gb文本:"+t);System.out.println("还原的head:"+Arrays.toString(t.getBytes("gb2312")));=========

⑦ 做Android蓝牙通信,byte转int求教

传输的2种方式:
1. 16 进制传输, 可以打印字符, 所有的内容都是0x00-0xFF,转化为byte的时候直接把16进制转化为byte
2. AscII 转化 new String(byte), 比如特殊字符串abc , 转化为byte的时候取字符的ASCII码,转化为byte
1. 首先理解左移、右移:
1<<1 =2 : 1左移1位, 相当于乘2,左移多少位,乘多少个2
1: 0000 0001 1
<<1: 0000 0010 2,超出8位长度丢掉,左边补0

2>>1 = 1 : 右移, 右边补0
2: 0000 0010 2
2>>1 0000 0001 1
如何把int 转化为byte 数组
使用byteArrayOutputStream
使用左移、右移
2. 使用左右移动实现
App->Ble 小端存储:
public static byte[] int2BytesArray(int n) {
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (n >> (24 - i * 8));
}
byte[] transitionByte=new byte[4];
transitionByte[3]=b[0];
transitionByte[2]=b[1];
transitionByte[1]=b[2];
transitionByte[0]=b[3];
return transitionByte;
}
比如: 00001100 00001111 00001001 00000001 : 202311937【int】
使用小端存储: 低字节存储低字节 高字节存储高字节
i= 0 右移24个字节 00000000 00000000 00000000 00001100
转化为byte, 丢弃前3个字节, 00001100 高字节
transitionByte[3]=b[0]; 高字节存储高地址

transitionByte[2]:
i=1 右移动16 个字节 00000000 00000000 00001100 00001111
转化为byte, 丢弃前3个字节 00001111
最终:00001111
Ble->App 数据解析
byte[] sTime = new byte[4]; // 小端存储 byte[0] 存储低位

⑧ android蓝牙通信、byte转换方面的问题

觉得你这几个方法都要改写吧。
通常协议操作绝不能用String作为交换格式。
多次转码。导致数据变形,
特别是“同步头(2B) 包类型(1B) 数据长度(2B) ”
这个数据从byte[] ->String->byte[]多次转换,100%会导致数据变化。
通常只在byte[]上操作,改成
private byte[]getPackage();
private byte[] getHead(byte []);
sendMessage(byte[]);
这几个方法都改成byte[],不然即使强调硬扭弄对也有运气成分。

System.out.println("原head:"+Arrays.toString(head));
String t=new String(head,"GB2312")+"hello world";
System.out.println("合并gb文本:"+t);
System.out.println("还原的head:"+Arrays.toString(t.getBytes("gb2312")));
=========
原head:[85, -86, -32, -2, -36]
合并gb文本:U��hello world
还原的head:[85, 63, 63, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]

用还原后,前5个字节中出现负值的都完全变化,即根本不能用String作为字节的交换格式。

回问被吞了?我也没看到..

⑨ android 字符串转byte数组

Android 字符串、byte数组与16进制数组间的转换

<spanstyle="font-family:SimSun;font-size:14px;">//字符串转换成16进制文字列的方法
publicStringtoHex(Stringstr){
StringhexString="0123456789ABCDEF";
byte[]bytes=str.getBytes();
StringBuilderhex=newStringBuilder(bytes.length*2);
for(inti=0;i<bytes.length;i++){
hex.append(hexString.charAt((bytes[i]&0xf0)>>4));//作用同n/16
hex.append(hexString.charAt((bytes[i]&0x0f)>>0));//作用同n
hex.append('');//中间用空格隔开
}
returnhex.toString();
}

//将16进制数组转换为字符串
publicstaticStringdecode(Stringbytes){
StringhexString="0123456789ABCDEF";
ByteArrayOutputStreambaos=newByteArrayOutputStream(bytes.length()/2);
//将每2位16进制整数组装成一个字节
//for(inti=0;i<bytes.length();i+=2)
//baos.write((hexString.indexOf(bytes.charAt(i))<<4|hexString.indexOf(bytes.charAt(i+1))));
//将每3位(第3位为空格)中的前2位16进制整数组装成一个字节
for(inti=0;i<bytes.length();i+=3){
baos.write((hexString.indexOf(bytes.charAt(i))<<4|hexString.indexOf(bytes.charAt(i+1))));
}
returnnewString(baos.toByteArray());
}</span>

详细

热点内容
如何用方向键控制安卓机 发布:2024-05-17 16:38:11 浏览:197
雨田系统源码 发布:2024-05-17 16:28:06 浏览:585
新手直播脚本 发布:2024-05-17 16:27:25 浏览:846
python双引号单引号 发布:2024-05-17 16:19:31 浏览:947
0xxc语言 发布:2024-05-17 16:17:40 浏览:699
php与java的区别 发布:2024-05-17 16:12:48 浏览:339
registrar服务器地址是什么 发布:2024-05-17 16:11:46 浏览:112
订阅号助手如何找到密码 发布:2024-05-17 15:57:47 浏览:711
搜解压缩 发布:2024-05-17 15:38:32 浏览:764
水泵扬程算法 发布:2024-05-17 15:37:29 浏览:977