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

Sha*_*ica 29 c# vb.net ascii

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

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

Gar*_*ler 29

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

这是获得完全相同功能的最简单方法.

  • VisualBasic在任何可移植库中都不存在.这仅适用于*经典Windows操作系统下的*. (8认同)
  • +1实际上**正确**,不像*all*其他答案.他们使用来自VB Asc和Chr的*不同*编码,并且*错误*. (5认同)
  • 如果添加引用,它将存在. (4认同)

And*_*are 22

因为Asc()你可以把charint变成这样:

int i = (int)your_char;
Run Code Online (Sandbox Code Playgroud)

因为Chr()你可以charint这样的方式回到:

char c = (char)your_int;
Run Code Online (Sandbox Code Playgroud)

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • -1因为这是*错*.VB.Net Asc*不会返回ASCII代码,*也不会返回Unicode代码.它[返回](http://msdn.microsoft.com/en-us/library/zew1e4wc(v = vs.71).aspx)当前Windows代码页中的"ANSI"代码(即取决于当前线程的语言环境) .演员表将返回Unicode代码点.大多数语言环境中的大多数字符都不同.使用Microsoft.VisualBasic.Strings.Asc和Chr. (14认同)

dea*_*anN 5

我使用resharper来获得这些,确切的代码由VB在您的计算机上运行

/// <summary>
/// Returns the character associated with the specified character code.
/// </summary>
/// 
/// <returns>
/// Returns the character associated with the specified character code.
/// </returns>
/// <param name="CharCode">Required. An Integer expression representing the <paramref name="code point"/>, or character code, for the character.</param><exception cref="T:System.ArgumentException"><paramref name="CharCode"/> &lt; 0 or &gt; 255 for Chr.</exception><filterpriority>1</filterpriority>
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;
  }
}


/// <summary>
/// Returns an Integer value representing the character code corresponding to a character.
/// </summary>
/// 
/// <returns>
/// Returns an Integer value representing the character code corresponding to a character.
/// </returns>
/// <param name="String">Required. Any valid Char or String expression. If <paramref name="String"/> is a String expression, only the first character of the string is used for input. If <paramref name="String"/> is Nothing or contains no characters, an <see cref="T:System.ArgumentException"/> error occurs.</param><filterpriority>1</filterpriority>
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;
  }
}


/// <summary>
/// Returns an Integer value representing the character code corresponding to a character.
/// </summary>
/// 
/// <returns>
/// Returns an Integer value representing the character code corresponding to a character.
/// </returns>
/// <param name="String">Required. Any valid Char or String expression. If <paramref name="String"/> is a String expression, only the first character of the string is used for input. If <paramref name="String"/> is Nothing or contains no characters, an <see cref="T:System.ArgumentException"/> error occurs.</param><filterpriority>1</filterpriority>
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]);
}
Run Code Online (Sandbox Code Playgroud)

资源只是存储的错误消息,因此您想以某种方式忽略它们,而其他两种您无权访问的方法如下:

internal static Encoding GetFileIOEncoding()
{
    return Encoding.Default;
}

internal static int GetLocaleCodePage()
{
    return Thread.CurrentThread.CurrentCulture.TextInfo.ANSICodePage;
}
Run Code Online (Sandbox Code Playgroud)