Skip to content

Instantly share code, notes, and snippets.

@ElonPark
Last active May 3, 2019 07:16
Show Gist options
  • Save ElonPark/9dae8cc9234990871bf3b919546dc169 to your computer and use it in GitHub Desktop.
Save ElonPark/9dae8cc9234990871bf3b919546dc169 to your computer and use it in GitHub Desktop.
func euckrEncoding(_ query: String) -> String { //EUC-KR 인코딩
let rawEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.EUC_KR.rawValue))
let encoding = String.Encoding(rawValue: rawEncoding)
let eucKRStringData = query.data(using: encoding) ?? Data()
let outputQuery = eucKRStringData.map {byte->String in
if byte >= UInt8(ascii: "A") && byte <= UInt8(ascii: "Z")
|| byte >= UInt8(ascii: "a") && byte <= UInt8(ascii: "z")
|| byte >= UInt8(ascii: "0") && byte <= UInt8(ascii: "9")
|| byte == UInt8(ascii: "_") || byte == UInt8(ascii: ".") || byte == UInt8(ascii: "-")
{
return String(Character(UnicodeScalar(UInt32(byte))!))
} else if byte == UInt8(ascii: " ") {
return "+"
} else {
return String(format: "%%%02X", byte)
}
}.joined()
return outputQuery
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment