Skip to content

Instantly share code, notes, and snippets.

@yossan
Last active October 18, 2017 05:30
Show Gist options
  • Save yossan/efa230df61c10012e530e937dc4bd894 to your computer and use it in GitHub Desktop.
Save yossan/efa230df61c10012e530e937dc4bd894 to your computer and use it in GitHub Desktop.
converts string to little endian in swift
import Foundation
let str = "βœ‘πŸπŸŽβœ’"
// returns little endian
let data = str.withCString(encodedAs: UTF16.self) { (lead) -> Data in
let ret = UnsafeBufferPointer(start: lead, count: str.utf16.count)
let data = Data(buffer: ret)
return data
}
let decode = String(data: data, encoding: .utf16LittleEndian)!
print(decode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment