\x8
のような文字列を取得して、バイトを に追加しようとしていますdata
。残念ながら、 を使用するたびに@"\\x8
、OBJ-C はそれが 16 進コードであることを認識しません。動的エスケープ シーケンスを使用できるようにするには、どのエンコーディングまたはどのようにコーディングすればよいですか?
サンプル:
- (void)selectStandardPrinterMode:(int)font isEmphasized:(BOOL)emphasized isDoubleSize:(BOOL)size isUnderline:(BOOL)line
{
NSMutableString *printerMode;
unsigned int hex;
switch (font)
{
case 0:
hex = 0;
break;
case 1:
hex = 1;
break;
default:
hex = 0;
NSLog(@"[font]: unknown option");
break;
}
if (emphasized)
{
hex += 8;
}
if (size)
{
hex += 30;
}
if (line)
{
hex += 80;
}
if (hex > 100)
{
hex -= 100;
printerMode = [NSString stringWithFormat:@"%@%u", @"\xb", hex];
}
else
{
printerMode = [NSString stringWithFormat:@"%@%u", @"\\x", hex];
}
// not working
[_data appendBytes:ESC "!" length:2];
[_data appendBytes:[printerMode cStringUsingEncoding:NSASCIIStringEncoding] length:1];
// working example
[_data appendBytes:ESC "!" "\x8" length:3];
}