如何解决用无符号字节的Java发票打印CommPortIdentifier发出
|| 嘿, 我需要在热发票打印机中打印发票 我写了这个程序来做到这一点(见下文) 但是,由于本地化的原因,我需要将值范围为0x80 – 0x102的字符发送给打印机,但由于无法发送具有该值的字节(这是Java中不存在的无符号字节),因此我发现这是不可能的 关于如何将此值添加到将发送到打印机的字符串的任何想法? 谢谢import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.OutputStream;
public class SimpleWrite {
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
public static void print(String messageString){
try {
portId = CommPortIdentifier.getPortIdentifier( \"COM1\");
serialPort = (SerialPort) portId.open(\"SimpleWrite\",2000);
outputStream = serialPort.getOutputStream();
serialPort.setSerialPortParams( 9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
outputStream.write(messageString.getBytes());
serialPort.close();
}catch (IOException e) { System.out.println(e.getMessage());
}catch (PortInUseException e) { System.out.println(e.getMessage());
}catch (UnsupportedCommOperationException e) { System.out.println(e.getMessage());
}catch (NoSuchPortException e) { System.out.println(e.getMessage());}
}
public static void main(String[] args) throws IOException{
print(\"Hello,world \\n\\r\");
}
}
解决方法
当然,您可以发送一个十六进制值
0x80
的字节。
Java中byte
的值范围是:
-128 .. 127 (in decimal)
0x00 .. 0xff (in hexadecimal)
您可以使用\\u
转义以十六进制表示形式发送值,例如:
print(\"\\u0080\"); // sends 0x80 to your thermal printer
,您可以使用Unicode转义在源代码中写这样的字符,例如
print(\"Eighty: \\u0080\\n\");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。