c - how to convert wchar_t to string in swift -
i'd wchar_t array c library, , convert swift data structure, code here:
func getresults(recognizer: unsafemutablepointer<void>, stroke: unsafemutablepointer<int32>, touchstate: int32) -> [string] { var buflen : int32 var buf = unsafemutablepointer<wchar_t>.alloc(int(buflen)) getrecognition(recognizer, stroke, touchstate, buf, buflen) var results = string.fromcstring(buf)! //this line has error, cause buf wchar_t*, not char* } how convert buf swift data structure? know if buf unsafemutablepointer<int8>.alloc(int(buflen)), can use string.fromcstring(buf)! convert it.
if println(buf[0]), it's print integer 67, ascii value of 'c', how can println(buf[0]) 'c' instead of 0? thanks!
wchar_t alias int32 , contains utf-32 code point in host byte order (which little-endian on current ios , os x platforms).
therefore can convert buffer swift string follows:
if let str = nsstring(bytes: unsafepointer(buf), length: wcslen(buf) * sizeof(wchar_t), encoding: nsutf32littleendianstringencoding) as? string { println(str) } else { // encoding problem ... } (this assumes wchar_t string c library function zero-terminated.)
Comments
Post a Comment