I used a NSInputStream to read data from a socket server, and the byte data i received is a gb2312 string, so i can not use the code below to convert byte to NSString:
NSString(bytes: data!, length: data!.count, encoding: NSUTF8StringEncoding)
how could i convert the byte i received to a gb2312 encoding string?
==========================================================================
whole code is like this:
var inputStream :NSInputStream?
var outputStream:NSOutputStream?
NSStream.getStreamsToHostWithName(ip, port: port, inputStream: &inputStream, outputStream: &outputStream)
let reader = inputStream
let writer = outputStream
writer?.open()
reader?.open()
var message : UInt8 = 0
while reader!.read(&message, maxLength: 1)>0
{
let wa = NSString(bytes: &message, length: 1, encoding: CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.GB_18030_2000.rawValue))) as! String
}
when the message i receive is a chinese character, the xcode throws this: fatal error:unexpected found nil while unwrapping an optional value