您的足迹:首页 > 蓝牙BLE >Android BLE 数据转换方法集合

Android BLE 数据转换方法集合

______________________________________________________________

Android BLE 框架发布,功能全面,简单易用:

https://github.com/a1anwang/okble

______________________________________________________________

(1)  byte[] 数组转成 16进制字符串


	private final static byte[] hex = "0123456789ABCDEF".getBytes();

	// 从字节数组到十六进制字符串转
	public static String Bytes2HexString(byte[] b) {
		byte[] buff = new byte[2 * b.length];
		for (int i = 0; i < b.length; i++) {
			buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
			buff[2 * i + 1] = hex[b[i] & 0x0f];
		}
		return new String(buff);
	}


        示例:

byte[] bytes_1=new byte[]{(byte) 0xA0,(byte) 0xB1,0x2};
String hexStr=Conversion.Bytes2HexString(bytes_1);
LogUtils.print("hexStr:"+hexStr);

结果:hexStr:A0B102


(2)  16进制字符串转成 byte[] 数组

    

public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
}


示例:

String strvalue = "A0B102"; 
byte[] bytes_2=Conversion.hexStringToBytes(strvalue);
String hexStr=Conversion.Bytes2HexString(bytes_2);
LogUtils.print("hexStr:"+hexStr);

结果:hexStr:A0B102


(3)  16进制字符串转成  int 整数

public static int hexStrToInt(String valueStr) {
		valueStr = valueStr.toUpperCase();
		if (valueStr.length() % 2 != 0) {
			valueStr = "0" + valueStr;
		}

		int returnValue = 0;

		int length = valueStr.length();

		for (int i = 0; i < length; i++) {

			int value = charToByte(valueStr.charAt(i));

			returnValue += Math.pow(16, length - i - 1) * value;
		}
		return returnValue;
}
public static byte charToByte(char c) {
  return (byte) "0123456789ABCDEF".indexOf(c);
}
示例:

String strvalue = "A0B102"; 
int  value=Conversion.hexStrToInt(strvalue);
LogUtils.print("       value:"+value);
LogUtils.print("0xA0B102:"+0xA0B102);

结果:
     hexStr:10531074
   0xA0B102:10531074


(4) int 转16进制

    

  public static String intToHexStr(int i){
		String hexStr=	Integer.toHexString(i);
		 
		if(hexStr.length()%2!=0){
			hexStr="0"+hexStr;
		}
		return hexStr.toUpperCase();
  }


    示例:

LogUtils.e("TAG", "hexStr:"+Conversion.intToHexStr(10));
    结果:
hexStr:0A

(5)  byte转bit

public static String byteToBit(byte b) {  
	    return "" +(byte)((b >> 7) & 0x1) +   
	    (byte)((b >> 6) & 0x1) +   
	    (byte)((b >> 5) & 0x1) +   
	    (byte)((b >> 4) & 0x1) +   
	    (byte)((b >> 3) & 0x1) +   
	    (byte)((b >> 2) & 0x1) +   
	    (byte)((b >> 1) & 0x1) +   
	    (byte)((b >> 0) & 0x1);  
}  
(6)  bitbyte


public static byte BitToByte(String byteStr) {  
	    int re, len;  
	    if (null == byteStr) {  
	        return 0;  
	    }  
	    len = byteStr.length();  
	    if (len != 4 && len != 8) {  
	        return 0;  
	    }  
	    if (len == 8) {// 8 bit处理  
	        if (byteStr.charAt(0) == '0') {// 正数  
	            re = Integer.parseInt(byteStr, 2);  
	        } else {// 负数  
	            re = Integer.parseInt(byteStr, 2) - 256;  
	        }  
	    } else {//4 bit处理  
	        re = Integer.parseInt(byteStr, 2);  
	    }  
	    return (byte) re;  
} 

(7)  byte[]截取


    public static byte[] subByteArray(byte[] src,int start,int length){


        byte[] result=new byte[length];

        System.arraycopy(src, start, result, 0, length);


        return result;

    }





本博客所有文章如无特别注明均为原创。作者:AlanWang复制或转载请以超链接形式注明转自 AlanWang的博客-专注android和蓝牙BLE技术分享
原文地址《Android BLE 数据转换方法集合

相关推荐

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交

网友评论(29)

