0

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

cmnewfan
  • 129
  • 1
  • 9
  • Possible duplicate of [swift how to change GB-2312 encoding to uff-8?](http://stackoverflow.com/questions/29121906/swift-how-to-change-gb-2312-encoding-to-uff-8) – t4nhpt Oct 23 '15 at 02:46
  • i have used that solution too and i faced another question. when i read a chinese character, xcode thorows this:a fatal error: unexpected found nil while unwrapping an optional values – cmnewfan Oct 23 '15 at 05:33

0 Answers0