免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
CSDN 文檔中心:手機(jī)中文碼制問題的一點(diǎn)理解
手機(jī)中文碼制問題的一點(diǎn)理解
論壇上很多的帖子都是在討論手機(jī)上的中文碼制問題,我也曾經(jīng)被此類的問題所困擾,并且得到了不少熱心的朋友的幫助。通過一端時(shí)間的資料查找和測(cè)試學(xué)習(xí),我對(duì)這個(gè)問題有一點(diǎn)點(diǎn)自己的理解和想法,不敢敝帚自珍,特分享給大家,由于本人水平有限,況且也是業(yè)余的開發(fā)愛好者,沒有專業(yè)的理論學(xué)習(xí)水平,所以請(qǐng)大家就文章中的一些錯(cuò)誤予以諒解并提出批評(píng),本篇的文章僅做拋磚引玉,非常的歡迎大家的跟貼,我們?nèi)翰呷毫?,共同來解決這個(gè)問題。:)

  論壇上很多的帖子都是在討論手機(jī)上的中文碼制問題,我也曾經(jīng)被此類的問題所困擾,并且得到了不少熱心的朋友的幫助。通過一端時(shí)間的資料查找和測(cè)試學(xué)習(xí),我對(duì)這個(gè)問題有一點(diǎn)點(diǎn)自己的理解和想法,不敢敝帚自珍,特分享給大家,由于本人水平有限,況且也是業(yè)余的開發(fā)愛好者,沒有專業(yè)的理論學(xué)習(xí)水平,所以請(qǐng)大家就文章中的一些錯(cuò)誤予以諒解并提出批評(píng),本篇的文章僅做拋磚引玉,非常的歡迎大家的跟貼,我們?nèi)翰呷毫?,共同來解決這個(gè)問題。:)

手機(jī)里面的字符串基本上都是采用的UTF-8的編碼法。
而我們?cè)赑C機(jī)器上所采用的基本上都是ASCII和unicode編碼法
ASCII編碼法是單字節(jié)的編碼方法,只能表示256個(gè)字符,英文字母是足夠了
但是無法表示漢字
unicode是雙字節(jié)的編碼法,可以用來表示漢字,但是卻對(duì)于一般的英文字母浪費(fèi)了太多的空間(至少面對(duì)于手機(jī)的存儲(chǔ)是這樣的)。
UTF-8就是專門手機(jī)這種嵌入式設(shè)備的新的編碼法,他的特點(diǎn)是,傳統(tǒng)的ASCII字符還是以一個(gè)字節(jié)來表示的,但是如果字符不屬于ASCII字符集時(shí),就用兩至三個(gè)位來表示。
在 0x0001-0x007F之間的字符(傳統(tǒng)的ASCII字符)用一個(gè)位來表示
  0 | bits0-6
在 0x000以及在0x0080-0x07FF之間的字符使用下面來表示:
  1 | 1 | 0 | bits 6-10 | 1 | 0 | bits 0-5
如果虛擬機(jī)看到這樣的一個(gè)字符的話,虛擬機(jī)會(huì)把第一個(gè)字節(jié)最前頭的110以及第二個(gè)字節(jié)的前頭的10拿掉把剩下的位重新組合成一個(gè)2字節(jié)的數(shù)位來表示字符:
  00000 | bits 6-10 | bits 0-5
同理,0x0800 - 0xFFFF的字符表示:
 1 | 1 | 1 | 0 | bits 12-15 | 1 | 0 | bits 6-11 | 1 | 0 | bits 0-5
也可以用同樣的方法重新組合成一個(gè)兩個(gè)字節(jié)的字符串來
 特別需要注意的是kjava中的null字符也使用兩個(gè)字節(jié)來表示而不是一個(gè)字節(jié):)

當(dāng)然英文字符串在UTF-8編碼法中不會(huì)出什么問題(默認(rèn)為標(biāo)準(zhǔn)的ACSII編碼機(jī)制)主要的問題還是中文,我個(gè)人在Kjava的手機(jī)開發(fā)中中文字符串所碰到的問題主要分為以下幾類:
 1.rms數(shù)據(jù)庫(kù)讀寫的問題;
 2.在jad中書寫游戲中文名稱;
 3.網(wǎng)絡(luò)傳輸中中文問題(kxml傳輸?shù)慕獯a);
 4.部分的模擬器也不支持中文.
這幾個(gè)部分是在手機(jī)開發(fā)中,中文經(jīng)常出錯(cuò)的險(xiǎn)區(qū),通常的表現(xiàn)形式是亂碼:)

1.了解到了UTF-8碼的基本原理就非常的有利于我們解決碼制轉(zhuǎn)化的問題
在轉(zhuǎn)化UTF-8碼中我處理的方法是這樣的

