Tag Archive for byte

Convert byte array to Hex String and vice versa

public static String getHexString(byte[] b) throws Exception {
  String result = "";
  for (int i=0; i < b.length; i++) {
    result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
  return result;

public static byte[] getByteArray(String hexString) {
  return BigInteger(hexString,16).toByteArray(); 


Create emtpy file with predefined size

dd if=/dev/zero of=filename bs=1024 count=20


Convert Byte Array to String

byte [] dBytes = { 1, 2, 3, 4, 5}
string str;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);


Convert String to Byte Array

public static byte[] ToByteArray(string str)
System.Text.ASCIIEncoding  encoding = new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);


Convert a BufferedImage to a Byte Array as JPEG

public static byte[] bufferedImageToByteArray(BufferedImage img) throws ImageFormatException, IOException{
ByteArrayOutputStream os = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
return os.toByteArray();


common typedefs for binary data management

typedef unsigned char   Uint8;
typedef signed char     Sint8;
typedef unsigned short  Uint16;
typedef signed short    Sint16;
typedef unsigned int    Uint32;
typedef signed int      Sint32;

typedef long long 	Sint64;
typedef unsigned long long Uint64;

//typedef unsigned char byte;