From 1da9a129a6f22b61c0bb61cef8f30b624d62c87c Mon Sep 17 00:00:00 2001 From: endian11 <Dana_Lee1016@126.com> Date: 星期一, 16 十二月 2019 11:58:44 +0800 Subject: [PATCH] 提交 十进制字符串转BCB码;相关util方法; 打开TCPCHannelInitializerHandler的loginauthrsp heartbeatrsp --- im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java | 144 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 144 insertions(+), 0 deletions(-) diff --git a/im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java b/im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java index b6b7989..020bf33 100644 --- a/im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java +++ b/im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java @@ -1,5 +1,7 @@ package com.anyun.im_lib.util; +import android.text.TextUtils; + import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; @@ -12,6 +14,148 @@ * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class ByteUtil { + + + /** + * @鍔熻兘: BCD鐮佽浆涓�10杩涘埗涓�(闃挎媺浼暟鎹�) + * @鍙傛暟: BCD鐮� + * @缁撴灉: 10杩涘埗涓� + */ + public static String bcd2Str(byte[] bytes) { + StringBuffer temp = new StringBuffer(bytes.length * 2); + for (int i = 0; i < bytes.length; i++) { + temp.append((byte) ((bytes[i] & 0xf0) >>> 4)); + temp.append((byte) (bytes[i] & 0x0f)); + } + return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp + .toString().substring(1) : temp.toString(); + } + + /** + * @鍔熻兘: 10杩涘埗涓茶浆涓築CD鐮� + * @鍙傛暟: 10杩涘埗涓� + * @缁撴灉: BCD鐮� + */ + public static byte[] str2Bcd(String asc) { + int len = asc.length(); + int mod = len % 2; + if (mod != 0) { + asc = "0" + asc; + len = asc.length(); + } + byte abt[] = new byte[len]; + if (len >= 2) { + len = len / 2; + } + byte bbt[] = new byte[len]; + abt = asc.getBytes(); + int j, k; + for (int p = 0; p < asc.length() / 2; p++) { + if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) { + j = abt[2 * p] - '0'; + } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) { + j = abt[2 * p] - 'a' + 0x0a; + } else { + j = abt[2 * p] - 'A' + 0x0a; + } + if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) { + k = abt[2 * p + 1] - '0'; + } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) { + k = abt[2 * p + 1] - 'a' + 0x0a; + } else { + k = abt[2 * p + 1] - 'A' + 0x0a; + } + int a = (j << 4) + k; + byte b = (byte) a; + bbt[p] = b; + } + return bbt; + } + + public static String getString(byte[] bytes, String charsetName) { + return new String(bytes, Charset.forName(charsetName)); + } + + public static String getString(byte[] bytes) { + return getString(bytes, "GBK"); + } + + /** + * bytes杞崲鎴愬崄鍏繘鍒跺瓧绗︿覆 + * @param byte[] b byte鏁扮粍 + * @return String 姣忎釜Byte鍊间箣闂寸┖鏍煎垎闅� + */ + public static String byte2HexStr(byte[] b) + { + String stmp=""; + StringBuilder sb = new StringBuilder(""); + for (int n=0;n<b.length;n++) + { + stmp = Integer.toHexString(b[n] & 0xFF); + sb.append((stmp.length()==1)? "0"+stmp : stmp); +// sb.append(" "); + } + return sb.toString().toUpperCase().trim(); + } + + /** + * bytes瀛楃涓茶浆鎹负Byte鍊� + * @param String src Byte瀛楃涓诧紝姣忎釜Byte涔嬮棿娌℃湁鍒嗛殧绗� + * @return byte[] + */ + public static byte[] hexStr2Bytes(String src) + { + int m=0,n=0; + int l=src.length()/2; + System.out.println(l); + byte[] ret = new byte[l]; + for (int i = 0; i < l; i++) + { + m=i*2+1; + n=m+1; + ret[i] = Byte.decode("0x" + src.substring(i*2, m) + src.substring(m,n)); + } + return ret; + } + + /** + * 16杩涘埗鐨勫瓧绗︿覆琛ㄧず杞垚瀛楄妭鏁扮粍 + * + * @param hexString 16杩涘埗鏍煎紡鐨勫瓧绗︿覆 + * @return 杞崲鍚庣殑瀛楄妭鏁扮粍 + **/ + public static byte[] toByteArray(String hexString) { + if (TextUtils.isEmpty(hexString)) + throw new IllegalArgumentException("this hexString must not be empty"); + + hexString = hexString.toLowerCase(); + final byte[] byteArray = new byte[hexString.length() / 2]; + int k = 0; + for (int i = 0; i < byteArray.length; i++) {//鍥犱负鏄�16杩涘埗锛屾渶澶氬彧浼氬崰鐢�4浣嶏紝杞崲鎴愬瓧鑺傞渶瑕佷袱涓�16杩涘埗鐨勫瓧绗︼紝楂樹綅鍦ㄥ厛 + byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff); + byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff); + byteArray[i] = (byte) (high << 4 | low); + k += 2; + } + return byteArray; + } + + + public static int getInt(byte i) { + int low = i&0x0f; + int hight= (i>>4)&0x0f; + return low+hight*16; + } + + public static int getBCDInt(byte i) { + int low = i&0x0f; + int hight= (i>>4)&0x0f; + return low+hight*10; + } + + + + /** * ByteBuffer 杞崲 String * @param buffer -- Gitblit v1.8.0