我想在给定字符串的特定位置插入十六进制字节,例如0A和00,即字符串set =“ 16 10 36 07 02 00 00 00 00 00 00 00 00 00 00 00 00 0B 11 B7 93”;我想在给定字符串的第4位和第5位插入0A和00。我该如何用Java编写代码
最佳答案
我为您提供了一个简短的摘要,效果很好!
public static String insertAtPos(String input, int pos, String insert) {
return
String.format("%s%s%s%s",input.substring(0, 3 * pos), insert, " ", input.substring(3 * pos, input.length()));
}
用法:
public static void main(String[] args) {
String set= "16 10 36 07 02 00 00 00 00 00 00 00 00 00 00 0B 11 B7 93";
String s0A = "0A";
String sFF = "FF";
System.out.println(insertAtPos(set, 4, s0A));
System.out.println(insertAtPos(set, 5, sFF));
}