16进制的字符串转byte[]数组
public class ClientString16 {
@Test
public void get16Str(){
String str="48 47 12 00 14 12 16 08 15 0d 30 0f 02 30 30 30 30 30 30 30 30 30 30 00 c2";
byte[] bytes = hexStringToByteArray(str);
getBytetoString(bytes);
//String sendSocket = sendSocket("",bytes,null);
//System.out.println("---sendSocket---"+sendSocket);
}
public static void getBytetoString(byte[] bytes1){
// 用字节数组的内容创建一个ByteBuf
ByteBuf buf = Unpooled.copiedBuffer(bytes1);
//将buffer内容转换为多行美化过的十六进制表示的字符串
System.out.println(ByteBufUtil.prettyHexDump(buf));
}
/**
* 16进制表示的字符串转换为字节数组
*
* @param hexString 16进制表示的字符串
* @return byte[] 字节数组
*/
public static byte[] hexStringToByteArray(String hexString) {
hexString = hexString.replaceAll(" ", "");
int len = hexString.length();
byte[] bytes = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
// 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节
bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
.digit(hexString.charAt(i + 1), 16));
}
return bytes;
}
}
将字节数组转换成十六进制的字符串
byte[] 转16进制的字符串
public static void get() {
Socket socket = null;
String strReturn = null;
try {
System.out.println("connecting...");
socket = new Socket("localhost",8090);
System.out.println("connection success");
while (true){
// 使用用户名和密码申请授权码(仅使用一次,取得正确授权码后,就不需要了。授权码应保存在磁盘上,供以后登录使用)
//String str="48 47 12 00 12 12 16 08 15 0d 30 0f 20 30 30 30 30 30 30 30 30 30 30 20 18 1C 63 FB 2F CF 5B BE 2A D9 A4 0D C2 31 F2 DF 4B FF CF 9C 68 E1 E5 63 EC AD 8F 48 78 D4 E2 89 72";
//String str="48 47 12 00 12 12 16 08 1F 0A 03 0F 20 30 30 30 30 30 30 30 30 30 30 20 6F 73 A7 02 9A CC 19 5A 16 0A 56 ED BB 45 EC DC 29 56 8D CB 67 0B D9 93 82 D6 51 F7 8C 85 52 32 B9";
//48 47 12 00 12 12 16 08 1F 0A 03 0F 10 30 30 30 30 30 30 30 30 30 30 61 00 61 E8 44 B8 06 8D EE B7 C3 E4 45 01 B7 90 A3 3F D7 68 92 80 A0 16 0F 37 AF 79 EA E3 E8 3A F5 A0 46 ED 10 1A D2 DB 2C 80 00 74 3F 05 9E 66 64 CB 6A D9 E6 A0 B8 4C 39 5D 39 04 1D 37 23 AC 59 30 AB 1B 3F AA C2 CC 9B 81 B5 30 DD BF A7 2A 88 22 2B F1 16 2F BC C9 D7 73 3C 6A 9D 2F 35 09 28 F5 C2
//String str="48 47 12 00 97 00 16 09 02 11 21 08 21 30 30 30 30 30 30 30 30 30 30 60 57 08 B5 0D 71 8E 74 94 5E 30 29 15 D1 9A D4 AA C2 FD 1E 53 E9 3A E1 CA F5 CF A2 6A 6A 74 2B 8F 87 B6 DF A1 5B CA 05 03 1F 3F 86 CF 8A 0C 05 85 7D 94 65 DE 12 4A A5 E3 EF 46 CD DE C8 13 5F 4C 17 2C 08 41 8F 31 99 44 55 AA B5 A5 A8 25 2A 8D 97 FC 22 76 11 5