Bom dia PessoALL !
Preciso enviar uma informação/string para um equipamento/impressora em formato hexadecimal.
Já tentei transformar o string em array de bytes e depois enviar mas o equipamento / impressora não interpreta corretamente.
byte[] byt = str.getBytes();
Estou utilizando o método abaixo para transformar o string em array de strings em formato hexadecimal.
Encontrei o método abaixo no site: http://www.java2s.com/Code/Java/Data-Type/dumpanarrayofbytesinhexform.htm
private static final byte[] HEX_CHAR = new byte[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
public static final String dumpBytes(byte[] buffer) {
if (buffer == null) {
return "";
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buffer.length; i++) {
sb.append("0x").append((char) (HEX_CHAR[(buffer[i] & 0x00F0) >> 4])).append(
(char) (HEX_CHAR[buffer[i] & 0x000F])).append(" ");
}
System.out.println("string-hexa: " + sb.toString());
return sb.toString();
}
dumpBytes("testando a impressora".getBytes());
** Retorno:
string-hexa: 0x74 0x65 0x73 0x74 0x61 0x6E 0x64 0x6F 0x20 0x61 0x20 0x69 0x6D 0x70 0x72 0x65 0x73 0x73 0x6F 0x72 0x61
Mas não estou conseguindo transformar cada parte do string em 01 byte para carregar no array de bytes para enviar.
Como posso fazer ? Poderiam me ajudar ?
Desde já agradeço pela ajuda e fico aguardando as respostas / sugestões.
Atenciosamente,
Wagner