Has anyone smiler c++ code to get hex dump of byte array ?
If not
I have Java method, please guide me to convert to c++
Java code
- Code: Select all
public static String hexdump (byte[] b, int offset, int len) {
StringBuffer sb = new StringBuffer ();
StringBuffer hex = new StringBuffer ();
StringBuffer ascii = new StringBuffer ();
String sep = " ";
String lineSep = System.getProperty ("line.separator");
for (int i=offset; i<len; i++) {
char hi = Character.forDigit ((b[i] >> 4) & 0x0F, 16);
char lo = Character.forDigit (b[i] & 0x0F, 16);
hex.append (Character.toUpperCase(hi));
hex.append (Character.toUpperCase(lo));
hex.append (' ');
char c = (char) b[i];
ascii.append ((c >= 32 && c < 127) ? c : '.');
int j = i % 16;
switch (j) {
case 7 :
hex.append (' ');
break;
case 15 :
sb.append (hexOffset (i));
sb.append (sep);
sb.append (hex.toString());
sb.append (' ');
sb.append (ascii.toString());
sb.append (lineSep);
hex = new StringBuffer ();
ascii = new StringBuffer ();
break;
}
}
if (hex.length() > 0) {
while (hex.length () < 49)
hex.append (' ');
sb.append (hexOffset (b.length));
sb.append (sep);
sb.append (hex.toString());
sb.append (' ');
sb.append (ascii.toString());
sb.append (lineSep);
}
return sb.toString();
}
