我在如何将String MBASE16 =“ 2701”发送到类似我的代码的字节中时遇到一些问题。
try {
socket = new Socket(address, port);
if (socket.isConnected()) {
System.out.println("Connected");
}
// sends output to the socket
out = new DataOutputStream(socket.getOutputStream());
//takes input from socket
in2 = new BufferedReader(new InputStreamReader(socket.getInputStream(), CHARSET));
byte[] MBASE16 = {0x02,0x70,0x1f};// <-- how to convert this string 2701 into byte like this
out.write(MBASE16);
字符串类具有用于将字符串转换为字节的getBytes方法
String str = "2701";
byte[] = str.getBytes(CHARSET);