腾讯笔试16进制字符串
传了几次代码,格式有错误,也不知道对不对,反正最后没有提交上,已GG。
-----更新
感谢 TomI同学提醒更改了代码
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); char[] arr = str.toCharArray(); int len = arr.length; for (int i = 0; i < len; i++) { if ((i + 1) % 16 == 1) { System.out.printf("%08x",i);//十六进制偏移 } String ch = Integer.toHexString(arr[i]);//十六进制字符 System.out.print(" " + ch); if (i != 0 && (i + 1) % 16 == 0) { System.out.println(" " + str.substring(i - 15, i + 1));//原文 } } } }