一些算法的整理

BA21转换成21BA的算法

String str1 = "BA21";                // 十六进制字符串     
String str2 = "";                                // 反转后的字符串
for (int i = str1.length() - 2; i >= 0; i = i - 2)
<span style="white-space:pre">    </span>{
           str2 += str1.substring(i,i+2);
    }
System.out.println(str2);

Java合并两个数组

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
   public static void main(String args[]) {
      String a[] = { "A", "E", "I" };
      String b[] = { "O", "U" };
      List list = new ArrayList(Arrays.asList(a));
      list.addAll(Arrays.asList(b));
      Object[] c = list.toArray();
      System.out.println(Arrays.toString(c));
   }
}

 

char[]和byte[]的互转

// char转byte
private byte[] getBytes (char[] chars) {
   Charset cs = Charset.forName ("UTF-8");
   CharBuffer cb = CharBuffer.allocate (chars.length);
   cb.put (chars);
                 cb.flip ();
   ByteBuffer bb = cs.encode (cb);
  
   return bb.array();

 }

// byte转char

private char[] getChars (byte[] bytes) {
      Charset cs = Charset.forName ("UTF-8");
      ByteBuffer bb = ByteBuffer.allocate (bytes.length);
      bb.put (bytes);
                 bb.flip ();
       CharBuffer cb = cs.decode (bb);
  
   return cb.array();

}

其他的一些,注释部分有详细的说明:

public static double[] getYData(String string) {
        string = string.trim().replace(" ", "");
        double[] data = new double[string.length() / 4];// 这里最好判断下字符串数据的长度
        for (int i = 0; i < data.length; i++) {
            data[i] = parseHex(string.substring(4 * i, 4 * i + 4));
        }
        return data;
    }

    /**
     * 将接收到的字节数组转化成16进制的字符串
     * 
     * @param b
     * @return
     * @throws Exception
     */
    public static String getHexString(byte[] b) throws Exception {
        String result = "";
        for (int i = 0; i < b.length; i++) {
            result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
        }
        return result;
    }

    /**
     * 将字符串转化成byte[],注意byte[]数组包含的是十六进制,但是打印的是显示十进制
     * 
     * @param hexString
     * @return
     */
    public static byte[] getByteArray(String hexString) {
        return new BigInteger(hexString, 16).toByteArray();
    }

    /**
     * 将十六进制的字符转变成十进制整型数据
     * 
     * @param str
     * @return
     */
    public static int parseHex(String str) {
        int result = 0;
        try {
            int temp = Integer.parseInt(str.trim(), 16);
            result = temp;
        } catch (NumberFormatException e) {
            long ltemp = Long.parseLong(str.trim(), 16);
            result = (int) ltemp;
        }
        return result;
    }
    /**
     * 将十进制转换为指定长度的十六进制字符串
     * @param algorism 十进制数字
     * @param maxLength 转化长度
     * @return algorismToHexString((s-5)/5,2)
     */
    public static String algorismToHexString(int algorism,int maxLength) {
        String result = "";
        result = Integer.toHexString(algorism);
        if (result.length() % 2 == 1) {
            result = "0" + result;
        }
        
        return patchHexString(result, maxLength);
    }
    /**
     * 
     * @param str
     * @param maxLength
     * @return
     */
    public static String patchHexString(String str,int maxLength) {
        String temp="";
        for (int i = 0; i < maxLength-str.length(); i++) {
            temp="0"+temp;
        }
        str=(temp+str).substring(0, maxLength);
        return str;
    }
    /**
     * 合并两个byte数组
     * @param byte_1
     * @param byte_2
     * @return
     */
    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2){  
        byte[] byte_3 = new byte[byte_1.length+byte_2.length];  
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);  
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);  
        return byte_3;  
    } 
    /**
     * 
     * @param time积分时间
     * @param counts扫描次数
     * @return
     */
    public static byte[] getCommand(int time,int counts) {
        String result = algorismToHexString((time-5)/5, 2)+algorismToHexString((counts*2), 2);//一开始搞混了
        byte[] data = getByteArray(result);
        byte[] dataHex = byteMerger(data, data);
        for (int i = 0; i < dataHex.length; i++) {
            byte b = (byte)dataHex[i];
            dataHex[i] = (byte)Integer.parseInt(Integer.toString( ( b & 0xff ) + 0x100, 16).substring( 1 ), 16);
        }
        return dataHex;//byte[]中数据以16进制显示
    }

 Java byte数据类型转换:

