<!-- 标题 -->
<h1 class="w3-center w3-padding-64 w3-tangerine"><span class="w3-tag w3-wide">ByteUtils.java</span></h1>
<!-- 内容 -->

<div style="text-align: right">
    <a title="Table Of Content" href="/archive/crypto/crypto_index.html">TOC</a>
    <a href="javascript:void(0);" onclick="toggle_all_sub_note()">Toggle Commentary</a>
</div>

<div title="ByteUtils.java" class="javacode">
import java.util.Formatter;

public class ByteUtils {
    public static byte[] xor(byte[] bytes1, byte[] bytes2, int num) {
        byte[] result_bytes = new byte[num];
        for (int i = 0; i < num; i++) {
            result_bytes[i] = (byte) (bytes1[i] ^ bytes2[i]);
        }
        return result_bytes;
    }

    public static byte[] concatenate(byte[] bytes1, byte[] bytes2) {
        int len1 = bytes1.length;
        int len2 = bytes2.length;

        byte[] result_bytes = new byte[len1 + len2];

        System.arraycopy(bytes1, 0, result_bytes, 0, len1);
        System.arraycopy(bytes2, 0, result_bytes, len1, len2);

        return result_bytes;
    }

    public static String toHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        Formatter fm = new Formatter(sb);
        for (byte b : bytes) {
            fm.format("%02X", (b & 0xFF));
        }
        return sb.toString();
    }

    public static String toBinary(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            for (int i = 7; i >= 0; i--) {
                int val = (b >> i) & 0x01;
                sb.append(val == 0 ? "0" : "1");
            }
            sb.append(" ");
        }
        return sb.toString();
    }
}
</div>

{'avg_line_length': 29.875, 'max_line_length': 104, 'alphanum_fraction': 0.5534967124925284, 'licenses': ['Apache-2.0'], 'repository_name': 'lsieun/lsieun-web', 'path': 'static/archive/crypto/byte_utils.html', 'size': 1681, 'lang': 'HTML'}