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
江天部落格
閱讀(297)
評論(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
江天部落格
江天部落格
搜索
積分與排名
積分 - 248364
排名 - 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和簽名(29158)
2.?Eclipse中導入android項目名前有紅叉但項目內文件無錯誤問題解決方法(19068)
3.?Installation failed due to invalid APK file!問題(16575)
4.?SQLite中的時間日期函數(轉)(14409)
5.?Android短信備份(12890)
評論排行榜
1.?Android短信備份(6)
2.?Android彩信閱讀軟件MMSViewer,用Gphone看彩信手機報非常好用(5)
3.?彩信閱讀2.1.2版本上線--應網友morning要求,新增刪除彩信功能。(3)
4.?使用FileFilter查找文件系統(3)
5.?數據庫表中插入重復數據的處理(3)
主站蜘蛛池模板:
亚洲日韩精品国产3区
|
蜜芽亚洲av无码精品色午夜
|
日本三级2019在线观看免费
|
国产无遮挡裸体免费视频
|
2019亚洲午夜无码天堂
|
国产亚洲视频在线观看网址
|
成人性生免费视频
|
久久久久亚洲精品天堂久久久久久
|
在线观看亚洲AV日韩A∨
|
中文字幕人成无码免费视频
|
亚洲综合精品伊人久久
|
免费看美女被靠到爽
|
高h视频在线免费观看
|
美丽的姑娘免费观看在线播放
|
免费夜色污私人影院在线观看
|
亚洲国产精品国自产电影
|
午夜视频免费在线观看
|
亚洲成人免费电影
|
国产免费AV片在线观看
|
免费少妇a级毛片
|
一级一级一级毛片免费毛片
|
亚洲精品成人网站在线观看
|
18禁男女爽爽爽午夜网站免费
|
亚洲国产精品成人精品小说
|
日本一道综合久久aⅴ免费
|
亚洲六月丁香六月婷婷蜜芽
|
国产成人精品亚洲精品
|
在线观看免费播放av片
|
亚洲国产视频一区
|
免费欧洲毛片A级视频无风险
|
亚洲第一区二区快射影院
|
3344免费播放观看视频
|
亚洲永久网址在线观看
|
中文字幕亚洲天堂
|
黄+色+性+人免费
|
全部在线播放免费毛片
|
久久综合亚洲色HEZYO社区
|
国产免费牲交视频
|
少妇人妻偷人精品免费视频
|
亚洲色偷偷偷综合网
|
亚洲va中文字幕无码久久
|