byte 为有符号数据,引用文章

android byte的使用

暴走邻家 2018-01-24 08:55:07

10333

收藏 4
分类专栏: android Android基础 文章标签: android app java byte 函数
版权

今天,简单讲讲android里byte的使用。
这个其实很简单,但是自己觉得一直没有完全弄明白,所以记录一下。
byte即字节的意思,是java中的基本类型,用心申明字节型的变量。
通常在读取非文本文件时(如图片,声音,可执行文件)需要用字节数组来保存文件的内容,在下载文件时,也是用byte数组作临时的缓冲器接收文件内容。所以说byte在文件操作时是必不可少的。不管是对文件写入还是读取都要用到。
byte在java中是一种是数据类型,代表一个字节,一个字节包含8个位,所以,byte类型的取值范围为-128到127。
在某些程序中(尤其是和硬件有关的程序)会将某些数据存储到字节类型的变量中,比如00110010,其中每个位都代表一个参数,然后以位运算的方式对参数进行取值和赋值操作。
下面介绍一些byte的相关函数:
一、实现功能
1、int与byte互转
2、int与byte[]互转
3、short与byte互转
4、short与byte[]互转
5、16位short与byte[]互转
6、long与byte[]互转
7、byte[]与inputstream互转
8、byte与String互转
9、16进制字符转int
10、十进制转2进制
11、byte[]转16进制字符
12、byte[]数组指定位置抽取byte[]
二、代码实现

  1. package cc.eguid.util;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.UnsupportedEncodingException;
  7. import java.nio.ByteBuffer;
  8. /**
  9. * 基本数据互转工具
  10. * @author eguid
  11. eguid的官网:http://www.eguid.cc
  12. *eguid的csdn博客:http://blog.csdn.net/eguid_1,博客园:http://www.cnblogs.com/eguid
  13. */
  14. public class ByteUtil {
  15. private static ByteBuffer buffer = ByteBuffer.allocate(8);
  16. /**
  17. * int转byte
  18. * @param x
  19. * @return
  20. */
  21. public static byte intToByte(int x) {
  22. return (byte) x;
  23. }
  24. /**
  25. * byte转int
  26. * @param b
  27. * @return
  28. */
  29. public static int byteToInt(byte b) {
  30. //Java的byte是有符号,通过 &0xFF转为无符号
  31. return b & 0xFF;
  32. }
  33. /**
  34. * byte[]转int
  35. * @param b
  36. * @return
  37. */
  38. public static int byteArrayToInt(byte[] b) {
  39. return b[3] & 0xFF |
  40. (b[2] & 0xFF) << 8 |
  41. (b[1] & 0xFF) << 16 |
  42. (b[0] & 0xFF) << 24;
  43. }
  44. public static int byteArrayToInt(byte[] b, int index){
  45. return b[index+3] & 0xFF |
  46. (b[index+2] & 0xFF) << 8 |
  47. (b[index+1] & 0xFF) << 16 |
  48. (b[index+0] & 0xFF) << 24;
  49. }
  50. /**
  51. * int转byte[]
  52. * @param a
  53. * @return
  54. */
  55. public static byte[] intToByteArray(int a) {
  56. return new byte[] {
  57. (byte) ((a >> 24) & 0xFF),
  58. (byte) ((a >> 16) & 0xFF),
  59. (byte) ((a >> 8) & 0xFF),
  60. (byte) (a & 0xFF)
  61. };
  62. }
  63. /**
  64. * short转byte[]
  65. *
  66. * @param b
  67. * @param s
  68. * @param index
  69. */
  70. public static void byteArrToShort(byte b[], short s, int index) {
  71. b[index + 1] = (byte) (s >> 8);
  72. b[index + 0] = (byte) (s >> 0);
  73. }
  74. /**
  75. * byte[]转short
  76. *
  77. * @param b
  78. * @param index
  79. * @return
  80. */
  81. public static short byteArrToShort(byte[] b, int index) {
  82. return (short) (((b[index + 0] << 8) | b[index + 1] & 0xff));
  83. }
  84. /**
  85. * 16位short转byte[]
  86. *
  87. * @param s
  88. * short
  89. * @return byte[]
  90. * */
  91. public static byte[] shortToByteArr(short s) {
  92. byte[] targets = new byte[2];
  93. for (int i = 0; i < 2; i++) {
  94. int offset = (targets.length - 1 - i) * 8;
  95. targets[i] = (byte) ((s >>> offset) & 0xff);
  96. }
  97. return targets;
  98. }
  99. /**
  100. * byte[]转16位short
  101. * @param b
  102. * @return
  103. */
  104. public static short byteArrToShort(byte[] b){
  105. return byteArrToShort(b,0);
  106. }
  107. /**
  108. * long转byte[]
  109. * @param x
  110. * @return
  111. */
  112. public static byte[] longToBytes(long x) {
  113. buffer.putLong(0, x);
  114. return buffer.array();
  115. }
  116. /**
  117. * byte[]转Long
  118. * @param bytes
  119. * @return
  120. */
  121. public static long bytesToLong(byte[] bytes) {
  122. buffer.put(bytes, 0, bytes.length);
  123. buffer.flip();//need flip
  124. return buffer.getLong();
  125. }
  126. /**
  127. * 从byte[]中抽取新的byte[]
  128. * @param data - 元数据
  129. * @param start - 开始位置
  130. * @param end - 结束位置
  131. * @return 新byte[]
  132. */
  133. public static byte[] getByteArr(byte[]data,int start ,int end){
  134. byte[] ret=new byte[end-start];
  135. for(int i=0;(start+i)<end;i++){
  136. ret[i]=data[start+i];
  137. }
  138. return ret;
  139. }
  140. /**
  141. * 流转换为byte[]
  142. * @param inStream
  143. * @return
  144. */
  145. public static byte[] readInputStream(InputStream inStream) {
  146. ByteArrayOutputStream outStream = null;
  147. try {
  148. outStream = new ByteArrayOutputStream();
  149. byte[] buffer = new byte[1024];
  150. byte[] data = null;
  151. int len = 0;
  152. while ((len = inStream.read(buffer)) != -1) {
  153. outStream.write(buffer, 0, len);
  154. }
  155. data = outStream.toByteArray();
  156. return data;
  157. }catch (IOException e) {
  158. return null;
  159. } finally {
  160. try {
  161. if (outStream != null) {
  162. outStream.close();
  163. }
  164. if (inStream != null) {
  165. inStream.close();
  166. }
  167. } catch (IOException e) {
  168. return null;
  169. }
  170. }
  171. }
  172. /**
  173. * byte[]转inputstream
  174. * @param b
  175. * @return
  176. */
  177. public static InputStream readByteArr(byte[] b){
  178. return new ByteArrayInputStream(b);
  179. }
  180. /**
  181. * byte数组内数字是否相同
  182. * @param s1
  183. * @param s2
  184. * @return
  185. */
  186. public static boolean isEq(byte[] s1,byte[] s2){
  187. int slen=s1.length;
  188. if(slen==s2.length){
  189. for(int index=0;index<slen;index++){
  190. if(s1[index]!=s2[index]){
  191. return false;
  192. }
  193. }
  194. return true;
  195. }
  196. return false;
  197. }
  198. /**
  199. * byte数组转换为Stirng
  200. * @param s1 -数组
  201. * @param encode -字符集
  202. * @param err -转换错误时返回该文字
  203. * @return
  204. */
  205. public static String getString(byte[] s1,String encode,String err){
  206. try {
  207. return new String(s1,encode);
  208. } catch (UnsupportedEncodingException e) {
  209. return err==null?null:err;
  210. }
  211. }
  212. /**
  213. * byte数组转换为Stirng
  214. * @param s1-数组
  215. * @param encode-字符集
  216. * @return
  217. */
  218. public static String getString(byte[] s1,String encode){
  219. return getString(s1,encode,null);
  220. }
  221. //测试
  222. public static void main(String []args){
  223. System.err.println(isEq(new byte[]{1,2},new byte[]{1,2}));
  224. }
  225. /**
  226. * 字节数组转16进制字符串
  227. * @param b
  228. * @return
  229. */
  230. public static String byteArrToHexString(byte[] b){
  231. String result="";
  232. for (int i=0; i < b.length; i++) {
  233. result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring(1);
  234. }
  235. return result;
  236. }
  237. /**
  238. * 16进制字符创转int
  239. * @param hexString
  240. * @return
  241. */
  242. public static int hexStringToInt(String hexString){
  243. return Integer.parseInt(hexString,16);
  244. }
  245. /**
  246. * 十进制转二进制
  247. * @param i
  248. * @return
  249. */
  250. public static String intToBinary(int i){
  251. return Integer.toBinaryString(i);
  252. }
  253. }

