转成char[],然后for循环+交换一下顺序,最后把char[]再转成string。
@Test
public void test_reverseHex() {
Assert.assertEquals("23010A2E", reverseHex("2E0A0123"));
Assert.assertEquals("2E0A0123", reverseHex(reverseHex("2E0A0123")));
}
private static String reverseHex(final String hex) {
final char[] charArray = hex.toCharArray();
final int length = charArray.length;
final int times = length / 2;
for (int c1i = 0; c1i < times; c1i += 2) {
final int c2i = c1i + 1;
final char c1 = charArray[c1i];
final char c2 = charArray[c2i];
final int c3i = length - c1i - 2;
final int c4i = length - c1i - 1;
charArray[c1i] = charArray[c3i];
charArray[c2i] = charArray[c4i];
charArray[c3i] = c1;
charArray[c4i] = c2;
}
return new String(charArray);
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。