This article is derived from Get Common SubString from two Strings, we need to get ASCII value.
I came from science, I have never had a chance to learn or play a game such as an algorithm in my student career, although I like logic thinking, I like the algorithm game, but I missed the age period to play this kind of game. Most algorithm problems I met are in interviews. In my view, these kinds of tricks are just boby level games, but if you did not have related training previously, you might not get the solution immediately. So, I will open a topic, or series, to record the algorithm questions I met or I played previously. The first one will be this. Please do not laugh at me to play this pupil question, you can think those also meant BASIC.
This series will include,
I will add more on this topic probably from my notes or practice code.
Introduction
This is the structure of this article,
- Introduction
- A - What is ASCII
- B - Get String ASCII Value in C#
A - What is ASCII
ASCII Table - Standard and Extended ASCII Chart, ASCII Codes
- ASCII --- stands for American Standard Code for Information Interchange.
- Standard ASCII codes --- range from 0 to 127 in Decimal or 00 to 7F in Hexadecimal
- ASCII Control Characters --- range from 0 to 31 in Decimal or 00 to 1F in Hexadecimal
- ASCII Printable Characters --- range from 32 to 127 in Decimal or 20 to 7F in Hexadecimal
- Extended ASCII codes --- range from 128 to 255 in Decimal or 80 to FF in Hexadecimal
It ranges from 0 to 255 in Decimal or 00 to FF in Hexadecimal. ASCII codes can be divided into two sets - Standard ASCII codes and Extended ASCII codes. Standard ASCII codes range from 0 to 127 in Decimal or 00 to 7F in Hexadecimal, they are mainly used for representing characters, such as characters "a" to "z" and number "0" to "9", these are called printable characters, note that code 0 to 31 (Decimal) in Standard ASCII are not printable, they are assigned for control characters that are used to control some peripheral devices such as printers, for example, 12 represents the Form Feed / New Page function. This command instructs a printer to skip to the top of the next page. The Standard ASCII chart below illustrates the details of the codes. Extended ASCII codes range from 128 to 255 in Decimal or 80 to FF in Hexadecimal. They meet the demand for more characters and symbols that are used in many languages. Together with the control codes (0 to 31) in the Standard ASCII table, Extended ASCII codes are also widely used in the communication protocol, such as RS-232, RS-485, RS-422, and TTL systems. The Extended ASCII chart below illustrates the details of the codes.
B - Get String ASCII Value in C#
The basic point is that we need to convert the char to int or byte, such as,
var s = "This is a string";
c = s[0];
var ascii_c1 = (int) c; // one value of int
var ascii_c2 = (byte) c; // one value of int
var ascii1_s1 = s.Select(x => (int) x); // series value of int
var ascii_s2 = Encoding.ASCII.GetBytes(s); // series value of int
The following code will demonstrate this,
public static void Main(string[] args) {
var s = "This is a string";
foreach(char c in s)
Console.Write((byte) c + " "); // print num
Console.WriteLine();
foreach(char c in s)
Console.Write((int) c + " "); // print num
Console.WriteLine();
for (int i = 0; i < s.Length; i++) Console.Write((int) s[i] + " "); // print num
Console.WriteLine();
var ascii1 = s.Select(x => (int) x);
for (int i = 0; i < s.Length; i++) Console.Write(ascii1.ElementAt(i) + " "); // print num
Console.WriteLine();
var ascii = Encoding.ASCII.GetBytes(s);
byte[] ascii2 = Encoding.ASCII.GetBytes(s);
for (int i = 0; i < s.Length; i++) Console.Write(ascii2[i] + " "); // print num
Console.WriteLine();
for (int i = 0; i < s.Length; i++) {
var s1 = s[i];
Console.Write(s1 + " "); // Character
}
Console.WriteLine();
for (int i = 0; i < s.Length; i++) Console.Write(s[i] + " "); // Character
Console.WriteLine();
var ascii3 = s.ToCharArray();
for (int i = 0; i < s.Length; i++) Console.Write(ascii3.ElementAt(i) + " "); // Character
Console.WriteLine();
for (int i = 0; i < s.Length; i++) Console.Write(ascii3[i] + " "); // Character
Console.ReadLine();
}
}
The result is,
Reference