//向數(shù)據(jù)庫(kù)中寫入中文
String appt3 = ‘中文字符‘;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF(appt3);
byte[] bytes3 = bos.toByteArray();
rs.addRecord(bytes3, 0, bytes3.length);

//從數(shù)據(jù)庫(kù)中讀出中文
byte b3[] = rs.getRecord(dbid);
DataInputStream dis=new DataInputStream(new ByteArrayInputStream(b3));
String chinastring = dis.readUTF();

writeUTF() 和 readUTF() 分別是DataOutputStream 和 DataInputStream對(duì)象的的方法,他們提供了一個(gè)由從Unicode到UTF-8的相互轉(zhuǎn)化的途徑。
仔細(xì)看看midp的說明文檔,可以看到以下內(nèi)容
writeUTF() :
First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the UTF-8 encoding for the character.If no exception is thrown, the counter written is incremented by the total number of bytes written to the output stream. This will be at least two plus the length of str, and at most two plus thrice the length of str.

當(dāng)然我們也可以自己來手工的編寫代碼,把中文字符串轉(zhuǎn)化成byte[]再放入RMS,取出時(shí)轉(zhuǎn)成String即可。
這里借用bingo_guan的方法(bingo_guan,請(qǐng)不要介意呀 :)),當(dāng)然了這段代碼也非常的設(shè)計(jì)模式化 :) hehe,這個(gè)類也可用于文本文件操作。

/**
*

Title:


*
Description: unicode字串轉(zhuǎn)換工具


*
Copyright: Copyright (c) 2003


*
Company: CC Studio


* @author Bingo
* @version 1.0
*/

public class UnicodeString
{

public UnicodeString()
{
}

public static String byteArrayToString(byte abyte0[], int i)
{
StringBuffer stringbuffer = new StringBuffer(‘‘);
for(int j = 0; j < i; )
{
int k = abyte0[j++]; //注意在這個(gè)地方進(jìn)行了碼制的轉(zhuǎn)換
if(k < 0)
k += 256;
int l = abyte0[j++];
if(l < 0)
l += 256;
char c = (char)(k + (l << 8));//把高位和低位數(shù)組裝起來
stringbuffer.append(c);
}

return stringbuffer.toString();
}

public static String byteArrayToString(byte abyte0[])
{
return byteArrayToString(abyte0, abyte0.length);
}

public static byte[] stringToByteArray(String s)
{
int i = s.length();
byte abyte0[] = new byte[i << 1];
int j = 0;
for(int k = 0; k < i; k++)
{
char c = s.charAt(k);
abyte0[j++] = (byte)(c & 0xff); //每一個(gè)位按位轉(zhuǎn)化
abyte0[j++] = (byte)(c >> 8);
}

return abyte0;
}
}
2.其次,在jad和manifest中的中文字(比如說游戲的名字)實(shí)際上也都是 UTF-8編碼,這一塊也是經(jīng)常出問題的險(xiǎn)區(qū),我建議還是自己手工轉(zhuǎn)化成UTF-8的編碼寫在上面,否則的話,如果你用unicode碼制寫入中文的話,在模擬器或者實(shí)際設(shè)備上就有無法識(shí)別而導(dǎo)致程序不能執(zhí)行的危險(xiǎn)。所以大家在編輯jad文件的時(shí)候應(yīng)該盡量小心才好 :) 特別注意,wtk的jad自動(dòng)生成的工具并不支持直接在jad和manifest輸入U(xiǎn)TF-8格式,所以手工修改這一步恐怕是免不了的了 :(。

3.不同的手機(jī)其實(shí)支持的默認(rèn)碼制也是不一樣的,這也是經(jīng)常出現(xiàn)問題的關(guān)鍵,CLDC的系統(tǒng)屬性‘microedition.encoding‘定義了設(shè)備的默認(rèn)字符編碼,它的值可以使用System.getProperty方法取得。我們也可以轉(zhuǎn)化成相關(guān)的支持的編碼機(jī)制來實(shí)際的運(yùn)行我們的程序。
這種方式我們通常會(huì)用在有關(guān)手機(jī)中文問題傳輸中,因?yàn)樵诼?lián)網(wǎng)時(shí)的手機(jī)是不確定的。以下我給出一段實(shí)例代碼,和大家探討一下這個(gè)問題。

服務(wù)器到客戶端:
------------------------------------------------------------------
下面代碼是服務(wù)器端把字符寫到Client端,經(jīng)過gbEncoding()方法,所有的字符編碼成:\uXXXX.
-----------------------------------------------------------------

代碼:-------------------------------------------------------------
/**
* Write the String data
*
* @param out
* @param value
*/
public static void writeUnicode(final DataOutputStream out, final String value) throws ActionException {
try {
final String unicode = StringFormatter.gbEncoding( value );
final byte[] data = unicode.getBytes();
final int dataLength = data.length;

System.out.println( ‘Data Length is: ‘ + dataLength );
System.out.println( ‘Data is: ‘ + value );
out.writeInt( dataLength ); //先寫出字符串的長(zhǎng)度
out.write( data, 0, dataLength ); //然后寫出轉(zhuǎn)化后的字符串
} catch (IOException e) {
throw new ActionException( IMDefaultAction.class.getName(), e.getMessage() );
}
}

