Java Hex String Create appendHexValue(StringBuffer buffer, byte b)

Here you can find the source of appendHexValue(StringBuffer buffer, byte b)

Description

Helper method that converts a single byte to a hex string representation.

License

Open Source License

Parameter

Parameter Description
b byte Byte to convert

Return

StringBuffer with the two-digit hex string

Declaration

public static void appendHexValue(StringBuffer buffer, byte b) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   ww w  .  j  a v  a2  s  .  c  o  m
    Helper method that converts a single byte to a hex string representation.
        
    @param b byte Byte to convert
    @return StringBuffer with the two-digit hex string
    */
    public static void appendHexValue(StringBuffer buffer, byte b) {
        int[] digits = { (b >>> 4) & 0x0F, b & 0x0F };
        for (int d = 0; d < digits.length; ++d) {
            int increment = (int) ((digits[d] < 10) ? '0' : ('a' - 10));
            buffer.append((char) (digits[d] + increment));
        }
    }

    /**
    Helper that appends a hex representation of a byte array to an
    existing StringBuffer.
    */
    public static void appendHexValue(StringBuffer buffer, byte[] bytes) {
        for (int i = 0; i < bytes.length; ++i)
            appendHexValue(buffer, bytes[i]);
    }
}

Related

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