0

I cannot figure out how to add my NSString to an NSMutableData structure

this is what I want to achieve, below is hardcoded and works, you can see the number string is what I want to replace with dynamic data from a helper function I have.

NSMutableData *commands = [NSMutableData data];

[commands appendBytes:"\x1b\x62\x06\x02\x02\x20" "09258384394951\x1e\r\n" length:sizeof("\x1b\x62\x06\x02\x02\x20" "09258384394951\x1e\r\n") - 1];

I tried constructing an NSString that would mimic the above exactly as follows

NSString *trNo = [NSString stringWithFormat:@"\"\x1b\x62\x06\x02\x02\x20\" \"%@\x1e\r\n\"", [NWTillHelper getCurrentOrderNumber]];

But this cannot be added to the NSMutableData construct, I tried as below

[commands appendBytes:(__bridge const void * _Nonnull)(trNo) length:sizeof("\x1b\x62\x06\x02\x02\x20" "09258384394951\x1e\r\n") - 1];

How can I create an appendBytes construct in such a way that I can insert my NSString into it and get it to be dynamic?

---- Further clarification -----

I am using the NSData structure to send to a printer

for example the following is the code to cut the label

 // Cut the paper
    [commands appendBytes:"\x1b\x64\x02" length:sizeof("\x1b\x64\x02") - 1];

This is the command for making the text aligned center

// Alignment (Center)
    [commands appendBytes:"\x1b\x1d\x61\x01" length:sizeof("\x1b\x1d\x61\x01") - 1];

As you can see these are plain Hex commands and they work fine as is

The problem I am having is that the command to print a barcode includes a dynamic string in the middle of all the HEX code and I cannot figure out how to get an NSString into the middle of this HEX code.

This is the command to print a barcode, where the barcode contains the numbers 09258384394951, as you can see those are hardcoded below. This code works fine, the printer prints the barcode correctly but the problem is that I cannot hardcode the numbers/characters 09258384394951 I need that to be a variable of some sort and this is where I am stuck.

[commands appendBytes:"\x1b\x62\x06\x02\x02\x20" "09258384394951\x1e\r\n" length:sizeof("\x1b\x62\x06\x02\x02\x20" "09258384394951\x1e\r\n") - 1];
Matt Douhan
  • 2,053
  • 1
  • 21
  • 40
  • You can try convert the string to bytes first. NSData *strByte = [trNo dataUsingEncoding:NSUTF8StringEncoding]; – GeneCode May 22 '17 at 06:12
  • Doesn't work, but printing trio, shows me the following, maybe I am not escaping right? trNo= "b " "1-20170522141541 – Matt Douhan May 22 '17 at 06:17
  • tried escaping some more but still don't work NSString *trNo = [NSString stringWithFormat:@"\"\\x1b\\x62\\x06\\x02\\x02\\x20\\\" \"%@\\x1e\\r\\n\\\"", [NWTillHelper getCurrentOrderNumber]]; – Matt Douhan May 22 '17 at 06:22
  • @GeneCode: In this case UTF-8 is a very bad solution, because Matt inserts always bytes in his string. If he would insert a byte greater than 127 he would get a multibyte sequence. `NSISOLatin1StringEncoding` should be a better choice. – clemens May 22 '17 at 06:22
  • NSString *trNo = [NSString stringWithFormat:@"\"\\x1b\\x62\\x06\\x02\\x02\\x20\\\" \"%@\\x1e\\r\\n\\\"", [NWTillHelper getCurrentOrderNumber]]; NSData *strByte = [trNo dataUsingEncoding:NSISOLatin1StringEncoding]; still don't work – Matt Douhan May 22 '17 at 06:26
  • the new escaping shows me the string is correct now 2017-05-22 14:25:57.900639 NWMobileTill[8796:2654546] trNo= "\x1b\x62\x06\x02\x02\x20\" "1-20170522142547\x1e\r\n\" – Matt Douhan May 22 '17 at 06:29
  • Do you really have to work with nsdata? A suggestion: is it plausible for you to convert everything in string, work with that (append/etc), and then change the resulting str to nsdata? – GeneCode May 22 '17 at 07:09

