`
eyesmore
  • 浏览: 363913 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
阅读更多

 

好些年前写的,偶然找出来,临时用下,顺便分享下~~  性能好像不咋地 

 

import java.io.ByteArrayOutputStream;

public class UltraEdit {
	public static void main(String[] args) {
		System.out.println(UltraEdit.getInstance().getHexUpperCase(" 测试 & 数据 Test Data".getBytes()));
		System.out.println(UltraEdit.getInstance().b2HEX(" 测试 & 数据 Test Data".getBytes(),":"));
		String hex = UltraEdit.getInstance().b2HEX(" 测试 & 数据 Test Data".getBytes());
		System.out.println(hex);
		System.out.println(new String(UltraEdit.getInstance().hex2b(hex)));
		
		UltraEdit.getInstance().hex2b("F");
	}
	
	
	private final static UltraEdit instance = new UltraEdit(); 
	
	private UltraEdit() {
		Configuration.byte2HexUpper = new Byte2HexUpper();
		Configuration.byte2HexLower = new Byte2HexLower();
		Configuration.byte2ASCII = new Byte2ASCII();
	}
	
	public synchronized static UltraEdit getInstance() {
		return instance;
	}
	
	public String getHexUpperCase(byte[] data) {
		return byte2HexFormat(data, 16, Configuration.byte2HexUpper,Configuration.byte2ASCII);
	}	
	
	public String getHexLowerCase(byte[] data) {
		return byte2HexFormat(data, 16, Configuration.byte2HexLower,Configuration.byte2ASCII);
	}
	
	public String getHexUpperCase(byte[] data,int width) {
		return byte2HexFormat(data, width, Configuration.byte2HexUpper,Configuration.byte2ASCII);
	}
	
	public String getHexLowerCase(byte[] data,int width) {
		return byte2HexFormat(data, width, Configuration.byte2HexLower,Configuration.byte2ASCII);
	}
	
	/** 将byte[]数据以十六进制的形式显示,显示格式仿照UltraEdit*/
	private String byte2HexFormat(byte[] data,int width,
			IByte2String hexPartitionMethod,IByte2String strPartitionMethod) 
	{
		StringBuffer sb = new StringBuffer();
		//表头
		for(int i=0;i<width;i++) {
			sb.append("-");
			if(i<10) sb.append(i);
			else sb.append((char)(i-10+'a'));
			sb.append("-");
		}
		sb.append("\r\n");
		int index = 0;
		StringBuffer hexPartionLine = new StringBuffer();
		StringBuffer strPartionLine = new StringBuffer();
		while(index < data.length) {
			int lineStart = index; 
			while(index < data.length && index < lineStart+width) {
				hexPartionLine.append(hexPartitionMethod.byte2String(data[index]))
									.append(Configuration.DELIMITER);
				strPartionLine.append(strPartitionMethod.byte2String(data[index]));
				index ++;
			}
			int placeholderNum = 0;
			while(index+placeholderNum < lineStart+width) {//对最后一行不足width的在HexPartition部分补充占位符
				hexPartionLine.append("   ");
				placeholderNum += 1;
			}
			sb.append(hexPartionLine).append(Configuration.PARTITION)
								.append(strPartionLine).append("\r\n");//完成一行显示
			hexPartionLine.delete(0, hexPartionLine.length());
			strPartionLine.delete(0, strPartionLine.length());
		}
		return sb.toString();
	}
	
	public String b2HEX(byte[] data) {
		return byte2Hex(data, "", Configuration.byte2HexUpper);
	}
	
	public String b2HEX(byte[] data,String delimiter) {
		return byte2Hex(data, delimiter, Configuration.byte2HexUpper);
	}
	
	public String b2hex(byte[] data) {
		return byte2Hex(data, "", Configuration.byte2HexLower);
	}
	
	public String b2hex(byte[] data,String delimiter) {
		return byte2Hex(data, "", Configuration.byte2HexLower);
	}
	
	private String byte2Hex(byte[] data,String delimiter,IByte2String form) {
		StringBuffer sb = new StringBuffer();
		int index = 0;
		while(index < data.length) {
			sb.append(form.byte2String(data[index])).append(delimiter);
			index ++;
		}
		return sb.toString();
	}

	public byte[] hex2b(String hexStr) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		if(hexStr.length() % 2 != 0) {
			throw new IllegalArgumentException("输入参数"+hexStr+"长度不是偶数.");
		}
		int c = 0;
		while(c < hexStr.length()) {
			int high = Configuration.hexChar2Int(hexStr.charAt(c));
			int low = Configuration.hexChar2Int(hexStr.charAt(c+1));
			if(high == -1) {
				throw new IllegalArgumentException("输入参数"+hexStr+".charAt("+c+")="+hexStr.charAt(c)+"不是合法的HexChar");
			}
			if(low == -1) {
				throw new IllegalArgumentException("输入参数"+hexStr+".charAt("+(c+1)+")="+hexStr.charAt(c+1)+"不是合法的HexChar");
			}
			baos.write(Configuration.BCDPackedFormat(high,low));
			c += 2;
		}
		return baos.toByteArray();
	}
		
	private static class Configuration {
		static final char[] HEX_UPPER_CASE = new char[]{
			'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
		};
	
		static final char[] HEX_LOWER_CASE = new char[]{
			'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
		};
		
		/**
		 * @return -1 表示输入参数hexChar不是个合法的HexChar. 其他情况返回的数据控制在[0,15]
		 * */
		static int hexChar2Int(char hexChar) {
			if(Character.isDigit(hexChar)) return hexChar - '0';
			else if(Character.isLowerCase(hexChar)) return hexChar - 'a' + 10;
			else if(Character.isUpperCase(hexChar)) return hexChar - 'A' + 10;
			else return -1;
		}
		
		static byte BCDPackedFormat(int high,int lower) {
			int min = Math.min(high, lower);
			int max = Math.max(high, lower);
			if(min<0 || max >15) {
				throw new IllegalArgumentException("输入参数"+high+","+lower+"都必须控制在范围[0,15]内");
			}
			return (byte)(high << 4 | lower);
		}
		
		/** 各个Byte之间的分割符号*/
		static final String DELIMITER = " ";
		
		/** Hex显示和字符显示区域分割符*/
		static final String PARTITION = ";";
		
		static IByte2String byte2HexUpper;
		
		static IByte2String byte2HexLower;
		
		static IByte2String byte2ASCII;
		
		
	} 
	
	interface IByte2String {
		public String byte2String(byte b); 
	}
	
	class Byte2HexUpper implements IByte2String {
		public String byte2String(byte b) {
			int high = 0x0F & b>>4;
			int low  = 0x0F & b;
			return new String(new char[] {Configuration.HEX_UPPER_CASE[high],
								Configuration.HEX_UPPER_CASE[low]});
		}
	}
	
	class Byte2HexLower implements IByte2String {
		public String byte2String(byte b) {
			int high = 0x0F & b>>4;
			int low  = 0x0F & b;
			return new String(new char[] {Configuration.HEX_LOWER_CASE[high],
							Configuration.HEX_LOWER_CASE[low]});
		}
	}
	
	class Byte2ASCII implements IByte2String {
		private final String INVISIBLE_SUBSTITUTION = ".";
		
		public String byte2String(byte b) {
			return (Character.isWhitespace(b) || Character.isLetterOrDigit(b)) ? new String(new byte[]{b}) : INVISIBLE_SUBSTITUTION;
		}
	}
}
 

 

分享到:
评论

相关推荐

    010.Editor.x64.v6.0.1.Cracked

    Hex editors are used to edit the individual bytes of binary files,and advanced hex editors such as 010 Editor can also edit hard drives, floppy drives, memory keys, flash drives, CD-ROMs, processes, ...

    010.Editor.x32.v6.0.1.Cracked

    Hex editors are used to edit the individual bytes of binary files,and advanced hex editors such as 010 Editor can also edit hard drives, floppy drives, memory keys, flash drives, CD-ROMs, processes, ...

    hex2byte byte2hex

    hex2byte byte2hex,转换成字符串传输

    HxD HeX Editor 英文绿色版

    16 Bit Intel Hex, 20 Bit Intel Hex, 32 Bit Intel Hex - Checksum-Generator: Checksum-8, ..., Checksum-32, CRC-16, CRC-16 CCITT, CRC-32, Custom CRC, SHA-1, SHA-256, SHA-384, SHA-512, MD-2, MD-4, MD5 ...

    VB编程资源大全(英文源码 文件)

    &lt;END&gt;&lt;br&gt;59,BINVIEW.zip BINVIEW is a 400 byte scrolling binary viewer that can display any part of any file as decimal bytes, hex bytes, signed, unsigned and long integers or as floating point ...

    Python中String, Bytes, Hex, Base64之间的关系与转换方法详解工程文件

    Program : Type Hint, String, Bytes, Hex, Base64 详解博客地址:https://blog.csdn.net/m0_52316372/article/details/125689591

    hex2bin_2.2_XiaZaiBa.zip

    Specifying this starting address will put pad bytes in the binary file so that the data supposed to be stored at 0100 will start at the same address in the binary file. -v Verbose messages for ...

    16 进制编辑工具 Hex Editor Neo Ultimate Edition 6.52.00 + x64.zip

    剪贴板操作,Bytes, Words, Double Words, Quad Words等双打编辑模式。 十六进制编辑器的使用范围:二进制文件,补丁,DLLs,AVI文件,MP3文件,JPG文件 16 进制编辑工具 Hex Editor Neo Ultimate Edition 中文...

    Hex Editor Neo Ultiamate 6.x Patch

    剪贴板操作,Bytes, Words, Double Words, Quad Words等双打编辑模式。 十六进制编辑器的使用范围:二进制文件,补丁,DLLs,AVI文件,MP3文件,JPG文件。 Free Hex Editor Neo主要特点: 支持使用正则表达式在替换...

    对Python3中bytes和HexStr之间的转换详解

    今天小编就为大家分享一篇对Python3中bytes和HexStr之间的转换详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    Python3编码问题 Unicode utf-8 bytes互转方法

    为什么需要本文,因为在对接某些很老的接口的时候,需要传递过去的是16进制的hex字符串,并且要求对传的字符串做编码,这里就介绍了utf-8 Unicode bytes 等等。 #英文使用utf-8 转换成16进制hex字符串的方法 newstr...

    nodejs 十六进制字符串型数据与btye型数据相互转换

    const Bytes2HexString = (b)=&gt; { let hexs = ; for (let i = 0; i &lt; b.length; i++) { let hex = (b[i]).toString(16); if (hex.length === 1) { hexs = '0' + hex; } hexs += hex.toUpperCase();

    JVIntelHex:一个 javascript Intel HEX 格式编写器(一个非常轻量级但功能强大的实现!)

    合资英特尔HEX 一个 javascript Intel HEX 格式编写器(轻量级实现) 1.0 版由创建 ...// How many bytes per HEX record. Usually 16 or 32 but can take arbitrary numbers. byteCount = 16 ; // Y

    cc2420+atmega128串口通信实验2

    学习使用AVR单片机中断方式实验串口通信 ...│test6.hex 750 bytes │test6.lss 5.06 KB │test6.lst 2.96 KB │test6.map 14.37 KB │test6.o 2.54 KB │test6.pnproj 60 bytes │test6.sym 1.62 KB

    cc2420+atmega128串口通信实验1

    │test5.hex 713 bytes │test5.lss 5.13 KB │test5.lst 3.22 KB │test5.map 14.06 KB │test5.o 2.65 KB │test5.pnproj 60 bytes │test5.sym 1.59 KB │UCCmds32.cmf 2.36 KB

    Free Hex Control

    / Hi every body, thanks for choosing Free Hex Control! / / Errrrr...Actually, I hesitate to release the source code of this control, / Because when I checked after completion, I found that it's ...

    二进制转换图片.rar

    二进制转换图片 OutputStream o = response.getOutputStream(); // 将图片转换成字符串 File f = new File("f:\\Vista.png");... String imgStr = byte2hex( bytes ); System.out.println( imgStr); ...

    cc2420*atmega128 看门狗实验

    学习使用AVR单片机的看门狗操作 ...│test4.hex 811 bytes │test4.lss 5.65 KB │test4.lst 3.92 KB │test4.map 13.98 KB │test4.o 2.93 KB │test4.pnproj 60 bytes │test4.sym 1.58 KB

    对python以16进制打印字节数组的方法详解

    l = [hex(int(i)) for i in bytes] print( .join(l)) 以上这篇对python以16进制打印字节数组的方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。

Global site tag (gtag.js) - Google Analytics