Public static string printhex (byte [] data, int rowlength) {If (rowlength % 2 = 1) throw new argumentexception ("must be an even number! "); Var buffer = new stringbuilder (); string segmentnumber =" "; string bytes =" "; string ASCII =" "; for (INT I = 1; I <= data. length; I ++) {bytes + = (data [I-1]. tostring ("X2") + ""; if (data [I-1] <0x21 | data [I-1]> 0x7e) {ASCII + = ". ";} else {ASCII + = encoding. ASCII. getstring (New byte [1] {data [I-1]});} if (I % rowlength! = 0 & I % (rowlength/2) = 0) {bytes + = ""; ASCII + = "" ;}if (I % rowlength = 0) {segmentnumber = (I-rowlength)/rowlength) * rowlength ). tostring ("X4"); buffer. appendline (segmentnumber + "" + bytes + "" + ASCII); bytes = ""; ASCII = ""; continue;} if (I = data. length) {segmentnumber = (I-rowlength)/rowlength) + 1) * rowlength ). tostring ("X4"); buffer. appendline (segmentnumber + "" + bytes. padright (rowlength * 3 + 1, '') +" "+ ASCII) ;}} return buffer. tostring ();}
Example:
Data is input in hexadecimal text to automatically adjust the output width