Gerdinando
Gerdinando

Reputation: 57

Dynamic escape sequence in OBJ-C

I'm trying to get a string like \x8 and append the bytes to data. Unfortunately whenever I use @"\\x8, OBJ-C doesn't recognize it's a hexcode. Which encoding or how can I code it so I can use dynamic escape sequences?

SAMPLE:

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

Upvotes: 0

Views: 103

Answers (1)

rmaddy
rmaddy

Reputation: 318814

Since you already have an number, why not use it this way:

Change the data type of hex to uint8_t. Then you can append the byte by doing:

[_data appendBytes:&hex length:1];

Append the ESC and ! before appending the byte.

Upvotes: 1

Related Questions