2 Answers2

1

Add the data in 3 steps:

NSMutableData *commands = [NSMutableData data];
NSData *orderNumber = [@"09258384394951" dataUsingEncoding:NSUTF8StringEncoding];  // example of order number converted to NSData
[commands appendBytes:"\x1b\x62\x06\x02\x02\x20" "" length:sizeof("\x1b\x62\x06\x02\x02\x20" "") - 1];  
[commands appendData:orderNumber];  
[commands appendBytes:"\x1e\r\n" length:sizeof("\x1e\r\n") - 1];    
Willeke
  • 14,578
  • 4
  • 19
  • 47
-1

Try this:

[commands appendData:[trNo dataUsingEncoding:NSUTF8StringEncoding]];
YeaTheMans
  • 1,005
  • 8
  • 19
  • This prints the actual string, as is, I guess the printer expects byte converted data? isn't that what the appendBytes does? – Matt Douhan May 22 '17 at 07:21
  • Oh, are you kinda trying to print hexadecimal string to nsdata? – YeaTheMans May 22 '17 at 07:39
  • Yes I am trying to create an nsdata string that looks exactly that the one in my question but that one is hard coded, I simply want to be able to make it dynamic – Matt Douhan May 22 '17 at 07:41
  • So you basically want to input the "hexadecimal" code straight into the nsmutabledata? so when you use something like "hexdump" on the file, it will show the same hex string? kinda? – YeaTheMans May 22 '17 at 08:10
  • I want to do exactly this, [commands appendBytes:"\x1b\x62\x06\x02\x02\x20" "09258384394951\x1e\r\n" length:sizeof("\x1b\x62\x06\x02\x02\x20" "09258384394951\x1e\r\n") - 1]; but change the 09258384394951 for a variable – Matt Douhan May 22 '17 at 08:13
  • Sorry I'm not quite following – YeaTheMans May 22 '17 at 08:26
  • The line in my comment above works fine, it prints a barcode instead of 09258384394951, so that command structure works, but of course the string 09258384394951 is hardcoded in my appendBytes structure, all I want is to be able to replace 09258384394951 in the above line of code (which works) with a variable so I can print different barcodes to the printer. – Matt Douhan May 22 '17 at 08:27
  • So you want to "edit" already set NSMutableData, like delete its characters? – YeaTheMans May 22 '17 at 08:32
  • not really edit I am creating the string from scratch, so assume an empty NSMutableData, and I need to build a structure that looks exactly like this, [commands appendBytes:"\x1b\x62\x06\x02\x02\x20" "09258384394951\x1e\r\n" length:sizeof("\x1b\x62\x06\x02\x02\x20" "09258384394951\x1e\r\n") - 1]; how would I build that when the number 09258384394951 is held in a NSString and cannot be hardcoded like above? – Matt Douhan May 22 '17 at 08:34
  • so you want this exact string in a nsmutabledata "\x1b\x62\x06\x02\x02\x20" or its values? – YeaTheMans May 22 '17 at 09:50
  • The exact string – Matt Douhan May 22 '17 at 09:55
  • If you want the exact string including \x as NSMutableData then in front of each \ and another \ eg. \\x, that will tell the string to ignore the \ in front of the x – YeaTheMans May 23 '17 at 02:55
  • /Users/mdouhan/Documents/dev/NWMobileTill/NWMobileTill/StdUK.m:511:27: Implicit conversion of Objective-C pointer type 'NSData *' to C pointer type 'const void * _Nonnull' requires a bridged cast, I get this when trying to add my NSData, I am sorry This really has me beat – Matt Douhan May 23 '17 at 03:45
  • Because your using appendBytes: not appendData: – YeaTheMans May 23 '17 at 05:33