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

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

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
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

25 条评论

  1. <a href="http://www.czappmasonry.com/lebron-12-deep-royal-metalleic-silber-schuhed">lebron 12 deep royal metalleic silber</a> <a href="http://www.hiussalipiilo.com/nike-dynamo-free-ps-ni%C3%B1os-negro-rojo-scarpea">nike dynamo free ps ni帽os negro rojo</a> <a href="http://www.olgroupusainc.com/nike-hyperdunk-hommes-basketbtout-chaussures-for-vente-queens-kurpesf">nike hyperdunk hommes basketbtout chaussures for vente queens</a> <a href="http://www.shahsmkj.com/limited-nike-blue-youth-james-bradberry-jersey-nfl-24-carolina-panthers-rush-vapor-untouchable-nflr">limited nike blue youth james bradberry jersey nfl 24 carolina panthers rush vapor untouchable</a> <a href="http://www.theuntamedco.com/jordan-6-rosa-argento-kurpesf">jordan 6 rosa argento</a> <a href="http://www.wsendeavors.com/asics-gel-kinsei-2-in-esecuzione-scarpe-maschio-nero-schuhef">asics gel kinsei 2 in esecuzione scarpe maschio nero</a>
    theartofdecay http://www.theartofdecay.com/

  2. <a href="http://www.keleric.com/nike-lunarglide-7-s%C3%B8lv-gr%C3%B8nn-obuvi">nike lunarglide 7 s酶lv gr酶nn</a> <a href="http://www.jeffreya.com/nike-shox-avenue-cielo-blu-argento-scarpej">nike shox avenue cielo blu argento</a> <a href="http://www.vuduchoan.com/mens-nike-air-max-tailwind-8-purple-white-shoesh">mens nike air max tailwind 8 purple white</a> <a href="http://www.gmfamilylaw.com/nike-kd-7-hombres-rojo-zapatosp">nike kd 7 hombres rojo</a> <a href="http://www.clubephytus.com/mujeres-asics-gel-kayano-21-naranja-blanco-zapatosk">mujeres asics gel kayano 21 naranja blanco</a> <a href="http://www.findnewcoupon.com/womens-denali-north-face-jackets-northfaceh">womens denali north face jackets</a>
    erstube http://www.erstube.com/

  3. <a href="http://www.ngprieska.com/slim-fit-long-sleeve-polo-shirts-for-m%C3%A6nd-polo_m">slim fit long sleeve polo shirts for m忙nd</a> <a href="http://www.planitcpm.com/nfl-jermey-parnell-mens-limited-teal-green-jersey-78-jacksonville-jaguars-nike-rush-drift-fashion-nflt">nfl jermey parnell mens limited teal green jersey 78 jacksonville jaguars nike rush drift fashion</a> <a href="http://www.veggietracks.com/orange-womens-nike-kyrie-2-trainersd">orange womens nike kyrie 2</a> <a href="http://www.fchsbulldogs.com/nike-lunarglide-7-mens-cheap-runninga">nike lunarglide 7 mens cheap</a> <a href="http://www.bodobe.com/nike-roshe-run-lyser%C3%B8d-kickz-kurpesb">nike roshe run lyser酶d kickz</a> <a href="http://www.chrisascut.com/oakley-oil-drum-sunglasses-sunglassesr">oakley oil drum sunglasses</a>
    angkorimage http://www.angkorimage.com/

  4. <a href="http://www.rexpousa.com/nike-zoom-vomero-10-bianca-scarpei">nike zoom vomero 10 bianca</a> <a href="http://www.dalvadesign.com/adidas-porsche-design-bounce-s4-cuero-blanco-negro-zapatosh">adidas porsche design bounce s4 cuero blanco negro</a> <a href="http://www.velvetwifi.com/adidas-gazelle-og-gr%C3%A5-hvid-skok">adidas gazelle og gr氓 hvid</a> <a href="http://www.sophiarugby.com/svart-r%C3%B8d-gr%C3%A5-nike-huarache-obuvi">svart r酶d gr氓 nike huarache</a> <a href="http://www.thumbglovepro.com/nike-air-max-tavas-negro-rojo-zapatosl">nike air max tavas negro rojo</a> <a href="http://www.ceracustica.com/ray-ban-sunglasses-rb4165-justin-sunglassesh">ray ban sunglasses rb4165 justin</a>
    cammaara http://www.cammaara.com/

  5. <a href="http://www.redlineart.net/nike-air-max-ltd-2-maschio-in-esecuzione-scarpa-obuva">nike air max ltd 2 maschio in esecuzione scarpa</a> <a href="http://www.reedrummond.net/lebron-soldier-10-all-r%C3%B8d-jordans-obuve">lebron soldier 10 all r酶d jordans</a> <a href="http://www.regulationonline.net/air-max-90-hvit-jd-sports-obuvf">air max 90 hvit jd sports</a> <a href="http://www.reporterviaggi.net/nike-air-huarache-matte-plata-aviator-schuhef">nike air huarache matte plata aviator</a> <a href="http://www.restoringthechurch.net/nike-sb-stefan-janoski-max-mujeres-negro-cielo-azul-kurpesd">nike sb stefan janoski max mujeres negro cielo azul</a> <a href="http://www.rusticlighting.net/air-max-zero-qs-silber-key-butyf">air max zero qs silber key</a>
    mydxp http://www.mydxp.net/

  6. <a href="http://www.goodmedusa.com/m%C3%A6nd-nike-air-max-tailwind-7-bl%C3%A5-orange-skoj">m忙nd nike air max tailwind 7 bl氓 orange</a> <a href="http://www.murskacorp.com/nike-flyknit-kobe-11-wei%C3%9F-schwarz-lila-schuheh">nike flyknit kobe 11 wei脽 schwarz lila</a> <a href="http://www.prajansim.com/men-adidas-nmd-r1-runner-primeknit-white-shoesk">men adidas nmd r1 runner primeknit white</a> <a href="http://www.pokersurga.com/mk-big-tote-mkh">mk big tote</a> <a href="http://www.arkmmj.com/nike-air-roshe-all-svart-obuvi">nike air roshe all svart</a> <a href="http://www.butterbroad.com/nike-flyknit-light-grigio-scarpek">nike flyknit light grigio</a>
    naturabottega http://www.naturabottega.com/

  7. <a href="http://www.htpmimoza.com/men-nike-lebron-12-christmas-green-shoesk">men nike lebron 12 christmas green</a> <a href="http://www.ayakkabiyeri.com/nike-air-max-2014-m%C3%A6nd-sapphire-orange-hvid-skol">nike air max 2014 m忙nd sapphire orange hvid</a> <a href="http://www.sozoteki.com/nike-free-flyknit-4.0-menn-bling-gr%C3%B8nn-gr%C3%A5-obuvk">nike free flyknit 4.0 menn bling gr酶nn gr氓</a> <a href="http://www.njforward.com/maschio-adidas-springblade-solyce-blu-nero-scarpei">maschio adidas springblade solyce blu nero</a> <a href="http://www.crimediainc.com/rojo-adidas-ace-16-zapatosh">rojo adidas ace 16</a> <a href="http://www.claybrookpenn.com/ladies-hvid-converse-skok">ladies hvid converse</a>
    dannyishere http://www.dannyishere.com/

  8. <a href="http://www.doctid.com/nike-lebron-barn-khaki-chaussuresc">nike lebron barn khaki</a> <a href="http://www.dhanakaryam.com/herre-nike-air-max-90-gul-r%C3%B8d-kurpesg">herre nike air max 90 gul r酶d</a> <a href="http://www.djmattconsola.com/adidas-zx-700-grau-gelb-schuhea">adidas zx 700 grau gelb</a> <a href="http://www.chinchout.com/air-jordan-super-fly-v-brun-sort-butyf">air jordan super fly v brun sort</a> <a href="http://www.mccrarycabins.com/jordan-retro-11-blanco-and-naranja-kurpesd">jordan retro 11 blanco and naranja</a> <a href="http://www.javajoeshhi.com/femminile-nike-zoom-pegasus-32-viola-rosso-butya">femminile nike zoom pegasus 32 viola rosso</a>
    aradefemalhas http://www.aradefemalhas.com/

  9. <a href="http://www.foolheartedly.com/how-to-choose-hermes-scarf-hermesr">how to choose hermes scarf</a> <a href="http://www.extonobgyn.com/prada-backpack-vz0062-instructions-pradac">prada backpack vz0062 instructions</a> <a href="http://www.cheljm.com/jimmy-choo-navy-boots-louboutinr">jimmy choo navy boots</a> <a href="http://www.salerahstore.com/mackage-coat-cheap-online-mackageb">mackage coat cheap online</a> <a href="http://www.sftfinance.com/tory-burch-wedge-thong-sandal-toryburchr">tory burch wedge thong sandal</a> <a href="http://www.cherrytreetv.com/billig-nike-air-force-1-kvinders-r%C3%B8d-runninge">billig nike air force 1 kvinders r酶d</a>
    namboathood http://www.namboathood.com/

  10. <a href="http://www.scoutpages.net/adidas-climacool-ride-ii-svart-gull-obuva">adidas climacool ride ii svart gull</a> <a href="http://www.scramboo.net/nike-free-flyknit-3.0-for-bon-march%C3%A9-quality-butyc">nike free flyknit 3.0 for bon march茅 quality</a> <a href="http://www.seusiteimobiliario.net/blau-nike-cortez-for-toddlers-scarpeb">blau nike cortez for toddlers</a> <a href="http://www.sharonbrown.net/jordan-future-infrarojo-nike-kurpesf">jordan future infrarojo nike</a> <a href="http://www.sheftalljewelers.net/adidas-nmd-runner-noir-and-bleu-zero-scarpea">adidas nmd runner noir and bleu zero</a> <a href="http://www.shoesilove.net/air-jordan-10-retro-blanco-negro-rojo-and-blanco-obuvc">air jordan 10 retro blanco negro rojo and blanco</a>
    tastingtheacorn http://www.tastingtheacorn.net/

  11. <a href="http://www.starizle.com/adidas-superstar-sko-how-to-wear-schuhea">adidas superstar sko how to wear</a> <a href="http://www.notpotatoes.com/mennns-nike-zoom-pegasus-32-oransje-hvit-chaussuresa">mennns nike zoom pegasus 32 oransje hvit</a> <a href="http://www.tedxuon.com/air-jordan-6-infrarojo-wave-obuva">air jordan 6 infrarojo wave</a> <a href="http://www.thejuicylucy.com/nike-hyperdunk-2014-low-schwarz-blau-butyc">nike hyperdunk 2014 low schwarz blau</a> <a href="http://www.almanyaonline.com/adidas-x-16.1-fg-lilla-svart-scarpeb">adidas x 16.1 fg lilla svart</a> <a href="http://www.trademelikes.com/nike-sb-dunk-mid-royal-bl%C3%A5-kurpesf">nike sb dunk mid royal bl氓</a>
    fifteenforty http://www.fifteenforty.com/

  12. <a href="http://www.michiganev.com/adidas-harden-vol-1-blue-black-purple-shoesk">adidas harden vol 1 blue black purple</a> <a href="http://www.serdarhan.com/air-jordan-retro-11-anti-fur-svart-obuvi">air jordan retro 11 anti fur svart</a> <a href="http://www.ankaralimnet.com/kvinners-nike-air-presto-oransje-gull-obuvk">kvinners nike air presto oransje gull</a> <a href="http://www.greenwichua.com/adidas-tubular-runner-svart-svart-obuvi">adidas tubular runner svart svart</a> <a href="http://www.minicomposer.com/negro-and-p%C3%BArpura-jordan-eclipse-zapatosl">negro and p煤rpura jordan eclipse</a> <a href="http://www.tabletopsport.com/nike-free-3.0-grigio-scarpeh">nike free 3.0 grigio</a>
    poluscompany http://www.poluscompany.com/

  13. <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/

  14. <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/

  15. 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/

  16. 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/

  17. 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/

  18. 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]

  19. 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/

  20. avatar 津电动卷帘门

    这个步骤还是挺好的

  21. avatar bet365娱乐城

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

  22. avatar 预应力钢绞线

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

  23. avatar 行业报告分析

    嗯嗯,看过了~

  24. avatar 梦居家

    看不太懂  不是很明白

  25. avatar 按摩椅

    看起来很不错哦

欢迎留言




用户登录

sitemap