----------------------------------------------------------------------
以下代碼是gbEncoding()方法,把雙字節(jié)字符轉(zhuǎn)換成\uXXXX,ASIIC碼在前面補(bǔ)00。
----------------------------------------------------------------------
/**
* This method will encode the String to unicode.
*
* @param gbString
* @return
*/

代碼:--------------------------------------------------------------------------------
public static String gbEncoding( final String gbString ) {
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = ‘‘;
for( int byteIndex = 0; byteIndex < utfBytes.length; byteIndex ++ ) {
String hexB = Integer.toHexString( utfBytes[ byteIndex ] );
if( hexB.length() <= 2 ) {
hexB = ‘00‘ + hexB;
}
unicodeBytes = unicodeBytes + ‘\\u‘ + hexB;
}
System.out.println( ‘unicodeBytes is: ‘ + unicodeBytes );
return unicodeBytes;
}
--------------------------------------------------------------------------------

----------------------------------------------------------------------
在客戶端收到服務(wù)器的數(shù)據(jù),先將其一個(gè)一個(gè)字符解碼。雙字節(jié)顯示正常。
----------------------------------------------------------------------

代碼:--------------------------------------------------------------------------------
/**
* This method will decode the String to a recognized String
* in ui.
* @param dataStr
* @return
*/
private StringBuffer decodeUnicode( final String dataStr ) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while( start > -1 ) {
end = dataStr.indexOf( ‘\\u‘, start + 2 );
String charStr = ‘‘;
if( end == -1 ) {
charStr = dataStr.substring( start + 2, dataStr.length() );
} else {
charStr = dataStr.substring( start + 2, end);
}
char letter = (char) Integer.parseInt( charStr, 16 ); // 16進(jìn)制parse整形字符串。
buffer.append( new Character( letter ).toString() );
start = end;
}
return buffer;
}
--------------------------------------------------------------------------------

----------------------------------------------------------------------
客戶端到服務(wù)器:
----------------------------------------------------------------------
客戶端使用下面方法把手機(jī)端的字符編碼成ISO-8859-1,傳給服務(wù)器。
----------------------------------------------------------------------
代碼:--------------------------------------------------------------------------------
/**
* write the String data
* @param value
* @param outData
*/
private void writeSjis(DataOutputStream outData, String value) {
try {
byte[] data = null;
// data = ( value ).getBytes( ‘UTF-8‘ );
data = ( value ).getBytes( ‘ISO8859_1‘ );
outData.writeInt(data.length);
outData.write(data, 0, data.length);

System.out.println(‘ data.length: ‘ + data.length);
System.out.println(‘ data.value: ‘ + value);
} catch (Exception ex) {
System.out.println(‘ write error ‘);
ex.printStackTrace();
}
}
--------------------------------------------------------------------------------

----------------------------------------------------------------------
服務(wù)器端收到客戶端字符流,是用下面方法將其轉(zhuǎn)為UTF-8,以后的操作都是基于UTF-8編碼。SQLServer可能會(huì)由于內(nèi)嗎不通有不同的變換,所以存取數(shù)據(jù)庫(kù)是還要是具體的DB內(nèi)碼作相應(yīng)的處理。
----------------------------------------------------------------------

代碼:--------------------------------------------------------------------------------
/**
*
* @param iso
* @return
*/
public static String isoToUtf( final String iso ) {
String utfString = iso;
if( iso != null ) {
try {
utfString = new String( iso.getBytes( ‘ISO-8859-1‘ ), ‘UTF-8‘ );
} catch ( UnsupportedEncodingException e ) {
utfString = iso;
}
} else {
utfString = ‘‘;
}
return utfString;
}

只要手機(jī)支持unicode的gb2312編碼,應(yīng)該都可以顯示正常。

4。至于某些手機(jī)的模擬器不支持中文(譬如nokia 60系列),那真的沒有辦法了,只有等待他的中文版本出來了 呵呵,:)
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
java亂碼問題分析
初學(xué)入門:JAVA里字符編碼的探索與理解 - 全部文章 - 技術(shù)學(xué)習(xí)
JAVA里字符編碼 - Java - linxh
Java中文問題詳解,底層編碼解剖
C# 如何將一個(gè)字符串轉(zhuǎn)換成字節(jié)數(shù)組”與“如何將一個(gè)字節(jié)數(shù)組轉(zhuǎn)換成一個(gè)字符串
Tomcat關(guān)于encoding編碼的默認(rèn)設(shè)置以及亂碼產(chǎn)生的原因
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服