Java ByteBuffer from String toByteBuffer(String hexStr)

Here you can find the source of toByteBuffer(String hexStr)

Description

to Byte Buffer

License

Open Source License

Declaration

public static ByteBuffer toByteBuffer(String hexStr) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;

public class Main {
    public static ByteBuffer toByteBuffer(String hexStr) {
        if (hexStr == null || hexStr.length() == 0) {
            return null;
        }//from   ww w  .j a  v  a 2 s .  c o  m
        if (hexStr.length() % 2 != 0) {
            throw new IllegalArgumentException("Invalid hex value.");
        }
        byte[] ba = new byte[hexStr.length() / 2];
        for (int i = 0; i < ba.length; i++) {
            ba[i] = (byte) Integer.parseInt(hexStr.substring(2 * i, 2 * i + 2), 16);
        }
        ByteBuffer buffer = ByteBuffer.allocate(ba.length);
        buffer.clear();
        buffer.put(ba);
        buffer.flip();
        return buffer;
    }
}

Related

  1. asByteBuffer(String s)
  2. stringToByteBuffer(String data)
  3. stringToByteBuffer(String string)
  4. stringToNullTerminatedByteBuffer(String s)
  5. toByteBuffer(final String s)
  6. toByteBuffer(String s)
  7. toByteBuffer(String value)
  8. toByteBuffer(String value)
  9. toByteBuffer(String value, String charsetName)