How to convert UInt8 byte array to string in Swift

前端 未结 14 2525
野的像风
野的像风 2020-12-05 06:29

I am facing problems while converting UInt8 Byte array to string in swift. I have searched and find a simple solution

String.stringWithBytes(buf         


        
相关标签:
14条回答
  • 2020-12-05 06:51

    Update for Swift 3/Xcode 8:

    String from bytes: [UInt8]:

    if let string = String(bytes: bytes, encoding: .utf8) {
        print(string)
    } else {
        print("not a valid UTF-8 sequence")
    }
    

    String from data: Data:

    let data: Data = ...
    if let string = String(data: data, encoding: .utf8) {
        print(string)
    } else {
        print("not a valid UTF-8 sequence")
    }
    

    Update for Swift 2/Xcode 7:

    String from bytes: [UInt8]:

    if let string = String(bytes: bytes, encoding: NSUTF8StringEncoding) {
        print(string)
    } else {
        print("not a valid UTF-8 sequence")
    }
    

    String from data: NSData:

    let data: NSData = ...
    if let str = String(data: data, encoding: NSUTF8StringEncoding) {
        print(str)
    } else {
        print("not a valid UTF-8 sequence")
    }
    

    Previous answer:

    String does not have a stringWithBytes() method. NSString has a

     NSString(bytes: , length: , encoding: )
    

    method which you could use, but you can create the string directly from NSData, without the need for an UInt8 array:

    if let str = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
        println(str)
    } else {
        println("not a valid UTF-8 sequence")
    }
    
    0 讨论(0)
  • 2020-12-05 06:52

    This worked for me:

    String(bytes: bytes, encoding: NSUTF8StringEncoding)
    
    0 讨论(0)
  • 2020-12-05 06:53

    Martin R in https://stackoverflow.com/a/29644387/2214832 answered Sunil Kumar on his problem but not on the topic question. The problem still appears if you already have UInt8 byte array and need to present it as string.

    Here is my solution:

    extension String {
        init(_ bytes: [UInt8]) {
            self.init()
            for b in bytes {
                self.append(UnicodeScalar(b))
            }
        }
    }
    

    Using this extension you can now init String with UInt8 byte array like that:

    func testStringUInt8Extension() {
        var cs : [UInt8] = []
        for char : UInt8 in 0..<255 {
            cs.append(char)
        }
        print("0..255 string looks like \(String(cs)))")
    }
    

    This is not ideal solution because practically you would need to decode something like UTF-8 encoded text. But for ASCII data this works as expected.

    0 讨论(0)
  • 2020-12-05 06:57

    Update for Swift 5.2.2:

    String(decoding: yourByteArray, as: UTF8.self)
    
    0 讨论(0)
  • 2020-12-05 06:58

    Swifty solution

    array.reduce("", combine: { $0 + String(format: "%c", $1)})
    

    Hex representation:

    array.reduce("", combine: { $0 + String(format: "%02x", $1)})
    
    0 讨论(0)
  • 2020-12-05 07:00

    This solution works.

    NSString(bytes: data!, length: data!.count, encoding: NSUTF8StringEncoding) 
    
    0 讨论(0)
提交回复
热议问题