java byte数组类_【Java】字节数组转换工具类

1 importorg.apache.commons.lang.ArrayUtils;2

3 importjava.nio.charset.Charset;4

5 /**

6 * 字节数组转换工具类7 */

8 public classBytesUtils {9

10 public static final String GBK = "GBK";11 public static final String UTF8 = "utf-8";12 public static final char[] ascii = "0123456789ABCDEF".toCharArray();13 private static char[] HEX_VOCABLE = { '0', '1', '2', '3', '4', '5', '6',14 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};15

16 /**

17 * 将short整型数值转换为字节数组18 *19 *@paramdata20 *@return

21 */

22 public static byte[] getBytes(shortdata) {23 byte[] bytes = new byte[2];24 bytes[0] = (byte) ((data & 0xff00) >> 8);25 bytes[1] = (byte) (data & 0xff);26 returnbytes;27 }28

29 /**

30 * 将字符转换为字节数组31 *32 *@paramdata33 *@return

34 */

35 public static byte[] getBytes(chardata) {36 byte[] bytes = new byte[2];37 bytes[0] = (byte) (data >> 8);38 bytes[1] = (byte) (data);39 returnbytes;40 }41

42 /**

43 * 将布尔值转换为字节数组44 *45 *@paramdata46 *@return

47 */

48 public static byte[] getBytes(booleandata) {49 byte[] bytes = new byte[1];50 bytes[0] = (byte) (data ? 1 : 0);51 returnbytes;52 }53

54 /**

55 * 将整型数值转换为字节数组56 *57 *@paramdata58 *@return

59 */

60 public static byte[] getBytes(intdata) {61 byte[] bytes = new byte[4];62 bytes[0] = (byte) ((data & 0xff000000) >> 24);63 bytes[1] = (byte) ((data & 0xff0000) >> 16);64 bytes[2] = (byte) ((data & 0xff00) >> 8);65 bytes[3] = (byte) (data & 0xff);66 returnbytes;67 }68

69 /**

70 * 将long整型数值转换为字节数组71 *72 *@paramdata73 *@return

74 */

75 public static byte[] getBytes(longdata) {76 byte[] bytes = new byte[8];77 bytes[0] = (byte) ((data >> 56) & 0xff);78 bytes[1] = (byte) ((data >> 48) & 0xff);79 bytes[2] = (byte) ((data >> 40) & 0xff);80 bytes[3] = (byte) ((data >> 32) & 0xff);81 bytes[4] = (byte) ((data >> 24) & 0xff);82 bytes[5] = (byte) ((data >> 16) & 0xff);83 bytes[6] = (byte) ((data >> 8) & 0xff);84 bytes[7] = (byte) (data & 0xff);85 returnbytes;86 }87

88 /**

89 * 将float型数值转换为字节数组90 *91 *@paramdata92 *@return

93 */

94 public static byte[] getBytes(floatdata) {95 int intBits =Float.floatToIntBits(data);96 returngetBytes(intBits);97 }98

99 /**

100 * 将double型数值转换为字节数组101 *102 *@paramdata103 *@return

104 */

105 public static byte[] getBytes(doubledata) {106 long intBits =Double.doubleToLongBits(data);107 returngetBytes(intBits);108 }109

110 /**

111 * 将字符串按照charsetName编码格式的字节数组112 *113 *@paramdata114 * 字符串115 *@paramcharsetName116 * 编码格式117 *@return

118 */

119 public static byte[] getBytes(String data, String charsetName) {120 Charset charset =Charset.forName(charsetName);121 returndata.getBytes(charset);122 }123

124 /**

125 * 将字符串按照GBK编码格式的字节数组126 *127 *@paramdata128 *@return

129 */

130 public static byte[] getBytes(String data) {131 returngetBytes(data, GBK);132 }133

134 /**

135 * 将字节数组第0字节转换为布尔值136 *137 *@parambytes138 *@return

139 */

140 public static boolean getBoolean(byte[] bytes) {141 return bytes[0] == 1;142 }143

144 /**

145 * 将字节数组的第index字节转换为布尔值146 *147 *@parambytes148 *@paramindex149 *@return

150 */

151 public static boolean getBoolean(byte[] bytes, intindex) {152 return bytes[index] == 1;153 }154

155 /**

156 * 将字节数组前2字节转换为short整型数值157 *158 *@parambytes159 *@return

160 */

161 public static short getShort(byte[] bytes) {162 return (short) ((0xff00 & (bytes[0] << 8)) | (0xff & bytes[1]));163 }164

165 /**

166 * 将字节数组从startIndex开始的2个字节转换为short整型数值167 *168 *@parambytes169 *@paramstartIndex170 *@return

171 */

172 public static short getShort(byte[] bytes, intstartIndex) {173 return (short) ((0xff00 & (bytes[startIndex] << 8)) | (0xff & bytes[startIndex + 1]));174 }175

176 /**

177 * 将字节数组前2字节转换为字符178 *179 *@parambytes180 *@return

181 */

182 public static char getChar(byte[] bytes) {183 return (char) ((0xff00 & (bytes[0] << 8)) | (0xff & bytes[1]));184 }185

186 /**

187 * 将字节数组从startIndex开始的2个字节转换为字符188 *189 *@parambytes190 *@paramstartIndex191 *@return

192 */

193 public static char getChar(byte[] bytes, intstartIndex) {194 return (char) ((0xff00 & (bytes[startIndex] << 8)) | (0xff & bytes[startIndex + 1]));195 }196

197 /**

198 * 将字节数组前4字节转换为整型数值199 *200 *@parambytes201 *@return

202 */

203 public static int getInt(byte[] bytes) {204 return (0xff000000 & (bytes[0] << 24) | (0xff0000 & (bytes[1] << 16))205 | (0xff00 & (bytes[2] << 8)) | (0xff & bytes[3]));206 }207

208 /**

209 * 将字节数组从startIndex开始的4个字节转换为整型数值210 *211 *@parambytes212 *@paramstartIndex213 *@return

214 */

215 public static int getInt(byte[] bytes, intstartIndex) {216 return (0xff000000 & (bytes[startIndex] << 24)217 | (0xff0000 & (bytes[startIndex + 1] << 16))218 | (0xff00 & (bytes[startIndex + 2] << 8)) | (0xff & bytes[startIndex + 3]));219 }220

221 /**

222 * 将字节数组前8字节转换为long整型数值223 *224 *@parambytes225 *@return

226 */

227 public static long getLong(byte[] bytes) {228 return (0xff00000000000000L & ((long) bytes[0] << 56)229 | (0xff000000000000L & ((long) bytes[1] << 48))230 | (0xff0000000000L & ((long) bytes[2] << 40))231 | (0xff00000000L & ((long) bytes[3] << 32))232 | (0xff000000L & ((long) bytes[4] << 24))233 | (0xff0000L & ((long) bytes[5] << 16))234 | (0xff00L & ((long) bytes[6] << 8)) | (0xffL & (long) bytes[7]));235 }236

237 /**

238 * 将字节数组从startIndex开始的8个字节转换为long整型数值239 *240 *@parambytes241 *@paramstartIndex242 *@return

243 */

244 public static long getLong(byte[] bytes, intstartIndex) {245 return (0xff00000000000000L & ((long) bytes[startIndex] << 56)246 | (0xff000000000000L & ((long) bytes[startIndex + 1] << 48))247 | (0xff0000000000L & ((long) bytes[startIndex + 2] << 40))248 | (0xff00000000L & ((long) bytes[startIndex + 3] << 32))249 | (0xff000000L & ((long) bytes[startIndex + 4] << 24))250 | (0xff0000L & ((long) bytes[startIndex + 5] << 16))251 | (0xff00L & ((long) bytes[startIndex + 6] << 8)) | (0xffL & (long) bytes[startIndex + 7]));252 }253

254 /**

255 * 将字节数组前4字节转换为float型数值256 *257 *@parambytes258 *@return

259 */

260 public static float getFloat(byte[] bytes) {261 returnFloat.intBitsToFloat(getInt(bytes));262 }263

264 /**

265 * 将字节数组从startIndex开始的4个字节转换为float型数值266 *267 *@parambytes268 *@paramstartIndex269 *@return

270 */

271 public static float getFloat(byte[] bytes, intstartIndex) {272 byte[] result = new byte[4];273 System.arraycopy(bytes, startIndex, result, 0, 4);274 returnFloat.intBitsToFloat(getInt(result));275 }276

277 /**

278 * 将字节数组前8字节转换为double型数值279 *280 *@parambytes281 *@return

282 */

283 public static double getDouble(byte[] bytes) {284 long l =getLong(bytes);285 returnDouble.longBitsToDouble(l);286 }287

288 /**

289 * 将字节数组从startIndex开始的8个字节转换为double型数值290 *291 *@parambytes292 *@paramstartIndex293 *@return

294 */

295 public static double getDouble(byte[] bytes, intstartIndex) {296 byte[] result = new byte[8];297 System.arraycopy(bytes, startIndex, result, 0, 8);298 long l =getLong(result);299 returnDouble.longBitsToDouble(l);300 }301

302 /**

303 * 将charsetName编码格式的字节数组转换为字符串304 *305 *@parambytes306 *@paramcharsetName307 *@return

308 */

309 public static String getString(byte[] bytes, String charsetName) {310 return newString(bytes, Charset.forName(charsetName));311 }312

313 /**

314 * 将GBK编码格式的字节数组转换为字符串315 *316 *@parambytes317 *@return

318 */

319 public static String getString(byte[] bytes) {320 returngetString(bytes, GBK);321 }322

323 /**

324 * 将16进制字符串转换为字节数组325 *326 *@paramhex327 *@return

328 */

329 public static byte[] hexStringToBytes(String hex) {330 if (hex == null || "".equals(hex)) {331 return null;332 }333 int len = hex.length() / 2;334 byte[] result = new byte[len];335 char[] chArr =hex.toCharArray();336 for (int i = 0; i < len; i++) {337 int pos = i * 2;338 result[i] = (byte) (toByte(chArr[pos]) << 4 | toByte(chArr[pos + 1]));339 }340 returnresult;341 }342

343 /**

344 * 将16进制字符串转换为字节数组345 *346 *@paramhex347 *@return

348 */

349 public static byte[] hexToBytes(String hex) {350 if (hex.length() % 2 != 0)351 throw newIllegalArgumentException(352 "input string should be any multiple of 2!");353 hex.toUpperCase();354

355 byte[] byteBuffer = new byte[hex.length() / 2];356

357 byte padding = 0x00;358 boolean paddingTurning = false;359 for (int i = 0; i < hex.length(); i++) {360 if(paddingTurning) {361 char c =hex.charAt(i);362 int index =indexOf(hex, c);363 padding = (byte) ((padding << 4) |index);364 byteBuffer[i / 2] =padding;365 padding = 0x00;366 paddingTurning = false;367 } else{368 char c =hex.charAt(i);369 int index =indexOf(hex, c);370 padding = (byte) (padding |index);371 paddingTurning = true;372 }373

374 }375 returnbyteBuffer;376 }377

378 private static int indexOf(String input, charc) {379 int index =ArrayUtils.indexOf(HEX_VOCABLE, c);380

381 if (index < 0) {382 throw new IllegalArgumentException("err input:" +input);383 }384 returnindex;385

386 }387

388 /**

389 * 将BCD编码的字节数组转换为字符串390 *391 *@parambcds392 *@return

393 */

394 public static String bcdToString(byte[] bcds) {395 if (bcds == null || bcds.length == 0) {396 return null;397 }398 byte[] temp = new byte[2 *bcds.length];399 for (int i = 0; i < bcds.length; i++) {400 temp[i * 2] = (byte) ((bcds[i] >> 4) & 0x0f);401 temp[i * 2 + 1] = (byte) (bcds[i] & 0x0f);402 }403 StringBuffer res = newStringBuffer();404 for (int i = 0; i < temp.length; i++) {405 res.append(ascii[temp[i]]);406 }407 returnres.toString();408 }409

410 /**

411 * 字节转整形412 *@paramvalue413 *@return

414 */

415 public static int bcdToInt(bytevalue){416 return ((value>>4) * 10) + (value&0x0F);417 }418

419 /**

420 * 字节数组转16进制字符串421 *@parambs422 *@return

423 */

424 public static String bytesToHex(byte[] bs) {425 StringBuilder sb = newStringBuilder();426 for (byteb : bs) {427 int high = (b >> 4) & 0x0f;428 int low = b & 0x0f;429 sb.append(HEX_VOCABLE[high]);430 sb.append(HEX_VOCABLE[low]);431 }432 returnsb.toString();433 }434

435 /**

436 * 字节数组取前len个字节转16进制字符串437 *@parambs438 *@paramlen439 *@return

440 */

441 public static String bytesToHex(byte[] bs, intlen) {442 StringBuilder sb = newStringBuilder();443 for (int i=0; i> 4) & 0x0f;446 int low = b & 0x0f;447 sb.append(HEX_VOCABLE[high]);448 sb.append(HEX_VOCABLE[low]);449 }450 returnsb.toString();451 }452 /**

453 * 字节数组偏移offset长度之后的取len个字节转16进制字符串454 *@parambs455 *@paramoffset456 *@paramlen457 *@return

458 */

