append Hex to StringBuffer - Java java.lang

Java examples for java.lang:String Hex

Description

append Hex to StringBuffer

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        StringBuffer buf = new StringBuffer();
        byte i = 42;
        appendHex(buf, i);/*from  w w  w .j  a v  a  2  s .c o  m*/
    }

    public static void appendHex(final StringBuffer buf, final byte i) {
        buf.append(Character.forDigit((i & 0xf0) >> 4, 16));
        buf.append(Character.forDigit((i & 0x0f), 16));
    }

    public static void appendHex(final StringBuffer buf, final int i) {
        buf.append(Integer.toHexString((i >> 24) & 0xff));
        buf.append(Integer.toHexString((i >> 16) & 0xff));
        buf.append(Integer.toHexString((i >> 8) & 0xff));
        buf.append(Integer.toHexString(i & 0xff));
    }
}

Related Tutorials