Java code To convert byte to Hexadecimal

后端 未结 19 2444
我寻月下人不归
我寻月下人不归 2020-11-22 17:26

I have an array of bytes. I want each byte String of that array to be converted to its corresponding hexadecimal values.

Is there any function in Java to convert a b

19条回答
  •  时光取名叫无心
    2020-11-22 17:44

    Creating (and destroying) a bunch of String instances is not a good way if performance is an issue.

    Please ignore those verbose (duplicate) arguments checking statements (ifs). That's for (another) educational purposes.

    Full maven project: http://jinahya.googlecode.com/svn/trunk/com.googlecode.jinahya/hex-codec/

    Encoding...

    /**
     * Encodes a single nibble.
     *
     * @param decoded the nibble to encode.
     *
     * @return the encoded half octet.
     */
    protected static int encodeHalf(final int decoded) {
    
        switch (decoded) {
            case 0x00:
            case 0x01:
            case 0x02:
            case 0x03:
            case 0x04:
            case 0x05:
            case 0x06:
            case 0x07:
            case 0x08:
            case 0x09:
                return decoded + 0x30; // 0x30('0') - 0x39('9')
            case 0x0A:
            case 0x0B:
            case 0x0C:
            case 0x0D:
            case 0x0E:
            case 0x0F:
                return decoded + 0x57; // 0x41('a') - 0x46('f')
            default:
                throw new IllegalArgumentException("illegal half: " + decoded);
        }
    }
    
    
    /**
     * Encodes a single octet into two nibbles.
     *
     * @param decoded the octet to encode.
     * @param encoded the array to which each encoded nibbles are written.
     * @param offset the offset in the array.
     */
    protected static void encodeSingle(final int decoded, final byte[] encoded,
                                       final int offset) {
    
        if (encoded == null) {
            throw new IllegalArgumentException("null encoded");
        }
    
        if (encoded.length < 2) {
            // not required
            throw new IllegalArgumentException(
                "encoded.length(" + encoded.length + ") < 2");
        }
    
        if (offset < 0) {
            throw new IllegalArgumentException("offset(" + offset + ") < 0");
        }
    
        if (offset >= encoded.length - 1) {
            throw new IllegalArgumentException(
                "offset(" + offset + ") >= encoded.length(" + encoded.length
                + ") - 1");
        }
    
        encoded[offset] = (byte) encodeHalf((decoded >> 4) & 0x0F);
        encoded[offset + 1] = (byte) encodeHalf(decoded & 0x0F);
    }
    
    
    /**
     * Decodes given sequence of octets into a sequence of nibbles.
     *
     * @param decoded the octets to encode
     *
     * @return the encoded nibbles.
     */
    protected static byte[] encodeMultiple(final byte[] decoded) {
    
        if (decoded == null) {
            throw new IllegalArgumentException("null decoded");
        }
    
        final byte[] encoded = new byte[decoded.length << 1];
    
        int offset = 0;
        for (int i = 0; i < decoded.length; i++) {
            encodeSingle(decoded[i], encoded, offset);
            offset += 2;
        }
    
        return encoded;
    }
    
    
    /**
     * Encodes given sequence of octets into a sequence of nibbles.
     *
     * @param decoded the octets to encode.
     *
     * @return the encoded nibbles.
     */
    public byte[] encode(final byte[] decoded) {
    
        return encodeMultiple(decoded);
    }
    

    Decoding...

    /**
     * Decodes a single nibble.
     *
     * @param encoded the nibble to decode.
     *
     * @return the decoded half octet.
     */
    protected static int decodeHalf(final int encoded) {
    
        switch (encoded) {
            case 0x30: // '0'
            case 0x31: // '1'
            case 0x32: // '2'
            case 0x33: // '3'
            case 0x34: // '4'
            case 0x35: // '5'
            case 0x36: // '6'
            case 0x37: // '7'
            case 0x38: // '8'
            case 0x39: // '9'
                return encoded - 0x30;
            case 0x41: // 'A'
            case 0x42: // 'B'
            case 0x43: // 'C'
            case 0x44: // 'D'
            case 0x45: // 'E'
            case 0x46: // 'F'
                return encoded - 0x37;
            case 0x61: // 'a'
            case 0x62: // 'b'
            case 0x63: // 'c'
            case 0x64: // 'd'
            case 0x65: // 'e'
            case 0x66: // 'f'
                return encoded - 0x57;
            default:
                throw new IllegalArgumentException("illegal half: " + encoded);
        }
    }
    
    
    /**
     * Decodes two nibbles into a single octet.
     *
     * @param encoded the nibble array.
     * @param offset the offset in the array.
     *
     * @return decoded octet.
     */
    protected static int decodeSingle(final byte[] encoded, final int offset) {
    
        if (encoded == null) {
            throw new IllegalArgumentException("null encoded");
        }
    
        if (encoded.length < 2) {
            // not required
            throw new IllegalArgumentException(
                "encoded.length(" + encoded.length + ") < 2");
        }
    
        if (offset < 0) {
            throw new IllegalArgumentException("offset(" + offset + ") < 0");
        }
    
        if (offset >= encoded.length - 1) {
            throw new IllegalArgumentException(
                "offset(" + offset + ") >= encoded.length(" + encoded.length
                + ") - 1");
        }
    
        return (decodeHalf(encoded[offset]) << 4)
               | decodeHalf(encoded[offset + 1]);
    }
    
    
    /**
     * Encodes given sequence of nibbles into a sequence of octets.
     *
     * @param encoded the nibbles to decode.
     *
     * @return the encoded octets.
     */
    protected static byte[] decodeMultiple(final byte[] encoded) {
    
        if (encoded == null) {
            throw new IllegalArgumentException("null encoded");
        }
    
        if ((encoded.length & 0x01) == 0x01) {
            throw new IllegalArgumentException(
                "encoded.length(" + encoded.length + ") is not even");
        }
    
        final byte[] decoded = new byte[encoded.length >> 1];
    
        int offset = 0;
        for (int i = 0; i < decoded.length; i++) {
            decoded[i] = (byte) decodeSingle(encoded, offset);
            offset += 2;
        }
    
        return decoded;
    }
    
    
    /**
     * Decodes given sequence of nibbles into a sequence of octets.
     *
     * @param encoded the nibbles to decode.
     *
     * @return the decoded octets.
     */
    public byte[] decode(final byte[] encoded) {
    
        return decodeMultiple(encoded);
    }
    

提交回复
热议问题