java console print array

前言

在cm中也看到有打印16进制数组的实现,整的好复杂.

写了一段代码,用来打印16进制数组. 这样, 在收完包后,就不用再对着wireshark来看, 到底收包了什么内容. 本来要打出一行16进制内容后, 再打印出16进制对应的字符内容. 整复杂了,以后再弄。

java支持移位操作和16进制数值写法,这个不错。

实验

        // 一个完整数据包读完了, 打印一下包的字节内容
        print_uchar_array("one packet recv over", m_uc_ary_buffer);
    }
    
    private void print_uchar_array(String str_tip, byte ary_in[])
    {
    	int i = 0;
        int i_tmp = 0;
        int i_tmp1 = 0;
        int i_tmp2 = 0;
        int i_index = 0;

        System.out.printf("========================================\n");
        System.out.printf("%s %d bytes\n", str_tip, ary_in.length);
        for (i = 0; i < ary_in.length; i++) {
        	i_tmp = ary_in[i];
        	System.out.printf("%c%c ", bit4_to_hex_char((i_tmp >> 4) & 0x0f), bit4_to_hex_char(i_tmp & 0x0f));
        	i_index += 1;
        	
        	if ((i_index % 16) == 0) {
        		i_index = 0;
        		System.out.printf("\n");
        	} else if ((i_index % 8) == 0)  {
        		System.out.printf(" ");
        	}
        }
        System.out.printf("\n----------------------------------------\n");
    }
    
    private int bit4_to_hex_char(int in)
    {
    	if ((in >= 0) && (in <= 9)) {
    		in += '0';
    	} else if ((in >= 10) && (in <= 15)) {
    		in -= 0x0a;
    		in += 'A';
    	} else {
    		in = '.';
    	}
    	return in;
    }

效果

========================================
one packet recv over 8 bytes
00 08 00 00 0B 00 00 00  
----------------------------------------
========================================
one packet recv over 24 bytes
00 18 00 00 02 00 00 00  01 34 00 00 08 00 7F FF 
01 00 00 00 00 18 61 01  
----------------------------------------
========================================
one packet recv over 127 bytes
00 7F 00 00 06 00 00 00  00 00 DE AD BE EF 00 75 
00 00 00 00 00 04 00 00  04 00 03 00 00 00 00 00 
04 00 05 0B 20 01 00 00  02 00 06 00 1F 00 0E 00 
01 DE AD BE EF 00 03 00  00 00 02 00 04 00 01 00 
01 00 02 00 00 00 00 00  04 00 05 0B 20 01 00 00 
02 00 06 FB FF 00 02 00  02 00 00 00 00 00 04 00 
05 0B 20 01 00 00 01 00  02 00 00 03 00 02 00 00 
00 00 00 04 00 05 0B 20  01 00 00 01 00 02 00 
----------------------------------------

猜你喜欢

转载自blog.csdn.net/LostSpeed/article/details/82814347
今日推荐