字符串和(Unicode)十六进制数之间的转换方法包含(C#和VB6)
来源:互联网 发布:安防平台软件 编辑:程序博客网 时间:2024/05/07 16:04
很多人在做短信发送的时候都会遇到字符转换的问题.也就是说汉字转换成Unicode编码的问题.
1.C#中的代码
/// <summary>
/// <函数:Encode>
/// 作用:将字符串内容转化为16进制数据编码,其逆过程是Decode
/// 参数说明:
/// strEncode 需要转化的原始字符串
/// 转换的过程是直接把字符转换成Unicode字符,比如数字"3"-->0033,汉字"我"-->U+6211
/// 函数decode的过程是encode的逆过程.
/// </summary>
/// <param name="strEncode"></param>
/// <returns></returns>
public static string Encode(string strEncode)
{
string strReturn = "";// 存储转换后的编码
foreach (short shortx in strEncode.ToCharArray())
{
strReturn += shortx.ToString("X4");
}
return strReturn;
}
/// <summary>
/// <函数:Decode>
///作用:将16进制数据编码转化为字符串,是Encode的逆过程
/// </summary>
/// <param name="strDecode"></param>
/// <returns></returns>
public static string Decode(string strDecode)
{
string sResult = "";
for (int i = 0; i < strDecode.Length / 4; i++)
{
sResult += (char)short.Parse(strDecode.Substring(i * 4, 4),global::System.Globalization.NumberStyles.HexNumber);
}
return sResult;
}
2.VB6中的代码
'*******************************************************************
'<函数:Encode>
'作用:将字符串内容转化为16进制数据编码,其逆过程是Decode
'参数说明:
'strSource 需要转化的原始字符串
Public Function Encode(strEncode As String) As String
Dim i As Long
Dim chrTmp$
Dim ByteLower$, ByteUpper$
Dim strReturn$ '存储转换后的编码
For i = 1 To Len(strEncode)
chrTmp$ = Mid(strEncode, i, 1)
ByteLower$ = Hex$(AscB(MidB$(chrTmp$, 1, 1)))
If Len(ByteLower$) = 1 Then ByteLower$ = "0" & ByteLower$
ByteUpper$ = Hex$(AscB(MidB$(chrTmp$, 2, 1)))
If Len(ByteUpper$) = 1 Then ByteUpper$ = "0" & ByteUpper$
strReturn$ = strReturn$ & ByteUpper$ & ByteLower$
Next
Encode = strReturn$
End Function
'</函数:Encode>
'*******************************************************************
'*******************************************************************
'<函数:Decode>
'作用:将16进制数据编码转化为字符串,是Encode的逆过程
Public Function Decode(strDecode As String) As String
Dim i As Long
Dim strCode$ '存储转换后的编码
Dim chrTmp$
On Error GoTo ErrProc
If Len(strDecode) Mod 4 <> 0 Then GoTo ErrProc
For i = 1 To Len(strDecode) Step 4
strCode = Mid$(strDecode, i, 4)
chrTmp$ = ChrW("&H" & strCode)
If chrTmp$ = "?" Then If strCode <> "003F" Then GoTo ErrProc
Decode = Decode & chrTmp$
Next
Exit Function
ErrProc:
Decode = strDecode
DealwithEvents "不能解析的消息:" & strDecode
End Function
'</函数:Decode>
'*******************************************************************
- 字符串和(Unicode)十六进制数之间的转换方法包含(C#和VB6)
- 字符串和(Unicode)十六进制数之间的转换方法包含(C#和VB6)
- 字符串和十六进制数之间的转换
- C#字符串和十六进制之间的转换方法
- C#字符串和十六进制之间的转换方法
- C#字符串和十六进制之间的转换方法
- C#字符串和十六进制之间的转换方法
- C#字符串和十六进制之间的转换方法
- 十六进制的数字字符和十六进制数之间的转换
- unicode和ANSI字符串之间的转换
- C#枚举和字符串以及整型数之间的转换
- 浮点数和十六进制之间的相互转换
- 字节数组和十六进制字符串之间的转换
- 十六进制字符串和byte数组之间的各种转换关系
- iOS NSData 和十六进制字符串之间的相互转换
- iOS NSData 和十六进制字符串之间的相互转换
- OC NSData 和十六进制字符串之间的相互转换
- Javabyte[]数组和十六进制String之间的转换Util------包含案例和代码
- js比例缩小显示图片
- C#中将UNICODE编码后的字符转换为汉字
- 什么样的快速开发平台才是我们想要的
- C#获取文件详细备注信息
- Programming shortcut
- 字符串和(Unicode)十六进制数之间的转换方法包含(C#和VB6)
- RGB图象缩放算法
- nchar,nvarchar,text,ntext,char,varchar的区别
- HTML5的视频格式之争
- SQL 连接数据库时 错误5120 的解决方法
- Visual Studio 2010带来的新机遇、新特性和新动力
- CAJViewer的金山词霸应用
- 理想的编程环境
- 枚举学习文摘 — 框架设计(第2版) CLR Via C#