集册 Java实例教程 通过将每两个十六进制数字编码为一个字符,将int转换为相应的字节数组。

通过将每两个十六进制数字编码为一个字符,将int转换为相应的字节数组。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

483
通过将每两个十六进制数字编码为一个字符,将int转换为相应的字节数组。

/*

 * Licensed to the Apache Software Foundation (ASF) under one or more

 * contributor license agreements.  See the NOTICE file distributed with

 * this work for additional information regarding copyright ownership.

 * The ASF licenses this file to You under the Apache License, Version 2.0

 * (the "License"); you may not use this file except in compliance with

 * the License.  You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

//package com.nowjava;//来自 时 代 J a v a 公 众 号

import java.io.ByteArrayOutputStream;


public class Main {

    public static void main(String[] argv) throws Exception {

        int integer = 2;

        System.out.println(java.util.Arrays.toString(convert(integer)));

    }


    /**

     * Convert an int into the corresponding byte array by encoding each two

     * hexadecimal digits as a char. This will return a byte array to the length

     * specified by bufsize.

     *

     * @param integer

     *            The int representation.

     * @param bufsize

     *            The required byte array size.

     * @return the hexadecimal digits as a byte array

     */

    public static byte[] convert(final int integer, final int bufsize) {

        final StringBuilder buf = new StringBuilder(

                Integer.toHexString(integer));

        // Convert to an even number of digits

        if (buf.length() % 2 != 0) {

            buf.insert(0, "0");

        }/* 来自 时代Java*/

        int size = buf.length() / 2;

        if (size > bufsize) {

            buf.delete(0, buf.length() - bufsize * 2);

        } else {

            while (size < bufsize) {

                buf.insert(0, "00");

                size++;

            }

        }

        return convert(buf.toString());

    }


    /**

     * Convert an int into the corresponding byte array by encoding each two

     * hexadecimal digits as a char.

     *

     * @param integer

     *            The int representation

     * @return the hexadecimal digits as a byte array

     */

    public static byte[] convert(final int integer) {

        return convert(Integer.toHexString(integer));

    }


    /**

     * Convert a String of hexadecimal digits into the corresponding byte array

     * by encoding each two hexadecimal digits as a byte.

     *

     * @param digits

     *            The hexadecimal digits representation.

     * @return the hexadecimal digits as a byte array

     */

    public static byte[] convert(String digits) {


        if (digits.length() % 2 == 0) {

            // Even number of digits, so ignore

        } else {

            // Convert to an even number of digits

            digits = "0" + digits;

        }


        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        for (int i = 0; i < digits.length(); i += 2) {

            final char c1 = digits.charAt(i);

            final char c2 = digits.charAt(i + 1);

            byte b = 0;

            if (c1 >= '0' && c1 <= '9') {

                b += (c1 - '0') * 16;

            } else if (c1 >= 'a' && c1 <= 'f') {

                b += (c1 - 'a' + 10) * 16;

            } else if (c1 >= 'A' && c1 <= 'F') {

                b += (c1 - 'A' + 10) * 16;

            } else {

                throw new IllegalArgumentException(
展开阅读全文