last day (15 days later) » 

5:28 PM
1
A: Append bytes to NSMutableData in Swift

Martin RAs already said, in Swift a string stores Unicode characters, and not – as in (Objective-)C – an arbitrary (NUL-terminated) sequence of char, which is a signed or unsigned byte on most platforms. Now theoretically you can retrieve a C string from a Swift string: let commands = NSMutableData() l...

 
Got it, if I would want to input an Ä with other text i guess i would do it like this? commands.appendData("Test\nAddress\n\u{c384}rtor\n".dataUsingEncoding(NSUTF8Stri‌​‌​ngEncoding)!) but the printer actually prints 8Ää (8 on its side) why is that?
 
@SamPettersson: In a Swift string you can simply write "Ä", or equivalently "\u{c4}". \u{c384} represents the Unicode code point U+C384 = HANGUL SYLLABLE SSE = 쎄. The UTF-8 representation of that character is EC 8E 84 ... - For a more specific answer we need to know exactly which byte sequence is expected by the printer to print an "Ä", in particular which encoding should be used for the character.
 
if I write Ä in my string i get a character which looks like |-ä?
 
@SamPettersson: Sorry, but that is too vague. Can you update your question with the exact code and the expected and actual result? Or is this a different question?
 
Basically I can send the printer a string of characters with the appendData function of NSMutableData, if I send "Test123\u{D6}\n" where D6 is the ASCII hex for Ö I get this imgur.com/ba6ynZb
 
5:28 PM
@SamPettersson: \u{D6} converted to UTF-8 is C3 96. I do not know the specification of your printer, so I cannot guess what it expects, perhaps some ISO encoding instead of UTF-8.
@SamPettersson: I have updated the answer, perhaps that helps.
 
The printer expects CP865 (also called Nordic (DOS)) I found out that 0x2147484698 is the UInt32 value for that encoding, so I tried this which didn't compile:
var DOSNordicEncoding: NSStringEncoding = 0x2147484698

        commands.appendData("Test123Ö\n\n\n\n".dataUsingEncoding(DOSNordicEncoding)!)
edit: it does compile but crashes
 
@SamPettersson: Hi! – I am currently busy with other things, but I will return later and have a look at it!
 
Thanks!
 
 
1 hour later…
6:57 PM
@SamPettersson: The CF865 encoding is 2147484698 (decimal) not 0x2147484698. You can get the programmatically as
let enc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue))
 
Fixed it by doing CFStringConvertEncodingToNSStringEncoding(0x041A), but thanks.
 
Sorry, I meant
let enc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.DOSNordic.rawValue))
using .DOSNordic instead of a "magic number".
 

last day (15 days later) »