1

I am new in swift. i have this bytes:

Bytes: [11, 143, 102, 88, 132, 238, 0, 156, 100, 166, 72, 98, 226, 109, 51, 196, 124, 124, 207, 252, 204, 129, 233, 209, 112, 127, 66, 177, 37, 141, 169, 158, 122, 74, 215, 103, 13, 128, 74, 81, 221, 46, 219, 145, 107, 131, 90, 246, 37, 212, 91, 237, 32, 138, 74, 147, 238, 40, 182, 158, 12, 124, 197, 17, 92, 24, 184, 44, 150, 127, 147, 161, 175, 186, 227, 4, 248, 44, 21, 83, 0]

and i used this code:

let dencryptedBytes: [UInt8] = try! AES(key: UrlManager.CONNECTION_KEY, iv:UrlManager.CONNECTION_IV, blockMode: .CBC).encrypt(bytes)

and result like this:

Decoded Bytes: [157, 29, 111, 190, 188, 31, 233, 140, 152, 67, 196, 83, 214, 238, 232, 184, 101, 149, 45, 184, 155, 85, 184, 69, 155, 173, 196, 145, 123, 54, 238, 243, 34, 178, 190, 129, 106, 11, 26, 147, 19, 207, 204, 162, 142, 81, 6, 24, 21, 93, 80, 134, 247, 151, 83, 79, 214, 134, 80, 222, 10, 196, 64, 247, 53, 194, 195, 207, 230, 79, 215, 134, 87, 32, 37, 100, 82, 125, 59, 41, 235, 36, 144, 171, 64, 247, 195, 12, 115, 194, 124, 243, 109, 84, 44, 155]

but i need convert this bytes to string. i can not find answer. please advise me. Thanks a lot !

Edit:

I found this:

var bytes: [UInt8] = [157, 29, 111, 190, 188, 31, 233, 140, 152, 67, 196, 83, 214, 238, 232, 184, 101, 149, 45, 184, 155, 85, 184, 69, 155, 173, 196, 145, 123, 54, 238, 243, 34, 178, 190, 129, 106, 11, 26, 147, 19, 207, 204, 162, 142, 81, 6, 24, 21, 93, 80, 134, 247, 151, 83, 79, 214, 134, 80, 222, 10, 196, 64, 247, 53, 194, 195, 207, 230, 79, 215, 134, 87, 32, 37, 100, 82, 125, 59, 41, 235, 36, 144, 171, 64, 247, 195, 12, 115, 194, 124, 243, 109, 84, 44, 155]


    print(bytes2String(bytes))

func bytes2String(array:[UInt8]) -> String {
return NSString(data: NSData(bytes: array, length: array.count), encoding: NSUTF8StringEncoding)! as String

}

link: NSData to String in Swift Issues

but it don't work Why is it ?

Error: fatal error: unexpectedly found nil while unwrapping an Optional value

8
  • Your bytes array contain plenty of unprintable characters. How do you want to deal with them? Commented Nov 7, 2015 at 4:33
  • @Zoff Dino, if convert this bytes to string, it will be string like json Commented Nov 7, 2015 at 4:45
  • What character does 29 map to? Can you show your expected output? Commented Nov 7, 2015 at 4:47
  • i must get like this result: {"res":"connect","hash":"4930022e2d524cc3bdef0a7706468251","state":"complete"} Commented Nov 7, 2015 at 5:00
  • What encoding scheme is this? The first double quote is 29, but the second is 31, the third one is 140? Also, your array has 96 bytes but your expected string has only 78 characters. Commented Nov 7, 2015 at 5:20

2 Answers 2

2

try this

(str=String(bytes: d, encoding: NSUTF8StringEncoding))

Sign up to request clarification or add additional context in comments.

1 Comment

where is this defined?
1

if the input data doesn't represent utf8 encoding string NSString constructor failed and return nil. if you unwrap nil value, the result is runtime error.

NSString(data: NSData(bytes: array, length: array.count), encoding: NSUTF8StringEncoding)!

it means exactly, that array bytes: [UInt8] is not representable as uft8 string. if i understand, you play with some crypto framework. are you sure that the framework does correct encryption, decryption? make some test first ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.