Java ByteBuffer to Hex toHexValue(ByteBuffer buffer)

Here you can find the source of toHexValue(ByteBuffer buffer)

Description

to Hex Value

License

Apache License

Declaration

public static String toHexValue(ByteBuffer buffer) 

Method Source Code


//package com.java2s;
/*/*from w  ww .j a  v a 2s  .  c  om*/
 * Copyright 2004-2005 the original author or authors.
 *
 * Licensed 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.
 */

import java.nio.ByteBuffer;

public class Main {
    public static String toHexValue(ByteBuffer buffer) {
        StringBuffer result = new StringBuffer(buffer.remaining() * 2);
        while (buffer.hasRemaining()) {
            String value = Integer.toHexString(buffer.get() & 0xff);
            if (value.length() == 1)
                result.append("0"); //ensure 2 digit
            result.append(value);
        }
        return result.toString();
    }

    public static String toHexValue(long l) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putLong(0, l);
        return toHexValue(buffer);
    }

    public static String toHexValue(int i) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(0, i);
        return toHexValue(buffer);
    }

    public static String toHexValue(short s) {
        ByteBuffer buffer = ByteBuffer.allocate(2);
        buffer.putShort(0, s);
        return toHexValue(buffer);
    }

    public static String toHexValue(byte b) {
        ByteBuffer buffer = ByteBuffer.allocate(1);
        buffer.put(0, b);
        return toHexValue(buffer);
    }
}

Related

  1. toHexString(ByteBuffer buffer)
  2. toHexString(ByteBuffer buffer, int size)
  3. toHexString(ByteBuffer byteBuffer)
  4. toHexString(ByteBuffer byteBuffer)
  5. toHexString(final ByteBuffer buffer)