当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《在C#中,VB的Asc()和Chr()函数相当于什么?》经验,为你挑选了3个好方法。

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

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



1> Garry Shutle..:

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

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


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

2> Andrew Hare..:

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

int i = (int)your_char;

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

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);
    }
}


-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.

3> deadManN..:

我使用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;
}

推荐阅读
罗文彬2502852027
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有