遇到一个vb的函数,在C#里应该怎样的?Function CodeToUni(code)
Dim ix, thisStr, ThisCode, newcode
If code = "" Then
CodeToUni = ""
Exit Function
End If
For ix = 1 To Len(code)
thisStr = Right(Left(code, ix), 1)
ThisCode = Hex(AscW(thisStr))
If Len(ThisCode) = 1 Then
ThisCode = "000" + ThisCode
ElseIf Len(ThisCode) = 2 Then
ThisCode = "00" + ThisCode
ElseIf Len(ThisCode) = 3 Then
ThisCode = "0" + ThisCode
End If
newcode = newcode + ThisCode
Next
CodeToUni = newcode
End Function
==============================
CodeToUni(“测试”) = “6D4B8BD5”
Dim ix, thisStr, ThisCode, newcode
If code = "" Then
CodeToUni = ""
Exit Function
End If
For ix = 1 To Len(code)
thisStr = Right(Left(code, ix), 1)
ThisCode = Hex(AscW(thisStr))
If Len(ThisCode) = 1 Then
ThisCode = "000" + ThisCode
ElseIf Len(ThisCode) = 2 Then
ThisCode = "00" + ThisCode
ElseIf Len(ThisCode) = 3 Then
ThisCode = "0" + ThisCode
End If
newcode = newcode + ThisCode
Next
CodeToUni = newcode
End Function
==============================
CodeToUni(“测试”) = “6D4B8BD5”
{
object ix = null, thisStr = null, ThisCode = null, newcode = null; if (string.IsNullOrEmpty(code)) {
CodeToUni() = "";
return;
} for (ix = 1; ix <= Strings.Len(code); ix++) {
thisStr = Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(code, ix), 1);
ThisCode = Conversion.Hex(Strings.AscW(thisStr));
if (Strings.Len(ThisCode) == 1) {
ThisCode = "000" + ThisCode;
} else if (Strings.Len(ThisCode) == 2) {
ThisCode = "00" + ThisCode;
} else if (Strings.Len(ThisCode) == 3) {
ThisCode = "0" + ThisCode;
}
newcode = newcode + ThisCode;
} return newcode;}
{
object ix = null, thisStr = null, ThisCode = null, newcode = null;
if (string.IsNullOrEmpty(code)) {
return;
} for (ix = 0; ix <code.Length; ix++) {
ThisCode = (char)int.Parse(code[ix], System.Globalization.NumberStyles.HexNumber);
if (ThisCode.length == 1) {
ThisCode = "000" + ThisCode;
} else if (ThisCode.length == 2) {
ThisCode = "00" + ThisCode;
} else if (ThisCode.length == 3) {
ThisCode = "0" + ThisCode;
}
newcode = newcode + ThisCode;
}
return newcode;
}
{
if (code == "") return ""; // 参数空直接返回空 string thisStr, thisCode, newcode = "";
for (int i = 0; i < code.Length; i++)
{
thisStr = code.Substring(i, 1);
thisCode = ((int)Convert.ToChar(thisStr)).ToString("X"); // 转换成十六进制
newcode += thisCode.PadLeft(4, '0'); // 左边加0,补足4位
}
return newcode;
}
// 测试用例
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show( CodeToUni("abc人民"));
}