比如我有十六进制字符串“2E0A”,怎样转化为“0A2E”?
转成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);
}
两个两个拆了,再倒叙排一下呗
public static void main(String[] args) {
System.out.println(hightolow("01 02"));
}
public static String hightolow(String temp) {
List<String> list = Arrays.asList(temp.split(" "));
Collections.reverse(list);
StringBuffer stringBuffer = new StringBuffer();
for(String string:list){
stringBuffer.append(string);
}
return stringBuffer.toString();
}
大佬帮我看看,我这输入的时候字符串中间必须加空格,怎样才能去掉?
@阿飞滴滴滴: 不需要按空格分隔,你按长度分隔就行了啊,可以参考下别人的分隔实现https://www.jianshu.com/p/c33281a91f20