美文网首页
clojure char int byte 互转

clojure char int byte 互转

作者: onedam | 来源:发表于2021-08-14 18:18 被阅读0次
     (.codePointAt (char \好)) ; 不能这样取. 因为 int char 就可以转化了.
     (.codePointAt "好" 0) ; 不能这样取. 因为 int char 就可以转化了.
     (Integer/toHexString (int \好)) ;597d
     (= \u597d \好)
     (map #(Integer/toHexString %) (intToByteArray (int \好))) ;("0" "0" "59" "7d")
    
    (defn intToByteArray [a]
     (byte-array [
                  (byte (bit-and (bit-shift-right a 24) 0xff))
                  (byte (bit-and (bit-shift-right a 16) 0xff))
                  (byte (bit-and (bit-shift-right a 8) 0xff))
                  (byte (bit-and a 0xff))
                  ])
     )
    
     (java.nio.charset.Charset/defaultCharset) ;gbk
     (System/getProperty "file.encoding") ;gbk
     (String. (byte-array [(byte 0x43)
                           (byte 0x6c)
                           (byte 0x6f)
                           (byte 0x6a)
                           12
                           32
                           61
                           ;129
                           0x59 0x7d  ;这是好的unicode编码
                           186 195    ;这是"好"的gbk 编码
                           ;(Integer/toBinaryString 186)
                           ;(Integer/toBinaryString 195)
                           2r10111010 2r11000011
                           ]) "gbk")  ;utf8 会乱码
     (String. (byte-array [(byte 0x43)
                           (byte 0x6c)
                           (byte 0x6f)
                           (byte 0x6a)
                           12
                           32
                           61
                           ;129
                           0x59 0x7d  ;这是好的unicode编码
                           186 195    ;这是"好"的gbk 编码
                           ;(Integer/toBinaryString 186)
                           ;(Integer/toBinaryString 195)
                           2r10111010 2r11000011
                           229 165 189
                           ]) "utf8")
     (map #(bit-and % 0xff) (.getBytes "好")) ;(186 195)
     (map #(bit-and % 0xff) (.getBytes "好" "utf-8")) ;(229 165 189)
    

    相关文章

      网友评论

          本文标题:clojure char int byte 互转

          本文链接:https://www.haomeiwen.com/subject/yoyebltx.html