Blogger Scott
一個utf8轉換程序
據說是一個通用的手機上使用的UTF8轉換程序,先記下來。
1
private
final
String readUnicodeFileUTF8(String filename)
{
2
StringBuffer sb
=
new
StringBuffer(
256
);
3
try
{
4
int
[] surrogatePair
=
new
int
[
2
];
5
InputStream is
=
this
.getClass().getResourceAsStream(filename);
6
7
int
val
=
0
;
8
int
unicharCount
=
0
;
9
while
((val
=
readNextCharFromStreamUTF8(is))
!=-
1
)
{
10
unicharCount
++
;
11
if
(val
<=
0xFFFF
)
{
12
//
if first value is the Byte Order Mark (BOM), do not add
13
if
(
!
(unicharCount
==
1
&&
val
==
0xFEFF
))
{
14
sb.append((
char
)val);
15
}
16
}
else
{
17
supplementCodePointToSurrogatePair(val, surrogatePair);
18
sb.append((
char
)surrogatePair[
0
]);
19
sb.append((
char
)surrogatePair[
1
]);
20
}
21
}
22
is.close();
23
}
catch
(Exception e)
{}
;
24
25
return
new
String(sb);
26
}
27
28
private
final
static
int
readNextCharFromStreamUTF8(InputStream is)
{
29
int
c
=
-
1
;
30
if
(is
==
null
)
return
c;
31
boolean
complete
=
false
;
32
33
try
{
34
int
byteVal;
35
int
expecting
=
0
;
36
int
composedVal
=
0
;
37
38
while
(
!
complete
&&
(byteVal
=
is.read())
!=
-
1
)
{
39
if
(expecting
>
0
&&
(byteVal
&
0xC0
)
==
0x80
)
{
/**/
/*
10xxxxxx
*/
40
expecting
--
;
41
composedVal
=
composedVal
|
((byteVal
&
0x3F
)
<<
(expecting
*
6
));
42
if
(expecting
==
0
)
{
43
c
=
composedVal;
44
complete
=
true
;
45
//
System.out.println("appending: U+" + Integer.toHexString(composedVal) );
46
}
47
}
else
{
48
composedVal
=
0
;
49
expecting
=
0
;
50
if
((byteVal
&
0x80
)
==
0
)
{
/**/
/*
0xxxxxxx
*/
51
//
one byte character, no extending byte expected
52
c
=
byteVal;
53
complete
=
true
;
54
//
System.out.println("appending: U+" + Integer.toHexString(byteVal) );
55
}
else
if
((byteVal
&
0xE0
)
==
0xC0
)
{
/**/
/*
110xxxxx
*/
56
expecting
=
1
;
//
expecting 1 extending byte
57
composedVal
=
((byteVal
&
0x1F
)
<<
6
);
58
}
else
if
((byteVal
&
0xF0
)
==
0xE0
)
{
/**/
/*
1110xxxx
*/
59
expecting
=
2
;
//
expecting 2 extending bytes
60
composedVal
=
((byteVal
&
0x0F
)
<<
12
);
61
}
else
if
((byteVal
&
0xF8
)
==
0xF0
)
{
/**/
/*
11110xxx
*/
62
expecting
=
3
;
//
expecting 3 extending bytes
63
composedVal
=
((byteVal
&
0x07
)
<<
18
);
64
}
else
{
65
//
non conformant utf-8, ignore or catch error
66
}
67
}
68
}
69
70
}
catch
(Exception e)
{
71
System.out.println(e.toString());
72
}
73
74
return
c;
75
}
76
77
private
final
static
void
supplementCodePointToSurrogatePair(
int
codePoint,
int
[] surrogatePair)
{
78
int
high4
=
((codePoint
>>
16
)
&
0x1F
)
-
1
;
79
int
mid6
=
((codePoint
>>
10
)
&
0x3F
);
80
int
low10
=
codePoint
&
0x3FF
;
81
82
surrogatePair[
0
]
=
(
0xD800
|
(high4
<<
6
)
|
(mid6));
83
surrogatePair[
1
]
=
(
0xDC00
|
(low10));
84
}
posted on 2009-06-07 16:37
江天部落格
閱讀(303)
評論(0)
編輯
收藏
所屬分類:
Android
、
Java
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
Android給scrollView截圖超過屏幕大小形成長圖
如何獲得谷歌admob廣告條的高度
Android開源庫和開源資源
關于error: Error: String types not allowed (at 'configChanges' with value 'keyboard|keyboardHidden| orientation|screenLayout|uiMode|screenSize|smallestScreenSize').
[轉]eclipse android工程沒有錯卻出現紅叉
轉:android 調用系統的接口
Eclipse中導入android項目名前有紅叉但項目內文件無錯誤問題解決方法
彩信閱讀2.1.2版本上線--應網友morning要求,新增刪除彩信功能。
Android Intent傳遞對象和ArrayList
Android事件處理模型二(基于監聽接口的事件處理)
Powered by:
BlogJava
Copyright © 江天部落格
My Links
BlogJava
首頁
聯系
聚合
管理
Blog Stats
Posts - 75
Stories - 2
Comments - 32
Trackbacks - 0
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(11)
給我留言
查看公開留言
查看私人留言
隨筆分類
Android(43)
(RSS)
Apache(1)
(RSS)
Falsh(1)
(RSS)
Java(6)
(RSS)
Linux(3)
(RSS)
PALM OS(1)
(RSS)
php(1)
(RSS)
Prestashop(2)
(RSS)
RFID(1)
(RSS)
Windows(1)
(RSS)
Windows 11
(RSS)
微信公眾號(1)
(RSS)
數據庫(3)
(RSS)
綜合類(6)
(RSS)
隨筆檔案
2024年4月 (1)
2015年10月 (1)
2015年6月 (1)
2015年4月 (3)
2013年10月 (5)
2013年1月 (1)
2012年10月 (1)
2012年3月 (2)
2012年2月 (2)
2011年11月 (2)
2011年10月 (1)
2011年6月 (1)
2011年5月 (3)
2011年1月 (1)
2010年12月 (5)
2010年11月 (3)
2010年3月 (1)
2010年2月 (7)
2010年1月 (4)
2009年12月 (1)
2009年8月 (2)
2009年6月 (4)
2009年5月 (6)
2008年11月 (1)
2008年8月 (1)
2008年2月 (1)
2007年11月 (1)
2007年5月 (1)
2007年4月 (1)
2006年10月 (2)
2006年9月 (1)
2006年8月 (3)
文章分類
Jetty(1)
(RSS)
PALM OS
(RSS)
原創文章
(RSS)
網上摘抄(1)
(RSS)
文章檔案
2007年5月 (1)
2006年8月 (1)
相冊
我的相冊
相關鏈接
Android Plot
江天部落格
江天部落格
搜索
積分與排名
積分 - 249021
排名 - 231
最新隨筆
1.?允許一個用戶使用一個以上用戶與服務器或共享資源的多重連接。中斷與此服務器或共享資源的所有連接
2.?jquery ui日期選擇器 datepicker
3.?Prestashop如何更改底部Powerdby信息
4.?網站自動跳轉至http://wpkg.org原因及解決方法
5.?微信公眾號獲取access_token的PHP代碼
6.?Android給scrollView截圖超過屏幕大小形成長圖
7.?如何獲得谷歌admob廣告條的高度
8.?Android開源庫和開源資源
9.?關于error: Error: String types not allowed (at 'configChanges' with value 'keyboard|keyboardHidden| orientation|screenLayout|uiMode|screenSize|smallestScreenSize').
10.?[轉]eclipse android工程沒有錯卻出現紅叉
最新評論
1.?請問如何在自己的網站上禁用facebook的插件
請問如何在自己的網站上禁用facebook的插件
--liping rosy
2.?re: Android短信備份 無法看到telephony
/data/data/com.android.providers.telephony/安卓4.2.2無法看到這個文件夾,求賜教!
--Ong
3.?re: [轉]eclipse android工程沒有錯卻出現紅叉[未登錄]
與虛擬機有關,將虛擬機關掉就可以了
--jerry
4.?re: Eclipse中導入android項目名前有紅叉但項目內文件無錯誤問題解決方法
>工程在項目列表中刪除(不從磁盤刪除)并重新導入一次
這個就好了,我是真實用戶
--tailor
5.?re: Eclipse中導入android項目名前有紅叉但項目內文件無錯誤問題解決方法
真的不錯@tailor
--tailor
閱讀排行榜
1.?Android權限之sharedUserId和簽名(29165)
2.?Eclipse中導入android項目名前有紅叉但項目內文件無錯誤問題解決方法(19087)
3.?Installation failed due to invalid APK file!問題(16583)
4.?SQLite中的時間日期函數(轉)(14415)
5.?Android短信備份(12910)
評論排行榜
1.?Android短信備份(6)
2.?Android彩信閱讀軟件MMSViewer,用Gphone看彩信手機報非常好用(5)
3.?彩信閱讀2.1.2版本上線--應網友morning要求,新增刪除彩信功能。(3)
4.?使用FileFilter查找文件系統(3)
5.?數據庫表中插入重復數據的處理(3)
主站蜘蛛池模板:
国产成人aaa在线视频免费观看
|
亚洲毛片免费观看
|
在线观看免费视频一区
|
亚洲一级特黄大片在线观看
|
永久免费无码网站在线观看个
|
国产裸模视频免费区无码
|
日韩欧美亚洲国产精品字幕久久久
|
在线观看免费成人
|
精品亚洲av无码一区二区柚蜜
|
免费国产在线观看老王影院
|
人体大胆做受免费视频
|
中文字幕亚洲一区
|
伊人久久亚洲综合
|
成人性生交大片免费看好
|
久久精品国产亚洲AV麻豆王友容
|
午夜网站在线观看免费完整高清观看
|
青草青草视频2免费观看
|
全亚洲最新黄色特级网站
|
国产va免费精品观看精品
|
亚洲成a人在线看天堂无码
|
又长又大又粗又硬3p免费视频
|
久久乐国产精品亚洲综合
|
永久免费不卡在线观看黄网站
|
亚洲精品资源在线
|
全免费一级午夜毛片
|
国产精品亚洲一区二区三区
|
成人免费AA片在线观看
|
久久久亚洲精华液精华液精华液
|
亚洲国产午夜中文字幕精品黄网站
|
一二三四在线观看免费中文在线观看
|
中文字幕亚洲专区
|
3d动漫精品啪啪一区二区免费
|
中国亚洲呦女专区
|
色噜噜亚洲精品中文字幕
|
**一级毛片免费完整视
|
亚洲第一综合天堂另类专
|
亚洲免费观看视频
|
成人免费看片又大又黄
|
91精品成人免费国产
|
香蕉大伊亚洲人在线观看
|
国产精品亚洲mnbav网站
|