Hex String to Character in PURE Swift

前端 未结 4 1287
挽巷
挽巷 2021-01-05 02:06

I need a way to convert a string that contains a literal string representing a hexadecimal value into a Character corresponding to that particular hexadecimal value.

4条回答
  •  有刺的猬
    2021-01-05 03:08

    Another simple way based on ICU transforms:

    extension String {
      func transformingFromHex() -> String? {
        return "&#x\(self);".applyingTransform(.toXMLHex, reverse: true)
      }
    }
    

    Usage:

    "2C".transformingFromHex()
    

    Results in: ,

提交回复
热议问题