Created
June 26, 2018 20:41
-
-
Save lukecampbell/a51b4049f2a149c3220292b0cb07810c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Hexdump { | |
public static void main(String[] args) { | |
System.out.println("Hello"); | |
System.out.println(hexdump("Hello helo hell")); | |
} | |
public static String hexdump(String str) { | |
char[] chars = str.toCharArray(); | |
StringBuilder sb = new StringBuilder(); | |
/* | |
* ooooo xx xx xx xx xx xx xx xx |........| | |
*/ | |
for (int i = 0; i <= chars.length / 8; i++) { | |
if (i * 8 >= chars.length) { | |
break; | |
} | |
sb.append(String.format("%04x ", i)); | |
for (int j = 0; j < 8; j++) { | |
if (i * 8 + j >= chars.length) { | |
sb.append(" "); | |
} else { | |
sb.append(String.format("%02x ", (int) chars[i * 8 + j])); | |
} | |
} | |
sb.append(" |"); | |
for (int j = 0; j < 8; j++) { | |
if (i * 8 + j >= chars.length) { | |
sb.append(" "); | |
continue; | |
} | |
int value = (int) chars[i * 8 + j]; | |
if (value >= 48 && value <= 57) { | |
sb.append(String.format("%c", chars[i * 8 + j])); | |
} else if (value >= 65 && value <= 90) { | |
sb.append(String.format("%c", chars[i * 8 + j])); | |
} else if (value >= 97 && value <= 122) { | |
sb.append(String.format("%c", chars[i * 8 + j])); | |
} else { | |
sb.append("."); | |
} | |
} | |
sb.append("|\n"); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment