UTF-8和UTF-16编码方式相互转换的公式及步骤

2013年12月09日 其他资源 13条评论 阅读9515次

UTF-8和UTF-16编码方式相互转换的公式及步骤

 

不使用英文语言的地区,为使其语言文字正确显示到计算机等终端上,他的字符的编码就要单独处理了。

utf-8作为这种背景下的产物,因其编码范围等因素的制约,不得不将下一代编码方式utf-16推出。

utf-8和utf-16的编码相互转化就成为我们要掌握的重点了。以下就是正文内容:建议详细阅读。

 

UTF-16 编码程序

 

假设要将 U+64321 (16进位) 转成 UTF-16 编码. 因为它超过 U+FFFF, 所以他必须编译成32位(4个byte)的格式,如下所示

 

V  = 0x64321

Vx = V - 0x10000

   = 0x54321

   = 0101 0100 0011 0010 0001

 

Vh = 01 0101 0000 // Vx 的高位部份的 10 bits

Vl = 11 0010 0001 // Vx 的低位部份的 10 bits

w1 = 0xD800 //結果的前16位元初始值

w2 = 0xDC00 //結果的後16位元初始值

 

w1 = w1 | Vh

   = 1101 1000 0000 0000

   |        01 0101 0000

   = 1101 1001 0101 0000

   = 0xD950

 

w2 = w2 | Vl

   = 1101 1100 0000 0000

   |        11 0010 0001

   = 1101 1111 0010 0001

   = 0xDF21

 

所以这个字 U+64321 最后正确的 UTF-16 编码应该是:

 

0xD950 0xDF21

 

而在小尾序中最后的编码应该是:

 

0x50D9 0x21DF

 

假设要处理的UNICODE编码为X,来推一下位运算公式

 

第一步:X - 0x10000  得到结果为Y

 

第二步:取Y的高十位:Y >> 10   得到结果为Vh

 

第三步:取Y的低十位:Y  & 3FF(11 1111 1111) 这样就可以把十位前的全部清零了。 得到结果为Vl

 

第四步:将高位与前16位初始元0xD800 进行或操作 Vh | 0xD800  得到前半部  记为Uf

 

第五步:将低位与后16位初始元0xDC00进行或操作Vl |0xDC00 得到后半部份。Ue

 

第六步:将前后两部份合在一起。先左移十六位 (Uf << 16) 再或上Ue 即 (Uf << 16) | Ue

 

 

把这个写为宏的形式:

 

#define UNICODETOUTF16(x)  ((Uf << 16) | Ue) 将这个公式向前代替,最终得到

 

#define UNICODETOUTF16(x) (((((x - 0x10000) >>10) | 0xD800) << 16)  | (((x-0x10000)&3FF) | 0xDC00))        

 

同样使用UTF16转回为大于0x10000 的Unicode码;

 

#define MULITTHREEBYTEUTF16TOUNICODE(x,y) (((((x ^ 0xD800) << 2) | ((y ^ 0xDC00) >> 8)) << 8) | ((y ^ 0xDC00) & 0xFF)) + 0x10000 

 

本文总结整理来自多篇博客、论坛等资料。具体就不一一列出,若有侵权,请及时联系。

分享本文至:

WRITTEN BY

avatar
本文标签:UTF-8UTF-16
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

