我正在尝试使用一些收据数据打印文本,我需要使用欧元符号(\ xE2 \ x82 \ xAC€),但P25i移动打印机正在打印“â”。
我的代码:
int contentLength = (int)[_printBuffer length];
unsigned char buffer[contentLength+6];
int n = 0;
buffer[n++] = 0X55; buffer[n++] = 0x66; buffer[n++] = 0x77; buffer[n++] = 0x88; buffer[n++] = 0x44; //print command
//buffer[n++] = 29; buffer[n++] = 80; buffer[n++] = 600; buffer[n++] = 203; // motion units
//buffer[n++] = 29; buffer[n++] = 76; buffer[n++] = 20; buffer[n++] = 0; // left margin
//buffer[n++] = 27; buffer[n++] = 68; buffer[n++] = 5; buffer[n++] = 0; buffer[n++] = 9; // tab
//buffer[n++] = 27; buffer[n++] = 97; buffer[n++] = 1; // buffer[n++] = 49; // center
const char *cstr = [_printBuffer cStringUsingEncoding:NSUTF8StringEncoding];
for (int i = 0;i < contentLength; i++) { //add print content
NSLog(@"test: %c", cstr[i]);
if (cstr[i] == '\xE2' && cstr[i+1] == '\x82' && cstr[i+2] == '\xAC') NSLog(@"euro broken down: %c %c %c", cstr[i], cstr[i+1], cstr[i+2]);
if (cstr[i] == '\xC2') {
buffer[i+n] = ' '; // Don't add the Â,¿,â symbol in front of the currency values
} else {
buffer[i+n] = cstr[i];
}
}
NSData *data = [NSData dataWithBytes:(const uint8_t *)buffer length:contentLength+n];
[session writeData:data State:CPSTATE];
我甚至尝试用NSWindowsCP1252StringEncoding和其他编码替换[_printBuffer cStringUsingEncoding:x]部分,但我没有运气。有什么我想念的吗?
谢谢!
编辑:
我甚至尝试替换后面的NSData中的实际字节:
NSMutableData *tempData = [NSMutableData dataWithData:data];
const char* fileBytes = (const char*)[data bytes];
NSUInteger length = [data length];
NSUInteger index;
for (index = 0; index<length; index++) {
if (fileBytes[index] == '\xE2' && fileBytes[index+1] == '\x82' && fileBytes[index+2] == '\xAC') {
NSRange r = NSMakeRange(index, 3);
[tempData replaceBytesInRange:r withBytes:"\x80\x00\x00"];
}
}
使用这个,我得到的NSData就像:
55667788 44202020 20202020 20526563 65697074 0a202032 32206a75 696c2e20 32303135 2031353a 34330a0a 4d616461 6d652054 75737361 75647320 4f726c61 6e646f0a 0a436f6e 6669726d 6174696f 6e202320 36303030 31393937 380a0a46 6c6f7269 64612041 6e6e7561 6c205061 73730a49 6e646976 69647561 6c202020 20202031 34392c30 3020a080 00000a0a 53756274 6f74616c 3a202020 20202020 3134392c 303020a0 8000000a 5461783a 20202020 20202020 20202020 2020392c 363920a0 8000000a 546f7461 6c3a2020 20202020 20202020 3135382c 363920a0 8000000a 0a506179 6d656e74 730a5669 73612031 31313120 20202020 20203135 382c3639 20a08000 000a4175 74682043 6f646520 20202020 20202020 31323334 35360a0a 4167656e 743a7465 73740a53 74617469 6f6e3a48 616e64
当您将此数据插入http://string-functions.com/hex-string.aspx之类的内容时,我会得到:
UfwˆD Receipt
22 juil. 2015 15:43
Madame Tussauds Orlando
Confirmation # 600019978
Florida Annual Pass
Individual 149,00 €
Subtotal: 149,00 €
Tax: 9,69 €
Total: 158,69 €
Payments
Visa 1111 158,69 €
Auth Code 123456
Agent:test
Station:Hand
答案 0 :(得分:0)
我发现我需要根据ISO-8859-15编码提供正确的HEX值。
更具体地说,http://www.fileformat.info/info/unicode/char/20ac/charset_support.htm表明我需要使用0xA4打印出欧元符号。
更新代码:
for (index = 0; index<length; index++)
{
if (fileBytes[index] == '\xE2' && fileBytes[index+1] == '\x82' && fileBytes[index+2] == '\xAC') {
NSRange r = NSMakeRange(index, 3);
[tempData replaceBytesInRange:r withBytes:"\xA4\x00\x00"];
}
}