Asc函数返回一个 Integer 值,该值表示与某个字符相对应的字符代码。
Dim MyInt As Integer
MyInt = Asc("A") ' MyInt is set to 65.
MyInt = Asc("a") ' MyInt is set to 97.
MyInt = Asc("你") ' MyInt is set to -15133
在c#中
我使用显式转换
int myint=(int)'你' myint返回的代码确是20320
我错在什么地方啊,应该如何在C#中取某的字符对应的字符代码啊。
7 个解决方案
#1
20320 should be the right value, since VB does not support unsigned integer
char a = 'A';
int i = (int)a;
int j= Convert.ToInt32(a);
char a = 'A';
int i = (int)a;
int j= Convert.ToInt32(a);
#2
那么如何得到-15133这样的值呢?我现在要翻译Asc函数
很急啊!
很急啊!
#3
20320正确
还是vb好用
还是vb好用
#4
up
#5
Char 变量以无符号的 16 位(2 个字节)数字的形式存储,取值范围为 0 到 65535。每个数字代表一个 Unicode 字符。不能直接在 Char 数据类型和数值类型之间进行转换,但可以使用 AscW 和 ChrW 函数来完成。
在单字符字符串文本之后附加文本类型的字符 C,可将其强制转换为 Char 数据类型。这在启用了类型检查开关(Option Strict)的情况下是必需的,如下所示:
Option Strict On
' ...
Dim CharVar As Char
CharVar = "Z" ' Cannot convert String to Char with Option Strict On.
CharVar = "Z"C ' Successfully assigns single character to CharVar.
等价的 .NET 数据类型是 System.Char。
AscW("你").ToString()="20320"
在单字符字符串文本之后附加文本类型的字符 C,可将其强制转换为 Char 数据类型。这在启用了类型检查开关(Option Strict)的情况下是必需的,如下所示:
Option Strict On
' ...
Dim CharVar As Char
CharVar = "Z" ' Cannot convert String to Char with Option Strict On.
CharVar = "Z"C ' Successfully assigns single character to CharVar.
等价的 .NET 数据类型是 System.Char。
AscW("你").ToString()="20320"
#6
try
short CSharpASC(char s)
{
byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s.ToString());
return (short)( (bytes[0] << 8) + bytes[1] );
}
short value = CSharpASC('你');
short CSharpASC(char s)
{
byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s.ToString());
return (short)( (bytes[0] << 8) + bytes[1] );
}
short value = CSharpASC('你');
#7
sorry, it is probably not robust, but try
short CSharpASC(char s)
{
byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s.ToString());
if (bytes.Length == 2 )
{
return (short)( (bytes[0] << 8) + bytes[1] );
}
else
return bytes[0];
}
short CSharpASC(char s)
{
byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s.ToString());
if (bytes.Length == 2 )
{
return (short)( (bytes[0] << 8) + bytes[1] );
}
else
return bytes[0];
}
#1
20320 should be the right value, since VB does not support unsigned integer
char a = 'A';
int i = (int)a;
int j= Convert.ToInt32(a);
char a = 'A';
int i = (int)a;
int j= Convert.ToInt32(a);
#2
那么如何得到-15133这样的值呢?我现在要翻译Asc函数
很急啊!
很急啊!
#3
20320正确
还是vb好用
还是vb好用
#4
up
#5
Char 变量以无符号的 16 位(2 个字节)数字的形式存储,取值范围为 0 到 65535。每个数字代表一个 Unicode 字符。不能直接在 Char 数据类型和数值类型之间进行转换,但可以使用 AscW 和 ChrW 函数来完成。
在单字符字符串文本之后附加文本类型的字符 C,可将其强制转换为 Char 数据类型。这在启用了类型检查开关(Option Strict)的情况下是必需的,如下所示:
Option Strict On
' ...
Dim CharVar As Char
CharVar = "Z" ' Cannot convert String to Char with Option Strict On.
CharVar = "Z"C ' Successfully assigns single character to CharVar.
等价的 .NET 数据类型是 System.Char。
AscW("你").ToString()="20320"
在单字符字符串文本之后附加文本类型的字符 C,可将其强制转换为 Char 数据类型。这在启用了类型检查开关(Option Strict)的情况下是必需的,如下所示:
Option Strict On
' ...
Dim CharVar As Char
CharVar = "Z" ' Cannot convert String to Char with Option Strict On.
CharVar = "Z"C ' Successfully assigns single character to CharVar.
等价的 .NET 数据类型是 System.Char。
AscW("你").ToString()="20320"
#6
try
short CSharpASC(char s)
{
byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s.ToString());
return (short)( (bytes[0] << 8) + bytes[1] );
}
short value = CSharpASC('你');
short CSharpASC(char s)
{
byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s.ToString());
return (short)( (bytes[0] << 8) + bytes[1] );
}
short value = CSharpASC('你');
#7
sorry, it is probably not robust, but try
short CSharpASC(char s)
{
byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s.ToString());
if (bytes.Length == 2 )
{
return (short)( (bytes[0] << 8) + bytes[1] );
}
else
return bytes[0];
}
short CSharpASC(char s)
{
byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s.ToString());
if (bytes.Length == 2 )
{
return (short)( (bytes[0] << 8) + bytes[1] );
}
else
return bytes[0];
}