跳到主要內容

[IOS] 中文字串比大小

    //程式碼
    func compartStringByUnicode(string1 string1: String, string2: String, asc: Bool, index: Int)->Bool{
        MyVariables.logger.debug("===============> index=\(index), asc=\(asc)")
        let string1LengthEnough = string1.length > index
        let string2LengthEnough = string2.length > index
        
        if string1LengthEnough && string2LengthEnough {
            let targetChar1 = string1.characters.map { String($0) }[index]
            let targetChar2 = string2.characters.map { String($0) }[index]
            
            for u1 in targetChar1.utf16 {
                for u2 in targetChar2.utf16 {
                    if u1 == u2 {
                        return compartStringByUnicode(string1: string1, string2: string2, asc: asc, index: (index+1))
                    }else{
                        MyVariables.logger.debug("\(targetChar1) (\(u1), \(targetChar2) (\(u2)) ==> \(u1 < u2)")
                        if asc {
                            return u1 < u2
                        }else {
                            return u1 > u2
                        }
                    }
                }
            }
            MyVariables.logger.debug("exception")
            return false
        }else{
            if string1LengthEnough && !string2LengthEnough {
                if asc {
                    MyVariables.logger.debug("string2不夠長 ==> false")
                    return false
                }else {
                    MyVariables.logger.debug("string2不夠長 ==> true")
                    return true
                }
            }else{
                if asc {
                    MyVariables.logger.debug("string1不夠長 ==> true")
                    return true
                }else {
                    MyVariables.logger.debug("string1不夠長 ==> false")
                    return false
                }
            }
        }
    }

參考︰
http://jimmydeveloper.blogspot.tw/2015/01/swift.html
Unicode Lookup UTF-8 Tool
NSString 與 Unicode
[C/C++] Unicode 與 UTF-8 互轉(C 語言實現)
unicode, ucs2, ucs4,utf-8,utf-16,utf-32關係和區別?

unicode是編碼標準,每個字母,每個漢字對應著一個碼。 比如中文的「我」,對應的unicode碼是6211。

unicode要實現,要存在文件系統中,需要對unicode碼進行存儲。

那麼存在文件系統中是存在62和11,還是11和62,還是其他形式,這個就是ucs2/ucs4/utf8/utf16/utf32要做的事情了。 他就是把unicode碼轉為文件系統中(內存中)存儲的編碼,可以認為是對unicode的實現方式。

由於unicode標準話有兩個組織,雖然它們對unicode編碼是一樣的(就是同一字母和漢字對應的編碼是一樣的,即,中文的「我」,對應的都是6211),但是存儲上有所區別。

這裡ucs2和utf16, ucs4和utf32是一樣的,只是不同組織對同一實現方式的不同名稱。

然後就剩下utf8/utf16/utf32的區別了。

utf8是變長編碼,顯然英文字母很少,它佔用兩個字節來代表一個字母實在是太浪費資源了,所以呢就可以用一個字節表示一個字母,用多個字節表示一個漢字。其中漢字的「我」,utf8的編碼是e6 88 91。

utf16和ucs2是一樣的,它用兩個字節代表一個字母或者漢字。它還涉及到small endian和big endian。

utf32和ucs4是一樣的,由於漢字博大精深,兩個字節都不可以裝下所有的漢字,於是就出現了4字節的編碼,也稱surrogate

How to convert an Int to Hex String in Swift
Hex to Decimal - Swift

// 我的unicode: U+6211
print("\u{6211}") // 我

// Hex to Decimal
UInt16(strtoul("6211", nil, 16)) // 25105
// 算法︰16^3*6 + 16^2*2 + 16^1*1 + 16^0*1 = 25105

// Decimal to Hex
NSString(format:"%X", 25105) // 6211
String(25105, radix: 16) // 6211
String(format:"%02X", 25105) // 6211

let myChar = "我"
for c in myChar.utf16 {
    print("\(c)") // 25105
}

留言

這個網誌中的熱門文章

[Swift] String Format for NT$

  var amount = NSNumber(int: 8880) var _currencyFormatter = NSNumberFormatter() _currencyFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle //_currencyFormatter.currencyCode = "NT$ " //_currencyFormatter.formatterBehavior = NSNumberFormatterBehavior.BehaviorDefault //_currencyFormatter.negativeFormat = "-$#,##0" _currencyFormatter.positiveFormat = "NT$ #,##0" _currencyFormatter.stringFromNumber(amount)   輸出結果︰ NT$ 8,880   參考︰ http://www.cokco.cn/thread-35548-1-1.html http://www.freair.com/bbs/simple/?t558.html

小米路由器 mini 刷機

這次會來刷機是因為要給 MOD 使用 但原本的 小米路由器 mini 沒支援 IPTV 不想再買一台路由器(已經有四台了XD,有3台是被淘汰的)   參考文章︰ 1.  ERIC的攝影世界 - 小米路由器mini 免USB打開SSH  ( 備份檔案 ) 2.  ERIC的攝影世界 - 小米路由器mini韌體刷openwrt PandoraBox韌體  ( 備份檔案 ) 3.  寫寫東西 分享心得 - 小米路由器mini 改韌體 可看中華電信MOD iptv功能開啟 刷機 刷rom 直通MOD 老毛子Padavan固件 華碩 RT-AC54U  ( 備份檔案 )   其實找了很多篇文章,大部份都教用官方的那招,但不知道為什麼我就是取不到SSH的密碼 https://d.miwifi.com/rom/ssh  這個一直導不到要的那頁… 所以後來是用了  ERIC的攝影世界 - 小米路由器mini 免USB打開SSH  的第二招  免USB打開SSH 步驟︰ 1. 手動更新韌體到小米路由器mini 穩定版 2.8.14 ( 備用檔案 ) 2. 然後按  ERIC的攝影世界 - 小米路由器mini 免USB打開SSH  的 1.2.3.4.5   2.1 登入  小米路由器mini控制台  後複制網址     http://192.168.31.1/cgi-bin/luci/;stok=521b849e00a11c5b6743aa275ba84ed8 /web/home#router   2.2 將下面的網址紅色部份換成自己的,然後再貼到Browser上     http://192.168.31.1/cgi-bin/luci/;stok=521b849e00a11c5b6743aa275ba84ed8 /api/xqsystem/set_name_password?oldPwd=路由器控制台管理密碼&newPwd=admin     順利的會得到  {"code":0}   2.3 將下面的網址紅色部份換成自己的,然後再貼到Browser上     http://192.168.31.1/cgi-bin/luci/;stok=521b849e00a11c5b6743aa275ba84ed8 /api/x

[PS4 VR] 影片轉檔

怎麼用索尼PSVR看安卓VR 360° 3D視頻 => 將雙眼視頻改成單眼視頻,將格式改成MPEG-4,解析度1920*1080 ,60幀 15000kbps https://kknews.cc/tech/vme9a5l.html ============================================================== Wondershare Filmora:  8.0.0 for Mac 破解版下載 http://www.iphoneba.net/2563.html v8.2.0 http://www.zhinin.com/wondershare_filmora-mac.html ============================================================== for Littlstar http://docs.littlstar.com/plugins/servlet/mobile?contentId=2262863#content/view/2262863 Video Requirements H.264 High@5.1 or lower Max pixel width: 2560 Max frame rate: 60fps (最好是29.97) YUV420/NV12 color space 20Mbps bit rate or lower Audio Requirements Stereo Audio only AAC (Advanced Audio Coding) 320Kbps bit rate or lower Container Requirements File extensions: MP4, MOV No fragmented MP4 No DRM 15 min max duration 以上~ Littlstar不支援平面的3D格式,只支援180或360 而PSVR的媒體播放器不支援3D,只支援360 ============================================================== Make 3D Videos For PSVR Wat