«  java support encod...   |   Blog首页   |   闲扯原码、反码、补码(转载) »

2006/04/28

Hex Your Bytes to Display(转载)

 Have you ever needed to convert a byte array (byte []) into a displayable hex string? The JDK does not provide this capability for byte arrays, only for the wrapper classes.

Below is the code which will convert a byte array into a displayable "hex" representation of the string.

Now you can turn

		 byte b[] = {0xf1, 0x0d,  0x3c, 0x44};
		
into:
		 String s =  "F10D3C44"
		
like this:
		 String s =  byteArrayToHexString(b);
		
  • provides the same functionality found in Integer.toHexString(), Short.toHexString(), etc...
  • Simple to use
  • Fast, efficient and accurate
  • Tested and proven
Here's the code:
	 /**
* Convert a byte[] array to readable string format. This makes the "hex"readable!
* @return result String buffer in String format
* @param in byte[] buffer to convert to string format
*/
static String byteArrayToHexString(byte in[]) {
byte ch = 0x00;
int i = 0;
if (in == null || in.length <= 0)
return null;

String pseudo[] = {"0", "1", "2","3", "4", "5", "6", "7", "8","9", "A", "B", "C", "D", "E","F"};
StringBuffer out = new StringBuffer(in.length * 2);

while (i < in.length) {
ch = (byte) (in[i] & 0xF0); // Strip offhigh nibble
ch = (byte) (ch >>> 4); // shift the bits down
ch = (byte) (ch & 0x0F); // must do this is high order bit is on!
out.append(pseudo[ (int) ch]); // convert thenibble to a String Character
ch = (byte) (in[i] & 0x0F); // Strip offlow nibble
out.append(pseudo[ (int) ch]); // convert thenibble to a String Character
i++;
}
String rslt = new String(out);
return rslt;
}
Stay tuned, I'll present some additional routines which take the "hex" data and format it into a "Hex Dump" format like: 0000: 32 00 00 00 00 49 99 04 13 14 56 20 17 55 2B 3F ____[2 I™V U+?]
0010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ____[ ]
0020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ____[ ]
The above example was created using byteArrayToHexString() along with another routine....I also have a hex dump routine which dumps the hex data in "old" mainframe EBCDIC style format where each byte is display one nibble on top of the other.

 

阿涂 发表于 2006-04-28 23:02  阅读(1562) 评论( 5) 引用( 5) Java
所有人可见

  • 收藏文章:
  • save at del.icio.us
  • save at digg
  • save at my yahoo
  • save at blinklist
  • save at furl
  • save at simpy
  • save at blogmarks
  • submit at reddit
  • save at spurl
  • save at shadows
  • save at rawsugar
  • save at bloglines

引用

http://www.uuzone.com/app/trackBack.do?type=blog&trackBackID=99156

black dick
black boysindian porncum picsspy cam

Posted byblack dickat 2006-11-15 20:25

panties teen
sexy picturesmature womenhairy bearsskinny women

Posted bypanties teenat 2006-11-15 19:56

sex sites
kobe taiinterracial sexthai boysasian girls

Posted bysex sitesat 2006-11-15 16:31

gay asian
thong girlboys picturetopless girlsolder women nude

Posted bygay asianat 2006-11-15 04:15

hairy cunt
japanese lesbianhuge dicknude twinkcumshot pictures

Posted byhairy cuntat 2006-11-15 03:46

相关内容
更多..

回复列表每两分钟自动刷新一次,想立即刷新吗?点击这里

您的浏览器可能不支持Frame, 优友地带需要使用Frame才能显示正常页面!