private void doEcho(byte buffer[], int nBytes)
{
synchronized(System.out)
{
Date currentTime = new Date();
System.out.println("[" + name + ", (" + nBytes + " bytes) " + formatter.format(currentTime) +"]");
StringBuffer sb = new StringBuffer(nBytes); // formatted string
StringBuffer pb = new StringBuffer(4 * nBytes); // formatted binary string
String hb = new String(); // hex buffer string
String tb = new String(); // text buffer string
int j = 0; // line position counter
int c = 0; // byte counter
float k = 0; // binary character counter for(int i = 0; i < nBytes; i ++)
{
int value = (buffer[i] & 0xFF); // 32 bit Unicode to 16 bit ASCII
if(value == '\r' || value == '\n' || value == '\t' ||
(value >= ' ' && value <= '~')) {
sb.append((char)value); // text character
}
else {
sb.append("[" + hexPad(Integer.toHexString(value), 2) + "]"); // binary character
k++;
} if (value >= ' ' && value <= '~') {
tb = tb + (char)value; // "printable" character
} else {
tb = tb + "."; // non-printable
} hb = hb + hexPad(Integer.toHexString(value), 2);
if (j == 3 || j == 7 || j == 11) { // for readability, space every 4 chars
hb = hb + " ";
tb = tb + " ";
}
if (j == 15) { // 16 characters per line
pb.append(hexPad(Integer.toHexString(c), 4) +": " + hb + " " + tb + "\n");
j = 0;
c += 16;
hb = "";
tb = "";
} else {
j++;
}
}
for (int i = hb.length(); i < 35; i++) {
hb = hb + " "; // pad to length of other lines
}
pb.append(hexPad(Integer.toHexString(c), 4) +": " + hb + " " + tb + "\n");
if ((k/nBytes) < .05) { // less than 5% binary?
System.out.println(sb.toString()); // print as text
} else {
System.out.println(pb.toString()); // print as hex dump
}
}
}
/* prepend 0's to a hex string
*/
private String hexPad(String a, int length)
{
for (int j = a.length(); j < length; j++) {
a = "0" + a;
}
return a;
}
{
synchronized(System.out)
{
Date currentTime = new Date();
System.out.println("[" + name + ", (" + nBytes + " bytes) " + formatter.format(currentTime) +"]");
StringBuffer sb = new StringBuffer(nBytes); // formatted string
StringBuffer pb = new StringBuffer(4 * nBytes); // formatted binary string
String hb = new String(); // hex buffer string
String tb = new String(); // text buffer string
int j = 0; // line position counter
int c = 0; // byte counter
float k = 0; // binary character counter for(int i = 0; i < nBytes; i ++)
{
int value = (buffer[i] & 0xFF); // 32 bit Unicode to 16 bit ASCII
if(value == '\r' || value == '\n' || value == '\t' ||
(value >= ' ' && value <= '~')) {
sb.append((char)value); // text character
}
else {
sb.append("[" + hexPad(Integer.toHexString(value), 2) + "]"); // binary character
k++;
} if (value >= ' ' && value <= '~') {
tb = tb + (char)value; // "printable" character
} else {
tb = tb + "."; // non-printable
} hb = hb + hexPad(Integer.toHexString(value), 2);
if (j == 3 || j == 7 || j == 11) { // for readability, space every 4 chars
hb = hb + " ";
tb = tb + " ";
}
if (j == 15) { // 16 characters per line
pb.append(hexPad(Integer.toHexString(c), 4) +": " + hb + " " + tb + "\n");
j = 0;
c += 16;
hb = "";
tb = "";
} else {
j++;
}
}
for (int i = hb.length(); i < 35; i++) {
hb = hb + " "; // pad to length of other lines
}
pb.append(hexPad(Integer.toHexString(c), 4) +": " + hb + " " + tb + "\n");
if ((k/nBytes) < .05) { // less than 5% binary?
System.out.println(sb.toString()); // print as text
} else {
System.out.println(pb.toString()); // print as hex dump
}
}
}
/* prepend 0's to a hex string
*/
private String hexPad(String a, int length)
{
for (int j = a.length(); j < length; j++) {
a = "0" + a;
}
return a;
}
解决方案 »
- 菜鸟求解答(继承、对象调用)
- IIS 安装不上求9 啊 急!!
- 编写一个Applet,用来处理指定的键盘事件:当在键盘上按下某个字母键时,就显示该键编码和字母本身
- 各位同人忙忙,我一位刚学JAVA的新手,能给我提供一些JAVA连接SQL2000数据库的文章吗?
- 终于升成2个三角了,是不是该散点分呢??UP者有分哈
- 关于事件处理的问题!
- ascii码转换的问题hashcode()的反函数?应该是什么
- 我的Jbuilder7编辑器有问题,帮帮忙
- 怎样在jbuilder中建立和运行一个类似c++里可以建立和运行一个c语言程序!!!
- 面试题:求高效代码,将十六进制转换为十进制数
- 提交后,如何取得<select>中的多个选择的值?
- 在JAVA中能否使用API呀?如何调用?
bi.toString(16);