0

I am working at printing an image to a thermal printer. The image needs to be converted into an unsigned char buffer. For example

unsigned char buffer[10]={0x55,0x66,0x77,0x88,0x44, 0x1B,0x58,0x31,0x15,0x1D}

So far I can covert the image to a black and white version and then loop through it to get each hexadecimal value as an NSString stored in an NSMutableArray. Like below when outputted in the NSLog

( 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 )

I need to be able to loop through the array and retrieve each hex value stored as a string and convert it into an unsigned char so I can add it to my buffer to print

Byte buffer[hexStringArray count];
for (int i=0;i=[hexStringArray count];i++)//add print content
{
    buffer[i] = [hexStringArray objectAtIndex:i] // this from string to hex to unsigned char;
}
[sessionController writeData:[NSData dataWithBytes:buffer length:[hexStringArray count]];

How can I convert an NSString hex value into an actual hex, which can then be converted into an unsigned char.

  • 1
    Why are the hex codes of the image data represented as an `NSString`? That seems like a strange and difficult starting point. Is there a way to change that starting point to make this a lot easier? – rmaddy Jun 18 '15 at 21:13
  • Its because the code sample I am using to convert the UIImage to hex values stores them as a string. I agree it would be easier if it was stored automatically as a hex '[writeString addObject:[NSMutableString stringWithFormat:@"0x%02X",wp]];' – Duncan Shingleton Jun 18 '15 at 21:22
  • Then you need better sample code. Don't make this part harder by doing the initial part the wrong way. Update your question with the relevant code starting with the original `UIImage`. Then people can offer answers showing the proper way to do this. – rmaddy Jun 18 '15 at 21:25
  • I'm also interested in this, I need the image into black and white byte array to be able to send it to the printer buffer – Jordi Puigdellívol Jul 02 '15 at 13:48

0 Answers0