459 public static String bytesToHex(byte[] bs, int offset, intlen) {460 StringBuilder sb = newStringBuilder();461 for (int i=0; i> 4) & 0x0f;464 int low = b & 0x0f;465 sb.append(HEX_VOCABLE[high]);466 sb.append(HEX_VOCABLE[low]);467 }468 returnsb.toString();469 }470 /**

471 * 字节数组转16进制字符串472 *@parambs473 *@return

474 */

475 public static String byteToHex(byteb) {476 StringBuilder sb = newStringBuilder();477 int high = (b >> 4) & 0x0f;478 int low = b & 0x0f;479 sb.append(HEX_VOCABLE[high]);480 sb.append(HEX_VOCABLE[low]);481 returnsb.toString();482 }483 /**

484 * 将字节数组取反485 *486 *@paramsrc487 *@return

488 */

489 public static String negate(byte[] src) {490 if (src == null || src.length == 0) {491 return null;492 }493 byte[] temp = new byte[2 *src.length];494 for (int i = 0; i < src.length; i++) {495 byte tmp = (byte) (0xFF ^src[i]);496 temp[i * 2] = (byte) ((tmp >> 4) & 0x0f);497 temp[i * 2 + 1] = (byte) (tmp & 0x0f);498 }499 StringBuffer res = newStringBuffer();500 for (int i = 0; i < temp.length; i++) {501 res.append(ascii[temp[i]]);502 }503 returnres.toString();504 }505

