Question: I want to convert following hex-encoded `String` in Swift 3:

```
dcb04a9e103a5cd8b53763051cef09bc66abe029fdebae5e1d417e2ffc2a07a4

```

to its equivalant `String`:

```
Ü°J:\Øµ7cï  ¼f«à)ýë®^A~/ü*¤

```

Following websites do the job very fine:

<http://codebeautify.org/hex-string-converter>

<http://string-functions.com/hex-string.aspx>

But I am unable to do the same in Swift 3. Following code doesn't do the job too:

```
func convertHexStringToNormalString(hexString:String)->String!{
  if let data = hexString.data(using: .utf8){
      return String.init(data:data, encoding: .utf8)
  }else{ return nil}
}

```

Answer: 
You want to use the hex encoded data as an AES key, but the
data is not a valid UTF-8 sequence. You *could* interpret
it as a string in ISO Latin encoding, but the `AES(key: String, ...)`
initializer converts the string back to its UTF-8 representation,
i.e. you'll get different key data from what you started with.

Therefore you should not convert it to a string at all. Use the 

```
extension Data {
    init?(fromHexEncodedString string: String)
}

```

method from [hex/binary string conversion in Swift](https://stackoverflow.com/questions/40276322/hex-binary-string-conversion-in-swift/40278391#40278391)
to convert the hex encoded string to `Data` and then pass that
as an array to the `AES(key: Array<UInt8>, ...)` initializer:

```
let hexkey = "dcb04a9e103a5cd8b53763051cef09bc66abe029fdebae5e1d417e2ffc2a07a4"
let key = Array(Data(fromHexEncodedString: hexkey)!)

let encrypted = try AES(key: key, ....)

```
There is still a way to convert the key from hex to readable string by adding the below extension:

```
extension String {

    func hexToString()->String{

        var finalString = ""
        let chars = Array(self)

        for count in stride(from: 0, to: chars.count - 1, by: 2){
            let firstDigit =  Int.init("\(chars[count])", radix: 16) ?? 0
            let lastDigit = Int.init("\(chars[count + 1])", radix: 16) ?? 0
            let decimal = firstDigit * 16 + lastDigit
            let decimalString = String(format: "%c", decimal) as String
            finalString.append(Character.init(decimalString))
        }
        return finalString

    }

    func base64Decoded() -> String? {
        guard let data = Data(base64Encoded: self) else { return nil }
        return String(data: data, encoding: .init(rawValue: 0))
    }
}

```

Example of use:

```
    let hexToString = secretKey.hexToString()
    let base64ReadableKey = hexToString.base64Decoded() ?? ""

```