byte處理的幾種方法


/**
     * 字符串轉16進制byte
     * @param 
     * @return
     * @throws Exception
     * @author hw  
     * @date 2018/10/19 9:47
     */
    private static byte hexStr2Str(String hexStr) {
        String str = "0123456789abcdef";
        char[] hexs = hexStr.toCharArray();
        int n = 0;
        n = str.indexOf(hexs[0]) * 16;
        n += str.indexOf(hexs[1]);
        byte bytes = (byte) (n & 0xff);

        return bytes;
    }

輸入:"80"     

輸出:0x80 , 打印出為  -128

注:

0x80 表示 128,(0x 代表 16 進制,8 * 16¹ + 0 * 16º = 128),128 的二進制是 10000000,即 2 的 7 次方。
byte 共有 8 位,表示范圍是 -128 ~ 127,二進制即 10000000 ~ 01111111,第一位為符號位,1 表示負數,0 表示整數,11111111 即表示 -127,10000000 比較特殊,表示 -128。

所以,0x80 本來是整數的 128,二進制 00000000000000000000000010000000 (Java 中整數4個字節32位)。(byte)0x80,將其轉換為 byte,即截取最后 8 位,即 10000000,就是 byte 中的 -128。

 

 

/**
     * 將16進制字符串轉換為byte[]
     *
     * @param str
     * @return
     */
    public static byte[] toBytes(String str) {
        if(str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for(int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        return bytes;
    }

 public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "80b58be8af95";
        System.out.println("轉換后的字節數組:" + Arrays.toString(toBytes(str)));
    }

輸出:

轉換后的字節數組:[-128, -75, -117, -24, -81, -107]

 

 

/**
     * 將數組以字符串形式存儲
     * @param
     * @return
     * @throws Exception
     * @author hw
     * @date 2018/10/19 9:45
     */
    public static void saveFile2(byte[] bfile, String filename) throws IOException {
        String fileURI ="D:\\" + filename;
        try {
            File file = new File(fileURI);
            if(!file.exists()){
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file);
            BufferedWriter out = new BufferedWriter(fw);
            out.write(toHexString1(bfile));
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String toHexString1(byte[] b){
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < b.length; ++i){
            buffer.append(toHexString1(b[i]));
        }
        String str =  buffer.toString().substring(0,buffer.length()-1);
        return str;
    }

    public static String toHexString1(byte b){
        String s = Integer.toHexString(b & 0xFF);
        if (s.length() == 1){
            return "0" + s +",";
        }else{
            return s+",";
        }
    }
輸入:byte[] bytes1 = { (byte)0xE2, (byte)0xa8, 0x34, (byte)0x80,(byte)0x91,0x22,0x33,0x44,0x55,0x66,0x77};
輸出:E2, a8,34,80,91,22,33,44,55,66,77

 

 

/**
     * 讀取文件
     * @param
     * @return
     * @throws Exception
     * @author hw
     * @date 2018/10/17 19:59
     */
    public static byte[] readFile(String filePath) throws IOException {
        File file = new File(filePath);
        ByteArrayOutputStream out = null;
        FileInputStream in =null;
        try {
            in = new FileInputStream(file);
            out = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int i = 0;
            while ((i = in.read(b)) != -1) {
                String str = new String(b,0,i);
                out.write(str.getBytes(), 0, str.getBytes().length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            out.close();
            in.close();
        }

        byte[] bytes = out.toByteArray();
        return bytes;
    }

 

 

 

/**
     * 讀取文件存為byte[]
     * @param
     * @return
     * @throws Exception
     * @author hw
     * @date 2018/10/19 9:48
     */
    @ResponseBody
    @RequestMapping(value = "/fileToByte", method = RequestMethod.GET)
    public void fileToByte() throws Exception {
        String filePath1 = "D:\\test1.txt";
        byte[] bytes1 = readFile(filePath1);
        String str1= new String (bytes1);
        String[] strs1 = str1.split(",");

        String filePath2 = "D:\\test2.txt";
        byte[] bytes2 = readFile(filePath2);
        String str2 = new String(bytes2);

        String[] strs2 = str2.split(",");
        byte[] bytes3 = new byte[strs2.length];
        for(int i = 0 ; i<strs2.length ; i++){
            bytes3[i] =hexStr2Str(strs2[i]);
        }

//        String[] strs1={"2","5","7","9"};
//        String[] strs2 = {"e2","af","34","80","11","22","33","44","55","66","77"};

        int start = 0;
        int end = 0;
        for(int i = 0; i<strs1.length;i++){
            end = Integer.valueOf(strs1[i]);
            byte[] byte_1 = new byte[end - start];

            System.arraycopy(bytes3, start, byte_1, 0, end - start);
            start = end;

            bytes.offer(byte_1);
            System.out.println("處理文件當前隊列大小" + bytes.size());
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM