androidbyte数组使用_android byte的用法
byte 為有符號(hào)數(shù)據(jù),引用文章
android byte的使用
暴走鄰家 2018-01-24 08:55:07
10333
收藏 4
分類專欄: android Android基礎(chǔ) 文章標(biāo)簽: android app java byte 函數(shù)
版權(quán)
今天,簡(jiǎn)單講講android里byte的使用。
這個(gè)其實(shí)很簡(jiǎn)單,但是自己覺(jué)得一直沒(méi)有完全弄明白,所以記錄一下。
byte即字節(jié)的意思,是java中的基本類型,用心申明字節(jié)型的變量。
通常在讀取非文本文件時(shí)(如圖片,聲音,可執(zhí)行文件)需要用字節(jié)數(shù)組來(lái)保存文件的內(nèi)容,在下載文件時(shí),也是用byte數(shù)組作臨時(shí)的緩沖器接收文件內(nèi)容。所以說(shuō)byte在文件操作時(shí)是必不可少的。不管是對(duì)文件寫入還是讀取都要用到。
byte在java中是一種是數(shù)據(jù)類型,代表一個(gè)字節(jié),一個(gè)字節(jié)包含8個(gè)位,所以,byte類型的取值范圍為-128到127。
在某些程序中(尤其是和硬件有關(guān)的程序)會(huì)將某些數(shù)據(jù)存儲(chǔ)到字節(jié)類型的變量中,比如00110010,其中每個(gè)位都代表一個(gè)參數(shù),然后以位運(yùn)算的方式對(duì)參數(shù)進(jìn)行取值和賦值操作。
下面介紹一些byte的相關(guān)函數(shù):
一、實(shí)現(xiàn)功能
1、int與byte互轉(zhuǎn)
2、int與byte[]互轉(zhuǎn)
3、short與byte互轉(zhuǎn)
4、short與byte[]互轉(zhuǎn)
5、16位short與byte[]互轉(zhuǎn)
6、long與byte[]互轉(zhuǎn)
7、byte[]與inputstream互轉(zhuǎn)
8、byte與String互轉(zhuǎn)
9、16進(jìn)制字符轉(zhuǎn)int
10、十進(jìn)制轉(zhuǎn)2進(jìn)制
11、byte[]轉(zhuǎn)16進(jìn)制字符
12、byte[]數(shù)組指定位置抽取byte[]
二、代碼實(shí)現(xiàn)
package cc.eguid.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
/**
* 基本數(shù)據(jù)互轉(zhuǎn)工具
* @author eguid
eguid的官網(wǎng):http://www.eguid.cc
*eguid的csdn博客:http://blog.csdn.net/eguid_1,博客園:http://www.cnblogs.com/eguid
*/
public class ByteUtil {
private static ByteBuffer buffer = ByteBuffer.allocate(8);
/**
* int轉(zhuǎn)byte
* @param x
* @return
*/
public static byte intToByte(int x) {
return (byte) x;
}
/**
* byte轉(zhuǎn)int
* @param b
* @return
*/
public static int byteToInt(byte b) {
//Java的byte是有符號(hào),通過(guò) &0xFF轉(zhuǎn)為無(wú)符號(hào)
return b & 0xFF;
}
/**
* byte[]轉(zhuǎn)int
* @param b
* @return
*/
public static int byteArrayToInt(byte[] b) {
return b[3] & 0xFF |
(b[2] & 0xFF) << 8 |
(b[1] & 0xFF) << 16 |
(b[0] & 0xFF) << 24;
}
public static int byteArrayToInt(byte[] b, int index){
return b[index+3] & 0xFF |
(b[index+2] & 0xFF) << 8 |
(b[index+1] & 0xFF) << 16 |
(b[index+0] & 0xFF) << 24;
}
/**
* int轉(zhuǎn)byte[]
* @param a
* @return
*/
public static byte[] intToByteArray(int a) {
return new byte[] {
(byte) ((a >> 24) & 0xFF),
(byte) ((a >> 16) & 0xFF),
(byte) ((a >> 8) & 0xFF),
(byte) (a & 0xFF)
};
}
/**
* short轉(zhuǎn)byte[]
*
* @param b
* @param s
* @param index
*/
public static void byteArrToShort(byte b[], short s, int index) {
b[index + 1] = (byte) (s >> 8);
b[index + 0] = (byte) (s >> 0);
}
/**
* byte[]轉(zhuǎn)short
*
* @param b
* @param index
* @return
*/
public static short byteArrToShort(byte[] b, int index) {
return (short) (((b[index + 0] << 8) | b[index + 1] & 0xff));
}
/**
* 16位short轉(zhuǎn)byte[]
*
* @param s
* short
* @return byte[]
* */
public static byte[] shortToByteArr(short s) {
byte[] targets = new byte[2];
for (int i = 0; i < 2; i++) {
int offset = (targets.length - 1 - i) * 8;
targets[i] = (byte) ((s >>> offset) & 0xff);
}
return targets;
}
/**
* byte[]轉(zhuǎn)16位short
* @param b
* @return
*/
public static short byteArrToShort(byte[] b){
return byteArrToShort(b,0);
}
/**
* long轉(zhuǎn)byte[]
* @param x
* @return
*/
public static byte[] longToBytes(long x) {
buffer.putLong(0, x);
return buffer.array();
}
/**
* byte[]轉(zhuǎn)Long
* @param bytes
* @return
*/
public static long bytesToLong(byte[] bytes) {
buffer.put(bytes, 0, bytes.length);
buffer.flip();//need flip
return buffer.getLong();
}
/**
* 從byte[]中抽取新的byte[]
* @param data - 元數(shù)據(jù)
* @param start - 開始位置
* @param end - 結(jié)束位置
* @return 新byte[]
*/
public static byte[] getByteArr(byte[]data,int start ,int end){
byte[] ret=new byte[end-start];
for(int i=0;(start+i)<end;i++){
ret[i]=data[start+i];
}
return ret;
}
/**
* 流轉(zhuǎn)換為byte[]
* @param inStream
* @return
*/
public static byte[] readInputStream(InputStream inStream) {
ByteArrayOutputStream outStream = null;
try {
outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
byte[] data = null;
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
data = outStream.toByteArray();
return data;
}catch (IOException e) {
return null;
} finally {
try {
if (outStream != null) {
outStream.close();
}
if (inStream != null) {
inStream.close();
}
} catch (IOException e) {
return null;
}
}
}
/**
* byte[]轉(zhuǎn)inputstream
* @param b
* @return
*/
public static InputStream readByteArr(byte[] b){
return new ByteArrayInputStream(b);
}
/**
* byte數(shù)組內(nèi)數(shù)字是否相同
* @param s1
* @param s2
* @return
*/
public static boolean isEq(byte[] s1,byte[] s2){
int slen=s1.length;
if(slen==s2.length){
for(int index=0;index<slen;index++){
if(s1[index]!=s2[index]){
return false;
}
}
return true;
}
return false;
}
/**
* byte數(shù)組轉(zhuǎn)換為Stirng
* @param s1 -數(shù)組
* @param encode -字符集
* @param err -轉(zhuǎn)換錯(cuò)誤時(shí)返回該文字
* @return
*/
public static String getString(byte[] s1,String encode,String err){
try {
return new String(s1,encode);
} catch (UnsupportedEncodingException e) {
return err==null?null:err;
}
}
/**
* byte數(shù)組轉(zhuǎn)換為Stirng
* @param s1-數(shù)組
* @param encode-字符集
* @return
*/
public static String getString(byte[] s1,String encode){
return getString(s1,encode,null);
}
//測(cè)試
public static void main(String []args){
System.err.println(isEq(new byte[]{1,2},new byte[]{1,2}));
}
/**
* 字節(jié)數(shù)組轉(zhuǎn)16進(jìn)制字符串
* @param b
* @return
*/
public static String byteArrToHexString(byte[] b){
String result="";
for (int i=0; i < b.length; i++) {
result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring(1);
}
return result;
}
/**
* 16進(jìn)制字符創(chuàng)轉(zhuǎn)int
* @param hexString
* @return
*/
public static int hexStringToInt(String hexString){
return Integer.parseInt(hexString,16);
}
/**
* 十進(jìn)制轉(zhuǎn)二進(jìn)制
* @param i
* @return
*/
public static String intToBinary(int i){
return Integer.toBinaryString(i);
}
}
android byte的使用就講完了
就這么簡(jiǎn)單。
總結(jié)
以上是生活随笔為你收集整理的androidbyte数组使用_android byte的用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java.lang.IllegalArg
- 下一篇: 网络发现协议服务器,关于 DHCP 服务