首页 新闻 会员 周边

帮忙改编一段关于编码的VB代码为C#代码

0
悬赏园豆:5 [待解决问题]

Public Function 计算累加和(字符串) As String   '返回字符串(有汉字)的累加和
Dim i, a, Sum0
Sum0 = 0
For i = 1 To LenByte(字符串)
    a = AscByte(字符串, i)    '一个字符的ASC码,1字节或2字节
    Sum0 = Sum0 + a
Next i
计算累加和 = RightByte补空(Hex(Sum0), 2, "0")     '补足2位并且只要2位
End Function

 

Public Function LenByte(字符串)  '按字节返回字符串(有汉字)的字节数,代替Len
LenByte = LenB(StrConv(字符串, vbFromUnicode))
End Function

Public Function AscByte(字符串, 第几个) '按字节返回字符串(有汉字)的ASCII码,代替Asc
Dim s
s = StrConv(字符串, vbFromUnicode)
AscByte = AscB(MidB(s, 第几个, 1))
End Function

Public Function RightByte补空(字符串, 长度, 补空字符) '按字节返回字符串(有汉字)的右子串,不足补空格,代替Left
Dim L
L = LenByte(字符串)
If 长度 > L Then
    RightByte补空 = String(长度 - L, 补空字符) & 字符串
Else
    RightByte补空 = RightByte(字符串, 长度)
End If
End Function


Public Function RightByte(字符串, 长度) '按字节返回字符串(有汉字)的右子串,代替Right
Dim s
s = RightB(StrConv(字符串, vbFromUnicode), 长度)
RightByte = StrConv(s, vbUnicode)
End Function

 

不胜感激!!!

giszmr的主页 giszmr | 初学一级 | 园豆:195
提问于:2011-10-11 11:47
< >
分享
所有回答(1)
0

改了一下,你看下对不对。

private VbStrConv vbFromUnicode = VbStrConv.SimplifiedChinese;


///<summary>
/// 返回字符串(有汉字)的累加和
///</summary>
///<param name="字符串"></param>
///<returns></returns>
public string 计算累加和(string 字符串 )
{
dynamic Sum0 = null;
Sum0 = 0;
for (int i = 1; i <= LenByte(字符串); i++)
{
Sum0 = Sum0 + AscByte(字符串, i);
//一个字符的ASC码,1字节或2字节
}
return RightByte补空(Microsoft.VisualBasic.Conversion.Hex(Sum0), 2, '0');
//补足2位并且只要2位
}
///<summary>
/// 按字节返回字符串(有汉字)的字节数,代替Len
///</summary>
///<param name="字符串"></param>
///<returns></returns>
public int LenByte(string 字符串 )
{
return LenB(Strings.StrConv(字符串, vbFromUnicode));
}
///<summary>
/// 按字节返回字符串(有汉字)的ASCII码,代替Asc
///</summary>
///<param name="字符串"></param>
///<param name="第几个"></param>
///<returns></returns>
public int AscByte(string 字符串, int 第几个)
{
dynamic s = null;
s = Strings.StrConv(字符串, vbFromUnicode);
return AscB(MidB(s, 第几个, 1));
}
///<summary>
/// 按字节返回字符串(有汉字)的右子串,不足补空格,代替Left
///</summary>
///<param name="字符串"></param>
///<param name="长度"></param>
///<param name="补空字符"></param>
///<returns></returns>
public object RightByte补空(string 字符串 , int 长度 ,char 补空字符 )
{
string functionReturnValue = null;
int L = LenByte(字符串);
if (长度 > L)
{
字符串.PadLeft(长度 - L, 补空字符);
}
else
{
functionReturnValue = RightByte(字符串, 长度);
}
return functionReturnValue;
}
///<summary>
/// 按字节返回字符串(有汉字)的右子串,代替Right
///</summary>
///<param name="字符串"></param>
///<param name="长度"></param>
///<returns></returns>
public string RightByte(string 字符串, int 长度)
{
dynamic s = null;
s = RightB(Strings.StrConv(字符串, vbFromUnicode), 长度);
return Strings.StrConv(s, vbFromUnicode);
}

public static string RightB(string stTarget, int iByteSize)
{
System.Text.Encoding hEncoding = System.Text.Encoding.Unicode;
byte[] bBytes = hEncoding.GetBytes(stTarget);

return hEncoding.GetString(bBytes, bBytes.Length - iByteSize, iByteSize);
}


private static int LenB(string CharWords)
{
return (int)Encoding.Unicode.GetByteCount(CharWords);
}
private static int AscB(string CharWord)
{
if (CharWord == "")
{
return 0;
}
else
{
return (int)Encoding.Unicode.GetBytes(CharWord)[0];
}
}
private static string MidB(string CharWords, int Start, int ByteSize)
{
byte[] bytes = Encoding.Unicode.GetBytes(CharWords);
if (ByteSize == 0)
{
return Encoding.Unicode.GetString(bytes, Start - 1, bytes.Length - Start + 1);
}
else
{
return Encoding.Unicode.GetString(bytes, Start - 1, ByteSize);
}

}
LCM | 园豆:6876 (大侠五级) | 2011-10-11 13:59

源代码和您改后的结果不一致。

支持(0) 反对(0) giszmr | 园豆:195 (初学一级) | 2011-10-14 11:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册