android byte的使用就讲完了
就这么简单。

android byte的使用_谢岩的博客-CSDN博客​blog.csdn.net

androidbyte数组使用_android byte的用法相关推荐

  1. php 数组重新打乱_PHP 将数组打乱 shuffle函数的用法及简单实例

    shuffle() PHP shuffle() 函数随机排列数组单元的顺序(将数组打乱).本函数为数组中的单元赋予新的键名,这将删除原有的键名而不仅是重新排序. 语法: bool shuffle ( ...

  2. php arrayaccess 二维,PHP数组式访问接口ArrayAccess用法分析

    本文实例讲述了PHP数组式访问接口ArrayAccess用法.分享给大家供大家参考,具体如下: PHP  ArrayAccess接口又叫数组式访问接口,该接口的作用是提供像访问数组一样访问对象的能力. ...

  3. java int转byte数组_javaint转byte数组

    JAVA中怎么将int数据转换为byte数据? 例如将int b=325转换为 byte a,结果为多少啊? JAVA中根据以下代码将int数据转换为byte数据: public static byt ...

  4. bytearray java_详解Java中ByteArray字节数组的输入输出流的用法

    ByteArrayInputStream 介绍ByteArrayInputStream 是字节数组输入流.它继承于InputStream. 它包含一个内部缓冲区,该缓冲区包含从流中读取的字节:通俗点说 ...

  5. java编程int和byte的用法_Java中的Byte Array和Int转换

    Java中的Byte Array和Int转换 我有这两个函数有一些困难: byteArrayToInt和intToByteArray . 问题是,如果我使用另一个来得到另一个结果,结果是不同的,你可以 ...

  6. C++对象数组与对象指针的用法【C++初学面向对象编程】

    文章目录 一.对象数组 二.对象指针 一.对象数组 1.概念 对象数组本质上就是:数组中每一个元素都是同一个类的对象. //定义对象数组(装有3个对象的数组) Box aa[3]={Box(1, 1, ...

  7. jq添加数组_jquery中push()的用法(数组添加元素)

    push定义和用法 push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. 语法 arrayObject.push(newelement1,newelement2,....,newele ...

  8. numpy数组扩展函数repeat和tile用法

    numpy数组扩展函数有repeat和tile,由于数组不能进行动态扩展,故函数调用之后都重新分配新的空间来存储扩展后的数据. repeat函数功能:对数组中的元素进行连续重复复制 用法有两种: 1) ...

  9. java byte数组操作_Java byte数组操纵方式代码实例解析

    字节数组的关键在于它为存储在该部分内存中的每个8位值提供索引(快速),精确的原始访问,并且您可以对这些字节进行操作以控制每个位. 坏处是计算机只将每个条目视为一个独立的8位数 - 这可能是你的程序正在 ...

最新文章

  1. 1925亿美元,中国仍是世界最大芯片市场!2022全球半导体行业报告出炉
  2. SQL Server:定时作业的设置方法
  3. HDU 1181 变形课(dfs)
  4. [luogu P4198] 楼房重建(线段树 + 思维)
  5. Hadoop生态hive(一)介绍
  6. 【转】Microsoft Graph Web应用程序极致开发体验
  7. 骑车与走路(信息学奥赛一本通-T1050)
  8. MUI框架 按钮点击响应不好的问题解决办法
  9. PHP图片验证码制作(上)
  10. 2017-2018-1 20155202 《信息安全系统设计基础》第4周学习总结
  11. C++11 列表初始化
  12. 一键破解宝塔面板的专业版本权限
  13. 20P44 Premiere预设600个摄像机动画信号干扰调色视觉特效pr模板
  14. MySQL——基础知识
  15. 中国浙江省动漫产业发展方向探析及投资决策建议报告2021-2027年
  16. 【交换机】交换机简介
  17. 【AirSim】Windows下搭建AirSim
  18. Latex角度输入(°)
  19. PhotonServer中日志的配置
  20. 论文学习:基于集成YOLO算法的蝴蝶检测与分类

热门文章

  1. Debug和Realease版本的区别
  2. Kafka(六)Kafka基本客户端命令操作
  3. 5分钟入门Lindorm SearchIndex
  4. 微服务+异步工作流+ Serverless,Netflix 决定弃用稳定运行 7 年的旧平台
  5. 存储计算解耦合,构建中国人英语语音数据库
  6. Hive 终于等来了 Flink
  7. 高德在提升定位精度方面的探索和实践
  8. 原理解析 | 深入了解 Apache Flink 的网络协议栈
  9. 代码整洁之道(一)最佳实践小结 1
  10. 直播连麦贾扬清,谈谈他所理解的四大 AI 落地问题 | 攻“疫”技术公开课