1

I have String with 44 char length and i want to convert it to 32 char length in iOS Swift 3.1

let keyString = "u6KuXJLIUwEUl7noY8J8H1ffDRwLC/5gjaWW1qTQ3hE="

i use this code for convert it to Data with 32 bytes :

let keyData = Data(base64Encoded: keyString)

and now , how to convert it to string with 32 char length?

reza_khalafi
  • 6,230
  • 7
  • 56
  • 82
  • 1
    What should that string be if the bytes are (for example) `<00010203 00000000 ... FFFEFDFC>` ? Interpreting a binary blob as a string (usually) makes no sense. Why do you think that a 32 character string is needed? – Martin R May 09 '17 at 08:43
  • For using in laravel encryption via swift code @MartinR – reza_khalafi May 09 '17 at 08:53
  • 1
    Again: How should `<00010203 00000000 ... FFFEFDFC>` be represented as a 32 character string? Or in your concrete case `` ? – Martin R May 09 '17 at 08:56
  • just see this in php $key = "u6KuXJLIUwEUl7noY8J8H1ffDRwLC/5gjaWW1qTQ3hE="; $key = (string)base64_decode($key); @MartinR – reza_khalafi May 09 '17 at 09:09
  • What you want seems to be "http://stackoverflow.com/questions/39075043/how-to-convert-data-to-hex-string-in-swift". A representation of the Data into HexString. – Larme May 09 '17 at 09:17
  • 1
    @Larme: That would give 64 characters for 32 bytes :) – Martin R May 09 '17 at 09:19

1 Answers1

1
String(data: keyData, encoding: .utf8)

gives you an optional string. Since you know the source data does in fact contain UTF-8/ASCII data, you can safely unwrap it.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Base64 decoding the string `"u6KuXJLIUwEUl7noY8J8H1ffDRwLC/5gjaWW1qTQ3hE="` from the question gives `` which is *not* valid UTF-8. – Martin R May 09 '17 at 08:54