1. 支持String转 GB2312 编码

This commit is contained in:
HuangXin 2023-09-03 16:27:30 +08:00
parent d29ed06a94
commit fac5207c5d
2 changed files with 14 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package com.zjyr.beidouservice.adapter.impl.netty.encode; package com.zjyr.beidouservice.adapter.impl.netty.encode;
import com.zjyr.beidouservice.common.impl.ControlCommandName; import com.zjyr.beidouservice.common.impl.ControlCommandName;
import com.zjyr.beidouservice.misc.HelperUtils;
import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol; import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol;
import com.zjyr.beidouservice.pojo.vo.binary.HeartProtocol; import com.zjyr.beidouservice.pojo.vo.binary.HeartProtocol;
import com.zjyr.beidouservice.pojo.vo.binary.QuerySensorProtocol; import com.zjyr.beidouservice.pojo.vo.binary.QuerySensorProtocol;
@ -14,7 +15,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
@Component @Component
@RequiredArgsConstructor @RequiredArgsConstructor
@ -46,7 +46,8 @@ public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryPr
byte[] ttsArray = new byte[0]; byte[] ttsArray = new byte[0];
if (msg.getTtsContent() != null && !msg.getTtsContent().isEmpty()) { if (msg.getTtsContent() != null && !msg.getTtsContent().isEmpty()) {
ttsArray = msg.getTtsContent().getBytes(StandardCharsets.UTF_16BE); //ttsArray = msg.getTtsContent().getBytes(StandardCharsets.UTF_16BE);
ttsArray = HelperUtils.convertStringToGB2312(msg.getTtsContent());
ttsSize = ttsArray.length; ttsSize = ttsArray.length;
} else { } else {
ttsSize = 0; ttsSize = 0;
@ -64,7 +65,7 @@ public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryPr
byteBuf.writeByte(msg.getRepeatTimes().byteValue()); byteBuf.writeByte(msg.getRepeatTimes().byteValue());
byteBuf.writeShort((short) ttsSize); byteBuf.writeShort((short) ttsSize);
if(ttsSize > 0) { if (ttsSize > 0) {
byteBuf.writeBytes(ttsArray); byteBuf.writeBytes(ttsArray);
} }

View File

@ -1,5 +1,8 @@
package com.zjyr.beidouservice.misc; package com.zjyr.beidouservice.misc;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
public class HelperUtils { public class HelperUtils {
public static String bytesToHexString(byte[] bArray) { public static String bytesToHexString(byte[] bArray) {
StringBuilder sb = new StringBuilder(bArray.length); StringBuilder sb = new StringBuilder(bArray.length);
@ -13,6 +16,13 @@ public class HelperUtils {
} }
return sb.toString(); return sb.toString();
} }
public static byte[] convertStringToGB2312(String str) throws UnsupportedEncodingException {
String utf8 = new String(str.getBytes(StandardCharsets.UTF_8));
String unicode = new String(utf8.getBytes(), StandardCharsets.UTF_8);
String gbk = new String(unicode.getBytes("GB2312"));
return gbk.getBytes();
}
// //
// @SuppressWarnings("unchecked") // @SuppressWarnings("unchecked")
// public static <T> BaseBinaryProtocol<T> CastBaseBinaryProtocol(Object obj) { // public static <T> BaseBinaryProtocol<T> CastBaseBinaryProtocol(Object obj) {