public class DataTypeChangeHelper {  
    /** 
     * 将一个单字节的byte转换成32位的int 
     *  
     * @param b 
     *            byte 
     * @return convert result 
     */  
    public static int unsignedByteToInt(byte b) {  
        return (int) b & 0xFF;  
    }  
  
    /** 
     * 将一个单字节的Byte转换成十六进制的数 
     *  
     * @param b 
     *            byte 
     * @return convert result 
     */  
    public static String byteToHex(byte b) {  
        int i = b & 0xFF;  
        return Integer.toHexString(i);  
    }  
  
    /** 
     * 将一个4byte的数组转换成32位的无符号int类型 
     *  
     * @param buf 
     *            bytes buffer 
     * @param pos byte[]中开始转换的位置
     * @return convert result 
     */  
    public static long unsigned4BytesToInt(byte[] buf, int pos) {  
        int firstByte = 0;  
        int secondByte = 0;  
        int thirdByte = 0;  
        int fourthByte = 0;  
        int index = pos;//可以直接int index = 0;  
        firstByte = (0x000000FF & ((int) buf[index]));  
        secondByte = (0x000000FF & ((int) buf[index + 1]));  
        thirdByte = (0x000000FF & ((int) buf[index + 2]));  
        fourthByte = (0x000000FF & ((int) buf[index + 3]));  
        index = index + 4;  
        return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;  
    }  
  
    /** 
     * 将16位的short转换成byte数组 
     *  
     * @param s 
     *            short 
     * @return byte[] 长度为2 
     * */  
    public static byte[] shortToByteArray(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;  
    }  
  
    /** 
     * 将32位整数转换成长度为4的byte数组 
     *  
     * @param s 
     *            int 
     * @return byte[] 
     * */  
    public static byte[] intToByteArray(int s) {  
        byte[] targets = new byte[2];  
        for (int i = 0; i < 4; i++) {  
            int offset = (targets.length - 1 - i) * 8;  
            targets[i] = (byte) ((s >>> offset) & 0xff);  
        }  
        return targets;  
    }  
  
    /** 
     * long to byte[] 
     *  
     * @param s 
     *            long 
     * @return byte[] 
     * */  
    public static byte[] longToByteArray(long s) {  
        byte[] targets = new byte[2];  
        for (int i = 0; i < 8; i++) {  
            int offset = (targets.length - 1 - i) * 8;  
            targets[i] = (byte) ((s >>> offset) & 0xff);  
        }  
        return targets;  
    }  
  
    /** 32位int转byte[] */  
    public static byte[] int2byte(int res) {  
        byte[] targets = new byte[4];  
        targets[0] = (byte) (res & 0xff);// 最低位  
        targets[1] = (byte) ((res >> 8) & 0xff);// 次低位  
        targets[2] = (byte) ((res >> 16) & 0xff);// 次高位  
        targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。  
        return targets;  
    }  
  
    /** 
     * 将长度为2的byte数组转换为16位int 
     *  
     * @param res 
     *            byte[] 
     * @return int 
     * */  
    public static int byte2int(byte[] res) {  
        // res = InversionByte(res);  
        // 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000  
        int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或  
        return targets;  
    }  
}  
    /**
     * 把字符串中间多余的空格去掉,只保留一个空格,比如字符串“hello  my sister   Helen”运算之后的结果为“hello my sister Helen”
     * @param string
     * @return
     */
    public static String deleteMidTrim(String string) {
        String result = "";
        Pattern pattern = Pattern.compile("\s+");
        Matcher matcher = pattern.matcher(string.trim());
        result = matcher.replaceAll(" ");
        return result;
    }

  



原文地址:https://www.cnblogs.com/sowhat4999/p/4439863.html