0

\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];
}
4

1 に答える 1

1

すでに番号を持っているので、次のように使用しないでください。

のデータ型をhexに変更しますuint8_t。次に、次のようにしてバイトを追加できます。

[_data appendBytes:&hex length:1];

!バイトを追加する前に ESC を追加します。

于 2013-08-14T23:07:07.053 に答える