kevinl kevinl - 6 months ago 11
iOS Question

iOS: Can't print euro symbol on bluebamboo printer

I'm trying to print text using some receipt data, and I need to use the euro symbol (\xE2\x82\xAC €) but the P25i mobile printer is printing "â ¬" instead.

My code:

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];


I've even tried replacing the [_printBuffer cStringUsingEncoding:x] part with NSWindowsCP1252StringEncoding and other encodings, but I'm not having any luck. Is there something I'm missing?

Thanks!

Edit:

I've even tried replacing actual bytes in the NSData that follows:

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"];
}
}


Using this, I get my NSData to be something like:

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


When you plug this data into something like http://string-functions.com/hex-string.aspx, then I get:

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

Answer

I figured out that I need to be providing the correct HEX values based on the ISO-8859-15 encoding.

More specifically, http://www.fileformat.info/info/unicode/char/20ac/charset_support.htm shows that 0xA4 is what I need to be using to print out the euro symbol.

updated code:

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"];
        }

    }