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
江天部落格
閱讀(298)
評論(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
江天部落格
江天部落格
搜索
積分與排名
積分 - 248392
排名 - 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項目名前有紅叉但項目內文件無錯誤問題解決方法(19069)
3.?Installation failed due to invalid APK file!問題(16575)
4.?SQLite中的時間日期函數(轉)(14410)
5.?Android短信備份(12890)
評論排行榜
1.?Android短信備份(6)
2.?Android彩信閱讀軟件MMSViewer,用Gphone看彩信手機報非常好用(5)
3.?彩信閱讀2.1.2版本上線--應網友morning要求,新增刪除彩信功能。(3)
4.?使用FileFilter查找文件系統(3)
5.?數據庫表中插入重復數據的處理(3)
主站蜘蛛池模板:
亚洲一区二区三区久久久久
|
亚洲av色影在线
|
国产精品亚洲四区在线观看
|
久操视频免费观看
|
亚洲AV无码不卡无码
|
免费观看一区二区三区
|
亚洲精品午夜国产VA久久成人
|
青青操在线免费观看
|
亚洲av之男人的天堂网站
|
日韩精品内射视频免费观看
|
亚洲精品视频免费看
|
91精品视频在线免费观看
|
亚洲电影唐人社一区二区
|
亚洲第一成年免费网站
|
亚洲欧美一区二区三区日产
|
国产精品自在自线免费观看
|
一本久久免费视频
|
亚洲av午夜福利精品一区
|
麻豆高清免费国产一区
|
亚洲人成电影网站免费
|
亚洲精品第一国产综合境外资源
|
99re8这里有精品热视频免费
|
亚洲短视频男人的影院
|
国产精品成人免费一区二区
|
亚洲国产成人久久精品动漫
|
日韩免费视频一区二区
|
亚洲AV无码国产精品色
|
情侣视频精品免费的国产
|
日韩精品在线免费观看
|
亚洲国产亚洲综合在线尤物
|
日本免费一二区在线电影
|
国产真人无码作爱免费视频
|
亚洲一本综合久久
|
成人黄18免费视频
|
久久精品免费网站网
|
亚洲国产精品成人久久久
|
亚洲XX00视频
|
真实国产乱子伦精品免费
|
边摸边吃奶边做爽免费视频99
|
亚洲精品狼友在线播放
|
91视频国产免费
|