zoukankan
html css js c++ java
Asp.Net 编码和解码
最近因为项目需要,做了一个投票的页面
(Html
,比如
A
页面
)
,要把它
Post
到一个
Aspx
页面(比如
B
页面),在这个
Aspx
页面上,需要确认一下,在提交到数据库,可是问题出来了,用户在
A
页面上点击
Submit
,
Post
到
B
页面上的时候,在
B
页面用
Request.Form[
“
ID
”
],
接收,但是显示时,有时候是乱码,有的时候却是正常的,不知道为什么,在网上查了一些资料,看了一些编码的文章,感觉出现问题的原因是这样的,
A
页面虽然在开始有一句
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
,但是用户用的机器上的编码可能是
UTF
或者
GB
的,在用户
Post
的时候,是用用户自己上的编码格式编的码(我的理解),从而在
Request
的时候就已经成乱码了。一开始,我想两种办法,一种是用
Url
编码,比如我们在
A
页面上有一个
Input : <input type="radio" id="IsRightSee_2" name="radiobuttonIsRightSee" value="
音、视频播放不连续
" />
音、视频播放不连续
,
我们把它的
Value
用
Url
编码
,
变成
<input type="radio" id="IsRightSee_2" name="radiobuttonIsRightSee" value="%e%d..
之类的
" />
音、视频播放不连续,在
B
页面用
Request[“radiobuttonIsRightSee”],
得到
Value,
然后再对这个
Value
解码,可是出乎我的意料的是,用这个办法得到的也是乱码。碰壁后只有用第二种方案,就是用
Unicode对其编码
,
因为原先我没有遇到编码解码问题,所以就在网上搜索资源,但是与其相关的都没有一个好的解决方案,只搜到一个汉字转
Unicode
的工具,把其下载下来,先做了一个测试,对上文中的
”
音、视频播放不连续
”,
进行编码,得到“
\u
97F
3\u3001\u
89C
6\u9891\u64AD\u653E\u4E0D\u8FDE\u7EED
”,然后再
B
页面上在对这个
Value
解码,果然,没有成乱码,看到这得你会不会想到,我怎么对这一串进行解码的,这个工具又没有源代码,其实这个工具是用
.Net
写的,
.Net
写的我们就有办法看里边的源代码了,只要他没有对源代码加密,用
Reflector就行。
解码的源代码如下:
private
string
NormalU2C(
string
input)
{
string
str
=
""
;
char
[] chArray
=
input.ToCharArray();
Encoding bigEndianUnicode
=
Encoding.BigEndianUnicode;
for
(
int
i
=
0
; i
<
chArray.Length; i
++
)
{
char
ch
=
chArray[i];
if
(ch.Equals(
'
\\
'
))
{
i
++
;
i
++
;
char
[] chArray2
=
new
char
[
4
];
int
index
=
0
;
index
=
0
;
while
((index
<
4
)
&&
(i
<
chArray.Length))
{
chArray2[index]
=
chArray[i];
index
++
;
i
++
;
}
if
(index
==
4
)
{
try
{
str
=
str
+
this
.UnicodeCode2Str(chArray2);
}
catch
(Exception)
{
str
=
str
+
@"
\u
"
;
for
(
int
j
=
0
; j
<
index; j
++
)
{
str
=
str
+
chArray2[j];
}
}
i
--
;
}
else
{
str
=
str
+
@"
\u
"
;
for
(
int
k
=
0
; k
<
index; k
++
)
{
str
=
str
+
chArray2[k];
}
}
}
else
{
str
=
str
+
ch.ToString();
}
}
return
str;
}
private
string
UnicodeCode2Str(
char
[] u4)
{
if
(u4.Length
<
4
)
{
throw
new
Exception(
"
It's not a unicode code array
"
);
}
string
str
=
"
0123456789ABCDEF
"
;
char
ch
=
char
.ToUpper(u4[
0
]);
char
ch2
=
char
.ToUpper(u4[
1
]);
char
ch3
=
char
.ToUpper(u4[
2
]);
char
ch4
=
char
.ToUpper(u4[
3
]);
int
index
=
str.IndexOf(ch);
int
num2
=
str.IndexOf(ch2);
int
num3
=
str.IndexOf(ch3);
int
num4
=
str.IndexOf(ch4);
if
(((index
==
-
1
)
||
(num2
==
-
1
))
||
((num3
==
-
1
)
||
(num4
==
-
1
)))
{
throw
new
Exception(
"
It's not a unicode code array
"
);
}
byte
num5
=
(
byte
)(((index
*
0x10
)
+
num2)
&
0xff
);
byte
num6
=
(
byte
)(((num3
*
0x10
)
+
num4)
&
0xff
);
byte
[] bytes
=
new
byte
[]
{ num5, num6 }
;
return
Encoding.BigEndianUnicode.GetString(bytes);
}
写到这里问题基本上解决了,可是如果您的页面上有n多的input ,你还用这个工具一个input,一个input,把其中的Value
Copy –Convert-Parse,到你的页面上吗?
其实我们可以写一个正则表达式,用正则表达式来找出
input
中的
Value,
然后编码之后,在把原先的
Value
替换成编码后的
Value,这样的话,即省了功夫,又不会出错(除非你的正则有问题),
如果你不清楚怎么写的话,见一下代码:
protected
void
Button1_Click(
object
sender, EventArgs e)
{
string
strPatter
=
@"
(<input\s*[^>]*value\s*=\s*"")([^""]*)("".*?/>)
"
;
//txtcontent为需要替换的
//txtresult为结果
Regex rgx
=
new
Regex(strPatter, RegexOptions.Multiline);
this
.txtresult.Text
=
rgx.Replace(txtcontent.Text,
new
MatchEvaluator(Encode));
}
//
调用委托
private
string
Encode(Match m)
{
string
strValue1
=
m.Groups[
1
].Value;
string
strValue2
=
m.Groups[
2
].Value;
string
strValue3
=
m.Groups[
3
].Value;
return
strValue1
+
EncodeUniCode(strValue2)
+
strValue3;
}
//
对中文编码成Unicode
private
string
EncodeUniCode(
string
input)
{
Encoding bigEndianUnicode
=
Encoding.BigEndianUnicode;
char
[] chArray
=
input.ToCharArray();
string
str
=
""
;
foreach
(
char
ch
in
chArray)
{
if
(ch.Equals(
'
\r
'
)
||
ch.Equals(
'
\n
'
))
{
str
=
str
+
ch;
}
else
{
byte
[] bytes
=
bigEndianUnicode.GetBytes(
new
char
[]
{ ch }
);
str
=
(str
+
@"
\u
"
)
+
string
.Format(
"
{0:X2}
"
, bytes[
0
])
+
string
.Format(
"
{0:X2}
"
, bytes[
1
]);
}
}
return
str;
}
编码工具下载
查看全文
相关阅读:
172. Factorial Trailing Zeroes
96. Unique Binary Search Trees
95. Unique Binary Search Trees II
91. Decode Ways
LeetCode 328 奇偶链表
LeetCode 72 编辑距离
LeetCode 226 翻转二叉树
LeetCode 79单词搜索
LeetCode 198 打家劫舍
LeetCode 504 七进制数
原文地址:https://www.cnblogs.com/xbf321/p/asp_net_char_convert_to_unicode.html
最新文章
Hystrix断路器
centos安装RibbonMQ
RabbitMQ简介及安装
luogu P1446 [HNOI2008]Cards burnside引理 置换 不动点
luogu P1712 [NOI2016]区间 贪心 尺取法 线段树 二分
luogu P3645 [APIO2015]雅加达的摩天楼 分块 根号分治
luogu P3285 [SCOI2014]方伯伯的OJ splay 线段树
7.18 NOI模拟赛 树论 线段树 树链剖分 树的直径的中心 SG函数 换根
7.19 基础数据结构选讲
7.18 NOI模拟赛 因懒无名 线段树分治 线段树维护直径
热门文章
2020牛客暑期多校训练营 第二场 K Keyboard Free 积分 期望 数学
2020牛客暑期多校训练营 第二场 J Just Shuffle 置换 群论
2020牛客暑期多校训练营 第二场 I Interval 最大流 最小割 平面图对偶图转最短路
C# AOP的实现(利用.Net自带的轻量级框架RealProxy)
C# 线程同步之Moitor实现线程的顺序执行
139. Word Break
303. Range Sum Query
97. Interleaving String
204. Count Primes
793. Preimage Size of Factorial Zeroes Function
Copyright © 2011-2022 走看看