All pastes #1234966 Raw Edit

Hexviewer in Java

public java v1 · immutable
#1234966 ·published 2008-10-23 19:29 UTC
rendered paste body
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class Hexview{		public static void main(String[] args)	{		if(args.length < 1) return;		try		{			FileInputStream fis = new FileInputStream(new File(args[0]));			int n = 0;			int coll = 8;			byte[] b = new byte[coll];			int addr = 0;			int read = 0;			System.out.println("addr\t\t00 01 02 03 04 05 06 07");			System.out.println("----\t\t-----------------------");			while((read = fis.read(b, 0, coll))!=-1) {								System.out.print(convert(addr)+"\t\t");				for (int i = 0; i < read; i++)				{					int c = Integer.parseInt(Byte.toString(b[i]))&0x000000ff;					String hex = c < 16 ? "0"+ Integer.toHexString(c) : Integer.toHexString(c);					System.out.print(hex+" ");				}				System.out.print("\r\n");				addr += coll;			}						fis.close();		} catch (FileNotFoundException e)		{						e.printStackTrace();		} catch (IOException e)		{						e.printStackTrace();		}	}	private static String convert(int addr)	{		return zerosAsString(addr).concat(Integer.toHexString(addr));	}		private static String zerosAsString(int n) {		int len = new String(""+Integer.toHexString(n)).length();		String buf = "";		int maxlen = 6;		for (int i = 0; i < maxlen -len; i++) buf += "-";		return buf;	}}