2

I am converting following UInt8 array to String in swift and getting nil each time.what i should do ?? please help me.

[ 201, 201, 87, 200, 204 ]
let decData = NSData(bytes: mydata, length: Int(mydata.count) )
let result1 = NSString(data: decData, encoding: NSUTF8StringEncoding)

Edited : I am using InflateStream/deflateStream to compress my data. The return type of deflateStream is UInt8. I tried to convert this value to string so that i can send it to server.

This is my code

    let string = "hello"
    let data: [UInt8] = Array(string.utf8)

    // compress
    var deflater = DeflateStream()
    var (deflated, err) = deflater.write(data, flush: true)
    if err != nil{
        fatalError("\(err!)")
    }
    print(" ***** deflater: \(deflated)")

    // decompress
    var inflater = InflateStream()
    var (inflated, errr) = inflater.write(deflated, flush: true)
    if errr != nil{
        fatalError("\(errr!)")
    }
    print(" ***** inflated: \(inflated)")
Noman Akhtar
  • 690
  • 1
  • 12
  • 17
  • The array represents characters? What output do you *expect* for `result1`? `"ÉÉWÈÌ"`? – nhgrif May 04 '16 at 12:46
  • may be I'm not sure because this array i m getting after compression.any ways how you got this can you please show me ? – Noman Akhtar May 04 '16 at 12:54
  • The code is in my answer below. Per an ASCII chart I just looked at, that's the correct string given the presumably ASCII values you provided. – nhgrif May 04 '16 at 13:28
  • 2
    What does the data represent? It is not a valid UTF-8 sequence. Perhaps some ISO-8859 encoding? Unicode code points? ... ? Without that information (or knowing the expected result) we can only *guess*. – Martin R May 04 '16 at 13:32
  • @NomanAkhtar: Where does DeflateStream come from? I tried it with https://github.com/tidwall/DeflateSwift (which has to be updated for Swift 2). Then with your updated example, `inflated` becomes `[104, 101, 108, 108, 111]` and that *is* a valid UTF-8 sequence, and can be converted to the string "Hello" as in your first code snippet. Please post a (minimal) example demonstrating the *problem*. – Martin R May 04 '16 at 14:27
  • Yes brother I got deflatorStream/inflatorStream from https://github.com/tidwall/DeflateSwift .but after deflating "hello" I m getting array of UInt8.when I m inflating it I m again getting the same "hello" .but the problem is that after deflating I have to convert the result into string so that I can send it to server.the I have already written logic to handle string I don't want to modify code at server – Noman Akhtar May 04 '16 at 15:48
  • You cannot interpret arbitrary binary data as a UTF-8 string. What format/encoding does the server expect? Perhaps you want to use Base64? – Martin R May 04 '16 at 16:18
  • @Martin R: No brother, I am wanting iso-8859-1 format at server side .how can I get? – Noman Akhtar May 05 '16 at 05:21
  • Then this might be what you are looking for: http://stackoverflow.com/a/34497065/1187415 – Martin R May 05 '16 at 05:50
  • Could you resolve the problem? I have the same issue – Carolina Sep 01 '16 at 17:44

1 Answers1

6

Given an array of UInt8:

let letters: [UInt8] = [201, 201, 87, 200, 204]

We can start by mapping this into an array of Character:

let characters = letters.map { Character(UnicodeScalar($0)) }

And then we just call the String constructor that expects [Character]:

let result = String(Array(characters))
nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • here when i m again getting UInt8 using following code the output is comming different [ let data: [UInt8] = Array(result.utf8) print(data)] – Noman Akhtar May 04 '16 at 13:41
  • @NomanAkhtar: Of course, because `201, 201, 87, 200, 204` is *not* a valid UTF-8 sequence. Please let us know where the data comes from, what it represents, and what result you expect. – Martin R May 04 '16 at 13:42
  • i m using deflator/inflater to compress data after compression i got this data i have to send this data in the form of string – Noman Akhtar May 04 '16 at 13:52
  • let string = "hello" let data: [UInt8] = Array(string.utf8) // compress var deflater = DeflateStream() var (deflated, err) = deflater.write(data, flush: true) if err != nil{ fatalError("\(err!)") } print(" ***** deflater: \(deflated)") // decompress var inflater = InflateStream() var (inflated, errr) = inflater.write(deflated, flush: true) if errr != nil{ fatalError("\(errr!)") } print(" ***** inflated: \(inflated)") – Noman Akhtar May 04 '16 at 13:52
  • @NomanAkhtar Please add all relevant information to your question, not as a comment. – Martin R May 04 '16 at 13:55
  • 2
    This is a quite hypothetical solution, and also not applicable: mapping an unsigned eight bit integer to an Unicode Scalar should be a valid unicode value that can be represented as a single UInt8. The _given_ values - except `87` - are certainly not valid UTF-8 start bytes. – CouchDeveloper May 04 '16 at 14:48