簡體   English   中英

有沒有辦法在不使用數字的情況下將 Char 轉換為 Int?

[英]Is there a way to convert Char into Int without using numbers?

標題說明了一切。 我正在嘗試在 Visual Studio 中將 char 轉換為 int。

我已經試過了:

int a;
a = (int)x;
System.Console.WriteLine(a);

但除此之外它沒有給我任何東西(通過嘗試理解代碼):114117105

這將起作用:

//a char is a 16-bit numerical value
//usually used for the representation of characters
//here I assign 'a' to it; keep in mind; 'a' has also a numeric representation
//i.e.: 97
char x = 'a';

int a;

//the `int a` is an integer and, through a cast, receives the numeric value
//besides the bit-width (debatable) the data contents are the same.
//If we assign it to an `int` only the "purpose" c.q. representation will change
a = (int)x;

//since we put in an `int` a number will be shown (because of it's purpose)
//if we put in x, then `a` will be shown.
System.Console.WriteLine(a);

輸出

97

正如你現在所了解的那樣; 一個string ,是一個char array 因此,字符串很難用單個數字表示,因為它是二維的。

這就像說,將: 0,4,43434,878728,3477,3.14159265轉換為單個數字。

https://dotnetfiddle.net/qSYUdP

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/char

為什么輸出的a97 ; 您可以在字符表中查找它,例如: ascii

請注意,輸出的實際字符由所選字體/字符表決定。 對於大多數字體,已實現 ASCII,但不能保證。 所以, 97也不會總是產生a

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM