在C#中,VB的Asc()和Chr()函数相当于什么?

VB有几个本机函数,用于将char转换为ASCII值,反之亦然 – Asc()和Chr()。

现在我需要在C#中获得等效的function。 什么是最好的方式?

您始终可以添加对Microsoft.VisualBasic的引用,然后使用完全相同的方法: Strings.Chr和Strings.Asc 。

这是获得完全相同function的最简单方法。

对于Asc()您可以将char转换为int如下所示:

 int i = (int)your_char; 

对于Chr()您可以从这样的intchar

 char c = (char)your_int; 

这是一个小程序,演示了整个事情:

 using System; class Program { static void Main() { char c = 'A'; int i = 65; // both print "True" Console.WriteLine(i == (int)c); Console.WriteLine(c == (char)i); } } 

对于Chr(),您可以使用:

 char chr = (char)you_char_value; 

我使用resharper得到了这些,确切的代码由你的机器上的VB运行

 ///  /// Returns the character associated with the specified character code. ///  /// ///  /// Returns the character associated with the specified character code. ///  /// Required. An Integer expression representing the , or character code, for the character. < 0 or > 255 for Chr.1 public static char Chr(int CharCode) { if (CharCode < (int) short.MinValue || CharCode > (int) ushort.MaxValue) throw new ArgumentException(Utils.GetResourceString("Argument_RangeTwoBytes1", new string[1] { "CharCode" })); if (CharCode >= 0 && CharCode <= (int) sbyte.MaxValue) return Convert.ToChar(CharCode); try { Encoding encoding = Encoding.GetEncoding(Utils.GetLocaleCodePage()); if (encoding.IsSingleByte && (CharCode < 0 || CharCode > (int) byte.MaxValue)) throw ExceptionUtils.VbMakeException(5); char[] chars = new char[2]; byte[] bytes = new byte[2]; Decoder decoder = encoding.GetDecoder(); if (CharCode >= 0 && CharCode <= (int) byte.MaxValue) { bytes[0] = checked ((byte) (CharCode & (int) byte.MaxValue)); decoder.GetChars(bytes, 0, 1, chars, 0); } else { bytes[0] = checked ((byte) ((CharCode & 65280) >> 8)); bytes[1] = checked ((byte) (CharCode & (int) byte.MaxValue)); decoder.GetChars(bytes, 0, 2, chars, 0); } return chars[0]; } catch (Exception ex) { throw ex; } } ///  /// Returns an Integer value representing the character code corresponding to a character. ///  /// ///  /// Returns an Integer value representing the character code corresponding to a character. ///  /// Required. Any valid Char or String expression. If  is a String expression, only the first character of the string is used for input. If  is Nothing or contains no characters, an  error occurs.1 public static int Asc(char String) { int num1 = Convert.ToInt32(String); if (num1 < 128) return num1; try { Encoding fileIoEncoding = Utils.GetFileIOEncoding(); char[] chars = new char[1] { String }; if (fileIoEncoding.IsSingleByte) { byte[] bytes = new byte[1]; fileIoEncoding.GetBytes(chars, 0, 1, bytes, 0); return (int) bytes[0]; } byte[] bytes1 = new byte[2]; if (fileIoEncoding.GetBytes(chars, 0, 1, bytes1, 0) == 1) return (int) bytes1[0]; if (BitConverter.IsLittleEndian) { byte num2 = bytes1[0]; bytes1[0] = bytes1[1]; bytes1[1] = num2; } return (int) BitConverter.ToInt16(bytes1, 0); } catch (Exception ex) { throw ex; } } ///  /// Returns an Integer value representing the character code corresponding to a character. ///  /// ///  /// Returns an Integer value representing the character code corresponding to a character. ///  /// Required. Any valid Char or String expression. If  is a String expression, only the first character of the string is used for input. If  is Nothing or contains no characters, an  error occurs.1 public static int Asc(string String) { if (String == null || String.Length == 0) throw new ArgumentException(Utils.GetResourceString("Argument_LengthGTZero1", new string[1] { "String" })); return Strings.Asc(String[0]); } 

资源只是存储错误消息,所以你想要忽略它们的方式,以及你无权访问的另外两个方法如下:

 internal static Encoding GetFileIOEncoding() { return Encoding.Default; } internal static int GetLocaleCodePage() { return Thread.CurrentThread.CurrentCulture.TextInfo.ANSICodePage; } 

对于非ASCII字符,Strings.Asc不等同于普通的C#强制转换,它可以超过127个代码值。 我在https://social.msdn.microsoft.com/Forums/vstudio/en-US/13fec271-9a97-4b71-ab28-4911ff3ecca0/equivalent-in-c-of-asc-chr-functions-of上找到了答案-vb?forum = csharpgeneral相当于这样:

  static int Asc(char c) { int converted = c; if (converted >= 0x80) { byte[] buffer = new byte[2]; // if the resulting conversion is 1 byte in length, just use the value if (System.Text.Encoding.Default.GetBytes(new char[] { c }, 0, 1, buffer, 0) == 1) { converted = buffer[0]; } else { // byte swap bytes 1 and 2; converted = buffer[0] << 16 | buffer[1]; } } return converted; } 

或者,如果您希望读取交易添加对Microsoft.VisualBasic程序集的引用。

  //Char to Int - ASC("]") int lIntAsc = (int)Char.Parse("]"); Console.WriteLine(lIntAsc); //Return 91 //Int to Char char lChrChar = (char)91; Console.WriteLine(lChrChar ); //Return "]" 

给定char c和int i,以及函数fi(int)和fc(char):

从char到int(类似于VB Asc()):显式地将char转换为int: int i = (int)c;

或隐式转换(提升): fi(char c) {i+= c;}

从int到char(类似于VB Chr()):

显式地将int转换为char: char c = (char)i, fc(int i) {(char)i};

不允许隐式转换,因为int比char更宽(具有更大的值范围)