506 /**

507 * 比较字节数组是否相同508 *509 *@parama510 *@paramb511 *@return

512 */

513 public static boolean compareBytes(byte[] a, byte[] b) {514 if (a == null || a.length == 0 || b == null || b.length == 0

515 || a.length !=b.length) {516 return false;517 }518 if (a.length ==b.length) {519 for (int i = 0; i < a.length; i++) {520 if (a[i] !=b[i]) {521 return false;522 }523 }524 } else{525 return false;526 }527 return true;528 }529 /**

530 * 只比对指定长度byte531 *@parama532 *@paramb533 *@paramlen534 *@return

535 */

536 public static boolean compareBytes(byte[] a, byte[] b, intlen) {537 if (a == null || a.length == 0 || b == null || b.length == 0

538 || a.length < len || b.length

549 /**

550 * 将字节数组转换为二进制字符串551 *552 *@paramitems553 *@return

554 */

555 public static String bytesToBinaryString(byte[] items) {556 if (items == null || items.length == 0) {557 return null;558 }559 StringBuffer buf = newStringBuffer();560 for (byteitem : items) {561 buf.append(byteToBinaryString(item));562 }563 returnbuf.toString();564 }565

566 /**

567 * 将字节转换为二进制字符串568 *569 *@paramitems570 *@return

571 */

572 public static String byteToBinaryString(byteitem) {573 byte a =item;574 StringBuffer buf = newStringBuffer();575 for (int i = 0; i < 8; i++) {576 buf.insert(0, a % 2);577 a = (byte) (a >> 1);578 }579 returnbuf.toString();580 }581

582 /**

583 * 对数组a,b进行异或运算584 *585 *@parama586 *@paramb587 *@return

588 */

589 public static byte[] xor(byte[] a, byte[] b) {590 if (a == null || a.length == 0 || b == null || b.length == 0

591 || a.length !=b.length) {592 return null;593 }594 byte[] result = new byte[a.length];595 for (int i = 0; i < a.length; i++) {596 result[i] = (byte) (a[i] ^b[i]);597 }598 returnresult;599 }600

601 /**

602 * 对数组a,b进行异或运算 运算长度len603 *@parama604 *@paramb605 *@paramlen606 *@return

607 */

608 public static byte[] xor(byte[] a, byte[] b, intlen) {609 if (a == null || a.length == 0 || b == null || b.length == 0) {610 return null;611 }612 if (a.length < len || b.length

622 * 将short整型数值转换为字节数组623 *624 *@paramnum625 *@return

626 */

627 public static byte[] shortToBytes(intnum) {628 byte[] temp = new byte[2];629 for (int i = 0; i < 2; i++) {630 temp[i] = (byte) ((num >>> (8 - i * 8)) & 0xFF);631 }632 returntemp;633 }634

635 /**

636 * 将字节数组转为整型637 *638 *@paramnum639 *@return

640 */

641 public static int bytesToShort(byte[] arr) {642 int mask = 0xFF;643 int temp = 0;644 int result = 0;645 for (int i = 0; i < 2; i++) {646 result <<= 8;647 temp = arr[i] &mask;648 result |=temp;649 }650 returnresult;651 }652

653 /**

654 * 将整型数值转换为指定长度的字节数组655 *656 *@paramnum657 *@return

658 */

659 public static byte[] intToBytes(intnum) {660 byte[] temp = new byte[4];661 for (int i = 0; i < 4; i++) {662 temp[i] = (byte) ((num >>> (24 - i * 8)) & 0xFF);663 }664 returntemp;665 }666

667 /**

668 * 将整型数值转换为指定长度的字节数组669 *670 *@paramsrc671 *@paramlen672 *@return

673 */

674 public static byte[] intToBytes(int src, intlen) {675 if (len < 1 || len > 4) {676 return null;677 }678 byte[] temp = new byte[len];679 for (int i = 0; i < len; i++) {680 temp[len - 1 - i] = (byte) ((src >>> (8 * i)) & 0xFF);681 }682 returntemp;683 }684

685 /**

686 * 将字节数组转换为整型数值687 *688 *@paramarr689 *@return

690 */

691 public static int bytesToInt(byte[] arr) {692 int mask = 0xFF;693 int temp = 0;694 int result = 0;695 for (int i = 0; i < 4; i++) {696 result <<= 8;697 temp = arr[i] &mask;698 result |=temp;699 }700 returnresult;701 }702

703 /**

704 * 将long整型数值转换为字节数组705 *706 *@paramnum707 *@return

708 */

709 public static byte[] longToBytes(longnum) {710 byte[] temp = new byte[8];711 for (int i = 0; i < 8; i++) {712 temp[i] = (byte) ((num >>> (56 - i * 8)) & 0xFF);713 }714 returntemp;715 }716

717 /**

718 * 将字节数组转换为long整型数值719 *720 *@paramarr721 *@return

722 */

723 public static long bytesToLong(byte[] arr) {724 int mask = 0xFF;725 int temp = 0;726 long result = 0;727 int len = Math.min(8, arr.length);728 for (int i = 0; i < len; i++) {729 result <<= 8;730 temp = arr[i] &mask;731 result |=temp;732 }733 returnresult;734 }735

736 /**

737 * 将16进制字符转换为字节738 *739 *@paramc740 *@return

741 */

742 public static byte toByte(charc) {743 byte b = (byte) "0123456789ABCDEF".indexOf(c);744 returnb;745 }746

747 /**

748 * 功能描述:把两个字节的字节数组转化为整型数据,高位补零,例如:
749 * 有字节数组byte[] data = new byte[]{1,2};转换后int数据的字节分布如下:
750 * 00000000 00000000 00000001 00000010,函数返回258751 *@paramlenData 需要进行转换的字节数组752 *@return字节数组所表示整型值的大小753 */

754 public static int bytesToIntWhereByteLengthEquals2(bytelenData[]) {755 if(lenData.length != 2){756 return -1;757 }758 byte fill[] = new byte[]{0,0};759 byte real[] = new byte[4];760 System.arraycopy(fill, 0, real, 0, 2);761 System.arraycopy(lenData, 0, real, 2, 2);762 int len =byteToInt(real);763 returnlen;764

765 }766

767 /**

768 * 功能描述:将byte数组转化为int类型的数据769 *@parambyteVal 需要转化的字节数组770 *@return字节数组所表示的整型数据771 */

772 public static int byteToInt(byte[] byteVal) {773 int result = 0;774 for(int i = 0;i < byteVal.length;i++) {775 int tmpVal = (byteVal[i]<

791 result = result |tmpVal;792 }793 returnresult;794 }795 public static byte CheckXORSum(byte[] bData){796 byte sum = 0x00;797 for (int i = 0; i < bData.length; i++) {798 sum ^=bData[i];799 }800 returnsum;801 }802 /**

803 * 从offset开始 将后续长度为len的byte字节转为int804 *@paramdata805 *@paramoffset806 *@paramlen807 *@return

808 */

809 public static int bytesToInt(byte[] data, int offset, intlen){810 int mask = 0xFF;811 int temp = 0;812 int result = 0;813 len = Math.min(len, 4);814 for (int i = 0; i < len; i++) {815 result <<= 8;816 temp = data[offset + i] &mask;817 result |=temp;818 }819 returnresult;820 }821

822 /**

823 * byte字节数组中的字符串的长度824 *@paramdata825 *@return

826 */

827 public static int getBytesStringLen(byte[] data)828 {829 int count = 0;830 for (byteb : data) {831 if(b == 0x00)832 break;833 count++;834 }835 returncount;836 }837

838 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值