primperan 10 mg
<a href= http://tyronbizzl.startbewijs.nl >atenolol gador 50 mg precio</a>
<a href=http://leonelgoss.chapso.de>esertia vademecum</a>
calcitriol precio
<a href= http://loidaelgin.oneminutesite.it >seroxat cr glaxosmithkline</a>
<a href=http://lonvann.startspot.nl>celecoxib vademecum</a>
esertia 20 mg para que sirve
DouglasBed 1年前 (2017-03-27) 回复
esertia principio activo
<a href= http://carissagas.spruz.com >celecoxib 200 mg precio argentina</a>
<a href=http://leonesurwi.webblogg.se>seroxat 20 vademecum</a>
esertia 15 precio
<a href= http://francenefl.jigsy.com >esertia gotas</a>
<a href=http://www.devote.se/antionette>como tomar progyluton tabletas</a>
esertia precio mexico
JoshuaPlarf 1年前 (2017-03-26) 回复
strattera erfahrungen erwachsene
<a href= http://gaylaairol.guildomatic.com >clindamycin creme rezeptfrei</a>
<a href=http://claytonnul-blog.logdown.com/posts/1619351-claytonnul>chloramphenicol pumpspray hund</a>
zyban preise 100 st
<a href= http://claytonnul-blog.logdown.com/posts/1619351-claytonnul >chloramphenicol spray für katzen</a>
<a href=http://claytonnul-blog.logdown.com/posts/1619351-claytonnul>chloramphenicol pumpspray bestellen</a>
zyban beipackzettel
DonaldCop 1年前 (2017-03-21) 回复
dalacine vidal
<a href= http://www.onweb.fr/?company_id=17314 >diurizone poudre achat</a>
<a href=http://claytonver.wifeo.com>bisacodyl 5 mg</a>
prix dalacine 600
<a href= http://claytonver.wifeo.com >bisacodyl 10 mg czopki</a>
<a href=https://eliseoscar.jimdo.com>zithromax monodose</a>
amoxival 200 notice
Johnnyevone 1年前 (2017-03-19) 回复
cefixime 400 mg dosage
<a href= http://lucienklem.e-monsite.com >zimox posologia</a>
<a href=http://pamelaentw.bloggsite.se>nolvadex ginecomastia tratamento</a>
levodopa sinemet levodopa e carbidopa
<a href= http://lynellebau.eklablog.com >zyban 150 mg preço</a>
<a href=http://darenlintn.jigsy.com>famciclovir effetti collaterali</a>
zimox posologia gravidanza
Eddiecroca 1年前 (2017-03-18) 回复
bisacodyl side effects
<a href= http://lorettacat.postbit.com/glucophage.html >glucophage 500</a>
<a href=http://rolfsprung.spruz.com>zithromax monodose et alcool</a>
diurizone
<a href= http://www.dalacine-27.webself.net >dalacine 600 vidal</a>
<a href=http://nouw.com/robertosch/dimazon-29437023>dimazon 40 chien</a>
dimazon 10 mg prix
Stevenral 1年前 (2017-03-18) 回复
resochin nebenwirkungen rheuma
<a href= http://dianebrent.blogg.no >cipramil 10 mg preis</a>
<a href=http://sharenrowc.blogse.nl>gabapentin micro labs 300 mg erfahrungsberichte</a>
azithromycin 500 hec
<a href= http://markusvalr.1apps.com >zineryt rezeptfrei bestellen</a>
<a href=http://tonetteelv.ucoz.pl>azithromycin halbwertszeit</a>
resochin malaria tropica
JosephRUP 1年前 (2017-03-17) 回复
gabapentin neuropathischer schmerz dosis  
<a href= http://tammerarep.chapso.de >cipramil 10</a> <a href= http://lacyphlegm.cybersite.nu >avamys contiene cortisone</a>  
azithromycin milch  
<a href=http://zofiahager.asso-web.com>zineryt pret</a> <a href=http://tammerarep.chapso.de>cipramil 10mg</a>  
azithromycin 500 n2
DavidDex 1年前 (2017-03-16) 回复
avamys for sinusitis  
<a href= http://sherwoodba.over-blog.com/avamys.html >avamys for sinusitis</a> <a href= http://markusvalr.1apps.com >zineryt creme</a>  
nifurantin b6 wirkung  
<a href=http://sherwoodba.over-blog.com/avamys.html>avamys preis 60 hub</a> <a href=http://tonetteelv.ucoz.pl>azithromycin saft hexal</a>  
aknemycin
Raymondreino 1年前 (2017-03-16) 回复
www yasmin  
<a href= http://elwoodroeg.blogse.nl >orlistat xenical alli</a> <a href= http://conniegild.blogg.no >xenical</a>  
yasmin and breastfeeding  
<a href=http://eliseodela.over-blog.com/2017/03/yasmin.html>generic of yasmin birth control</a> <a href=http://gabriellah.soup.io>xenical orlistat buy online</a>  
yasmin online
RobertCox 1年前 (2017-03-13) 回复
1 2 3