Java Hex String Create appendHexValue(final StringBuffer sb, final byte data)

Here you can find the source of appendHexValue(final StringBuffer sb, final byte data)

Description

Convert a byte into hex value and add to buffer.

License

Open Source License

Parameter

Parameter Description
sb the StringBuffer.
data the data.

Declaration

static void appendHexValue(final StringBuffer sb, final byte data) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from w w  w .j ava 2s.c om*/
     * Convert a byte into hex value and add to buffer.
     *
     * @param sb the StringBuffer.
     * @param data the data.
     */
    static void appendHexValue(final StringBuffer sb, final byte data) {
        sb.append(nibbleToHex((byte) (data >> 4)));
        sb.append(nibbleToHex(data));
    }

    /**
     * Convert lower 4 bits into a hex character.
     *
     * @param data the data byte
     * @return the hex character
     */
    static char nibbleToHex(final byte data) {
        final int nibble = data & 0xf;
        if (nibble <= 9) {
            return (char) ('0' + nibble);
        } else {
            return (char) ('A' + nibble - 10);
        }
    }
}

Related

  1. appendHexPair(byte b, StringBuffer sb)
  2. appendHexStream(StringBuilder sb, byte[] bytes, String delimiter, boolean prefixEachValue, boolean upperCase)
  3. appendHexString(StringBuffer buffer, byte data)
  4. appendHexString(StringBuilder buffer, byte[] bytes)
  5. appendHexString(StringBuilder builder, int value)
  6. appendHexValue(StringBuffer buffer, byte b)