當前位置:首頁 » 安卓系統 » 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>

詳細

熱點內容
android仿微信底部菜單 發布:2024-05-03 04:09:34 瀏覽:694
LOL腳本識別 發布:2024-05-03 03:53:14 瀏覽:793
祁東福祥惠民卡初始密碼多少 發布:2024-05-03 03:36:02 瀏覽:247
王者什麼東西需要二級密碼 發布:2024-05-03 03:26:11 瀏覽:766
網頁界面升級訪問 發布:2024-05-03 03:26:06 瀏覽:210
安卓區怎麼更改充電提示音 發布:2024-05-03 03:23:56 瀏覽:48
遺傳演算法圖像分割 發布:2024-05-03 03:16:27 瀏覽:801
外圓圓弧怎麼編程 發布:2024-05-03 03:13:59 瀏覽:213
如何在conda中安裝需要編譯的軟體 發布:2024-05-03 02:41:21 瀏覽:780
易語言垃圾清理源碼 發布:2024-05-03 02:40:34 瀏覽:182