4

Equivalent of Android's Base 64 NO_WRAP option in swift's Base 64?

In android: hash = Base64.encodeToString(message.getBytes()), Base64.NO_WRAP);

See NO_WRAP option below: https://developer.android.com/reference/android/util/Base64.html

What's the equivalent in swift for iOS to convert a HEX String to Base 64 with the NO_WRAP option?

user3427013
  • 1,039
  • 1
  • 13
  • 28

1 Answers1

3

It's in the doc:

So just specify no option at all when encoding to avoid adding any line ending character, and specify ignoreUnknownCharacters when decoding to ignore line ending characters.

Step 1: Hex String to Data is a separate StackOverflow question (but you should avoid Hex String to begin with, it's a big waste of bytes!)

Step 2: Data to base64 Data or base64 String (choose one)

// By default, no line endings are inserted: https://developer.apple.com/reference/foundation/nsdata/1412739-base64encodeddata
let encodedAsData = data.base64EncodedData()

// By default, no line endings are inserted: https://developer.apple.com/reference/foundation/nsdata/1413546-base64encodedstring
let encodedAsString = data.base64EncodedString()
Community
  • 1
  • 1
Cœur
  • 37,241
  • 25
  • 195
  • 267