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)
主站蜘蛛池模板:
亚洲午夜激情视频
|
午夜高清免费在线观看
|
久久精品夜色噜噜亚洲A∨
|
色噜噜噜噜亚洲第一
|
成年女人毛片免费视频
|
亚洲一区精品视频在线
|
亚洲视频一区在线播放
|
99精品热线在线观看免费视频
|
亚洲AV一宅男色影视
|
免费看少妇高潮成人片
|
啦啦啦高清视频在线观看免费
|
亚洲国产精品久久久久网站
|
亚洲另类古典武侠
|
在线a免费观看最新网站
|
亚洲美女自拍视频
|
欧美a级在线现免费观看
|
亚洲人成色77777在线观看
|
免费v片视频在线观看视频
|
欧洲美女大片免费播放器视频
|
人妻无码久久一区二区三区免费
|
亚洲国产综合91精品麻豆
|
国产成人免费在线
|
亚洲精品乱码久久久久久按摩
|
亚洲性无码AV中文字幕
|
永久免费av无码入口国语片
|
亚洲国产婷婷六月丁香
|
亚洲免费观看网站
|
亚洲大码熟女在线观看
|
成人无遮挡裸免费视频在线观看
|
亚洲乱妇熟女爽到高潮的片
|
亚洲国产一区视频
|
中文字幕免费观看
|
国产成人 亚洲欧洲
|
亚洲AV午夜成人影院老师机影院
|
在线免费视频一区二区
|
性生大片视频免费观看一级
|
亚洲av片劲爆在线观看
|
免费黄色网址入口
|
亚洲最大的成人网
|
国产亚洲美女精品久久久
|
亚欧色视频在线观看免费
|