package sample; import java.nio.ByteBuffer; import javax.xml.bind.DatatypeConverter; public class ByteSample { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ // 符号なし整数バイト String hexString = "FFFFFFFF"; byte[] bytes = DatatypeConverter.parseHexBinary(hexString); System.out.print("bytes: "); for(byte b: bytes){ System.out.printf("%x ", b); } System.out.println(); // bytes -> int long num = ByteBuffer.wrap(bytes).getInt(); System.out.println("int: " + num); // シフト演算を用いた符号なし整数バイトの変換(Big-Endian) long longValue = 0L; for (byte b : bytes) { longValue = (longValue << 8) + (b & 0xff); } System.out.println("Big-Endian:" + longValue); // シフト演算を用いた符号なし整数バイトの変換(Little-Endian) longValue = 0L; for (int i = bytes.length - 1; i >= 0; i--) { longValue = (longValue << 8) + (bytes[i] & 0xff); } System.out.println("Little-Endian:" + longValue); } }