Java Utililty Methods Byte Convert From

List of utility methods to do Byte Convert From

Description

The list of methods to do Byte Convert From are organized into topic(s).

Method

byteasByte(Byte number)
as Byte
if (number != null) {
    return number;
return 0;
ByteasByte(final Object o)
as Byte
if (o instanceof Number) {
    return ((Number) o).byteValue();
return null;
ByteasByte(Object o)
Converts the given object to a Byte
if (o instanceof Number) {
    return ((Number) o).byteValue();
return null;
byte[]booleanAsByte(boolean val)
boolean As Byte
return new byte[] { val ? (byte) 1 : (byte) 0 };
byte[]bytesFrom(String hwAddress)
bytes From
String[] mac = hwAddress.split(":");
byte[] macAddress = new byte[6]; 
for (int i = 0; i < mac.length; i++) {
    macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();
return macAddress;
byte[]bytesFromDpid(long dpid)
bytes From Dpid
byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
for (short i = 0; i < 6; i++) {
    mac[5 - i] = (byte) dpid;
    dpid >>= 8;
return mac;
byte[]bytesFromHex(String s)
bytes From Hex
String clean = s.replaceAll("[^0-9a-f]", "");
if (clean.length() % 2 != 0)
    throw new RuntimeException("Not a hex string: " + s);
byte data[] = new byte[clean.length() / 2];
for (int i = 0; i < clean.length(); i += 2) {
    data[i / 2] = (Integer.decode("0x" + clean.charAt(i) + clean.charAt(i + 1))).byteValue();
return data;
...
byte[]bytesFromHexString(String values)
bytes From Hex String
String target = "";
if (values != null) {
    target = values;
String[] octets = target.split(":");
byte[] ret = new byte[octets.length];
for (int i = 0; i < octets.length; i++) {
    ret[i] = Integer.valueOf(octets[i], 16).byteValue();
...
byte[]bytesFromLong(long l)
bytes From Long
byte[] ret = new byte[8];
for (int i = 0; i < 8; i++) {
    ret[i] = (byte) l;
    l >>>= 8;
return (ret);
byte[]bytesFromString(String bytes)
bytes From String
String[] bs = bytes.split(" ");
byte[] result = new byte[bs.length];
for (int i = 0; i < bs.length; i++) {
    int b = Integer.parseInt(bs[i], 16);
    result[i] = (byte) b;
return result;