VBには、charをASCII値に変換するためのネイティブ関数が2つあります。逆もまた同様です。Asc()とChr()です。C#のVBのAsc()とChr()関数に相当するものは何ですか?
これで同等の機能をC#で取得する必要があります。何が最善の方法ですか? Asc()
については
VBには、charをASCII値に変換するためのネイティブ関数が2つあります。逆もまた同様です。Asc()とChr()です。C#のVBのAsc()とChr()関数に相当するものは何ですか?
これで同等の機能をC#で取得する必要があります。何が最善の方法ですか? Asc()
については
charと(値の大きな範囲を持っている)、許可されませんStrings.Asc。
これはまったく同じ機能を得る最も簡単な方法です。
参照を追加すると、その参照が存在します。 – Samuel
+1実際には**は**、他は*すべて*とは違って正しいです。彼らはVBのAscとChrからの*異なる*エンコーディングを使い、*間違っています。 – MarkJ
@ MarkJ: 'Asc'と' Chr'の最も一般的な使い方は、プログラマが対応するUnicodeコードポイントにマップすると予想される0-126の範囲の値を使用すると思われます。たとえばChr(34)が '' 'ではなく' ''を生成するシステムがあったとしても、Chr(34)を書いたプログラマはおそらく '' 'を意図していると思います。 – supercat
あなたはこのようint
にchar
をキャストすることができます
int i = (int)your_char;
とChr()
のためにあなたは、このようなint
からchar
に戻ってキャストすることができます:ここで
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については
-1の文字列からのchar-from-int-string-as-usedが*間違っています。 VB.Net Asc *はASCIIコードを返しません。*また、Unicodeコードを返しません。 (現在のスレッドのロケールによって異なります)現在のWindowsコードページの "ANSI"コードは、 。キャストはUnicodeコードポイントを返します。ほとんどのロケールのほとんどの文字と異なります。 Microsoft.VisualBasic.Strings.AscおよびChrを使用します。 – MarkJ
@MarkJに同意する必要があります - このコードを試してみましたが、いくつかの文字(a-zなど)では同じ結果が返されますが、他のもの(「<」など)では異なる結果が返されます。 – JDB
はい、結果は同じではありません: {case 1. Strings.Chr(128) 結果1:8364 '€'} {case 2.(char)128 結果2:128 '(ボックス値)'} さらに、2番目の場合、128より大きい値は有効な文字の代わりに 'ボックス値'を返します。 – Sadiq
()あなたが使用することができます。
char chr = (char)you_char_value;
文字cを考えると私をint型、およびFiの(int型)とFC(CHAR)機能:VBの(アナログをintに文字から
をAsc()): charをintとして明示的にキャストします。i =(int)c;
またはmplicitly cast(promote):fi(c)、i + = c; CHAR(VBのChrのアナログ())にINTから
:
明示的charとしてINTキャスト:C =(チャー)I、FC((CHAR)I); Strings.Chrをして:
暗黙のキャストは、intは広くなるようあなたはいつもMicrosoft.VisualBasicへの参照を追加して、まったく同じ方法を使用することができ
Strings.Ascは、127のコード値を超える非ASCII文字のプレーンC#キャストと等価ではありません。私はこのような何かに 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アセンブリへの参照を追加したい場合。私はこれらの使用ReSharperのだ
、正確なコードは
/// <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"/> < 0 or > 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]);
}
リソースはちょうどので何とか、あなたがそれらを無視望む方法、そしてあなたが他の二つの方法が、エラーメッセージが保存されているマシンにVBで実行されますアクセスを持っていない以下の通りです:
internal static Encoding GetFileIOEncoding()
{
return Encoding.Default;
}
internal static int GetLocaleCodePage()
{
return Thread.CurrentThread.CurrentCulture.TextInfo.ANSICodePage;
}
//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 "]"
ようこそスタックオーバーフロー!このコードスニペットは問題を解決するかもしれませんが、[説明を含めて](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)本当にあなたの投稿の質を向上させるのに役立ちます。将来読者の質問に答えていることを覚えておいてください。そうした人々はあなたのコード提案の理由を知らないかもしれません。また、コードと説明の両方の可読性が低下するため、説明的なコメントを使用してコードを混乱させないようにしてください。 – FrankerZ
'Asc'と' Chr'はASCIIやISO 8859-1(あなたのコードがしているもの)さえも含んでいません。それらには 'Encoding.Default'が含まれます。 –
めったに誰もがこれらの日ASCII値について話をしないんのでご注意ください。通常、代わりにUnicodeコードポイント(またはそのUTF-16エンコード)を使用しています:http://www.joelonsoftware.com/articles/Unicode。html –
VB.Net Asc *はASCIIコードを返しません。*また、Unicodeコードを返しません。 It [returns](http://msdn.microsoft.com/en-us/library/zew1e4wc(v = vs.71).aspx)現在のWindowsコードページの "ANSI"コード。 – MarkJ
@ MarkJこれはVB6とまったく同じです。行くフィギュア! –