mxpopstar
2022-05-03 e75ef5f04f61aa5fbd89fd5c413dcee1819b7a91
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com;
 
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Scanner;
 
public class ComFn {
    public static String bytesToHexString(byte[] src, int len){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || len <= 0) {
            return null;
        }
        for (int i = 0; i < len; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v).toUpperCase();
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv + " ");
        }
        return stringBuilder.toString();
    }
    
    public static byte[] hexStr2Byte(String hex) {
        ByteBuffer bf = ByteBuffer.allocate(hex.length() / 3);
        for (int i = 0; i<hex.length(); i++) {
            String hexStr = hex.charAt(i++) + "";
            hexStr += hex.charAt(i++);
            hexStr += hex.charAt(i);
            byte b = (byte) Integer.parseInt(hexStr.trim(), 16);
            bf.put(b);
        }
        return bf.array();
    }
    
    public String GetCpuID() {
        try {
            Process process = Runtime.getRuntime().exec(new String[]{"wmic", "cpu", "get", "ProcessorId"});
            process.getOutputStream().close();
            @SuppressWarnings("resource")
            Scanner sc = new Scanner(process.getInputStream());
            @SuppressWarnings("unused")
            String property = sc.next();
            String serial = sc.next();
            System.out.println(serial);
            return serial;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}