13 条评论

  1. <a href="http://www.helpforaspen.com/nike-kobe-6-grey-pink-trainersa">nike kobe 6 grey pink</a> <a href="http://www.mcmartial.com/nike-zoom-kd-5-ice-bl%C3%A5-scarped">nike zoom kd 5 ice bl氓</a> <a href="http://www.puurderleven.com/huarache-ultra-gituttio-tint-kurpesc">huarache ultra gituttio tint</a> <a href="http://www.ipartybooths.com/ray-ban-rb2132-sunglass-brown-sunglassesf">ray ban rb2132 sunglass brown</a> <a href="http://www.senegalreview.com/mens-nike-new-orleans-saints-11-tommylee-lewis-limited-black-2016-salute-to-service-nfl-jersey-nflr">mens nike new orleans saints 11 tommylee lewis limited black 2016 salute to service nfl jersey</a> <a href="http://www.poshandpearls.com/air-jordan-ultra-fly-up-goals-skoa">air jordan ultra fly up goals</a>
    medieneinsatz http://www.medieneinsatz.com/

  2. <a href="http://www.praiseti.com/kinder-nike-light-up-schuhe-grau-schuheb">kinder nike light up schuhe grau</a> <a href="http://www.cynshq.com/nike-super-bowl-lii-womens-john-hannah-limited-olivecamo-jersey-new-england-patriots-nfl-73-2017-salute-to-service-nflu">nike super bowl lii womens john hannah limited olivecamo jersey new england patriots nfl 73 2017 salute to service</a> <a href="http://www.fnjcompressor.com/nike-air-max-180-force-skou">nike air max 180 force</a> <a href="http://www.kifftradingco.com/black-blue-mens-nike-free-run-2.0-shoes-runninge">black blue mens nike free run 2.0 shoes</a> <a href="http://www.phaqtory.com/silber-blau-nike-air-zoom-pegasus-35-zapatosu">silber blau nike air zoom pegasus 35</a> <a href="http://www.cialislaw.com/yeezy-550-boost-schuhe-dam%C3%A4nner-chaussuresa">yeezy 550 boost schuhe dam盲nner</a>
    cosmicupcake http://www.cosmicupcake.com/

  3. avatar charleyscafe

    <a href="http://www.karotorossian.com/nike-zoom-hyperfuse-low-lyser%C3%B8d-butyf">nike zoom hyperfuse low lyser酶d</a> <a href="http://www.newworkmatrix.com/air-jordan-future-oransje-mint-gr%C3%B8nn-obuvf">air jordan future oransje mint gr酶nn</a> <a href="http://www.rochecpa.com/air-jordan-14-noir-desert-rose-camo-obuve">air jordan 14 noir desert rose camo</a> <a href="http://www.thermilax.com/nike-lebron-14-blanco-zinfandel-kurpesg">nike lebron 14 blanco zinfandel</a> <a href="http://www.buffaloquaker.com/mujeres-air-max-90-em-naranja-rosado-obuvc">mujeres air max 90 em naranja rosado</a> <a href="http://www.etnoyamal.com/nike-free-3.0-flyknit-svart-l%C3%B8ping-sko-nike-butya">nike free 3.0 flyknit svart l酶ping sko nike</a>
    charleyscafe http://www.charleyscafe.com/

  4. avatar subzion

    <a href="http://www.ionsrus.com/moncler-coat-khloe-white-lace-monclerg">moncler coat khloe white lace</a> <a href="http://www.orgalex.com/bon-march%C3%A9-adidas-eqt-support-hommes-rouge-sportsb">bon march茅 adidas eqt support hommes rouge</a> <a href="http://www.subzion.com/jordan-8-ovo-roshes-trainersa">jordan 8 ovo roshes</a> <a href="http://www.epetaph.com/nike-zoom-all-out-flyknit-black-purple-sneakere">nike zoom all out flyknit black purple</a> <a href="http://www.eshoplv.com/pittsburgh-pirates-nike-dri-fit-hat-key-hatg">pittsburgh pirates nike dri fit hat key</a> <a href="http://www.irasell.com/kansas-city-royals-digital-camo-hat-outfits-hatc">kansas city royals digital camo hat outfits</a>
    subzion http://www.subzion.com/

  5. avatar helpforaspen

    <a href="http://www.ntygadget.com/nike-roshe-one-flyknit-sizing-skob">nike roshe one flyknit sizing</a> <a href="http://www.purchasetulsa.com/nhl-knit-hats-with-pom-quilt-hatc">nhl knit hats with pom quilt</a> <a href="http://www.pennywave.com/a-buon-mercato-nike-free-2014-maschio-arancia-runninge">a buon mercato nike free 2014 maschio arancia</a> <a href="http://www.scoptix.com/hombres-reebok-realflex-naranja-schuhea">hombres reebok realflex naranja</a> <a href="http://www.domodrzwi.com/jordan-hydro-2-slides-schwarz-and-wei%C3%9F-scarpec">jordan hydro 2 slides schwarz and wei脽</a> <a href="http://www.magneticduo.com/womens-nike-air-max-bw-white-netherlands-shoeso">womens nike air max bw white netherlands</a>
    helpforaspen http://www.helpforaspen.com/

  6. avatar rpnailsalon

    <a href="http://www.evolutionecig.com/jordan-11-legend-bl%C3%A5-baby-sko-kurpesc">jordan 11 legend bl氓 baby sko</a> <a href="http://www.georginabauer.com/oakley-prescription-sunglasses-austin-tx-sunglassese">oakley prescription sunglasses austin tx</a> <a href="http://www.bootsoffprice.com/padres-8-erick-aybar-brown-new-cool-base-stitched-mlb-jersey-nflr">padres 8 erick aybar brown new cool base stitched mlb jersey</a> <a href="http://www.theprintjobs.com/nike-kobe-10-se-what-the-skoa">nike kobe 10 se what the</a> <a href="http://www.dbajournal.com/kyrie-1-white-red-grey-sneakerf">kyrie 1 white red grey</a> <a href="http://www.procomedian.com/air-jordan-1-retro-low-og-premium-sort-kurpesa">air jordan 1 retro low og premium sort</a>
    [url=http://www.rpnailsalon.com/]rpnailsalon[/url]

  7. avatar dbajournal

    <a href="http://www.myfraimz.com/air-max-thea-coral-silver-sneakerb">air max thea coral silver</a> <a href="http://www.winnercells.com/nike-zoom-ascention-mens-white-grey-shoesf">nike zoom ascention mens white grey</a> <a href="http://www.atppest.com/hawaii-hurley-hat-hats">hawaii hurley hat</a> <a href="http://www.windriftbar.com/air-jordan-11-gold-jumpman-instrumental-sneakere">air jordan 11 gold jumpman instrumental</a> <a href="http://www.kidtokidboise.com/jordan-11-low-hvit-s%C3%B8lv-obuvd">jordan 11 low hvit s酶lv</a> <a href="http://www.mesmertronic.com/nike-ash-womens-brett-maher-name-number-logo-slim-fit-nfl-2-dallas-cowboys-long-sleeve-t-shirt-nflx">nike ash womens brett maher name number logo slim fit nfl 2 dallas cowboys long sleeve t shirt</a>
    dbajournal http://www.dbajournal.com/

  8. avatar 津电动卷帘门

    这个步骤还是挺好的

  9. avatar bet365娱乐城

    看过了··还真不是太晓得

  10. avatar 预应力钢绞线

    这个公式可以好好学习的哈

  11. avatar 行业报告分析

    嗯嗯,看过了~

  12. avatar 梦居家

    看不太懂  不是很明白

  13. avatar 按摩椅

    看起来很不错哦

欢迎留言




用户登录

sitemap