I think it confuses people that single characters are interchangeable with byte and int, so here I am telling you that they are interchangeable up to an ASCII limit of 128 characters. This means that you have 0 to 127 positive integers (characters) you can play with. Check out the table here.

One byte is 8 bits which is 2^8 which yields 256.

A signed integer (int) is 4 bytes long (32 bit) which is 2^32/2 – 1 which yields 2,147,483,647.

I am not saying: char == byte == int, but I am saying that they are interchangeable if used correctly. You can however say that a char < byte < int. In other words a char will fit in a byte or an int, and a byte will fit in an int, but not the reverse of that statement.

Here is a code example of converting a string to a List of int:

string temp = "ABC";

char[] arr = temp.ToCharArray();

List<int> lst = new List<int>();

foreach (char c in arr)
 lst.Add(c);

for (int i = 0; i < lst.Count; i++)
{
 Console.WriteLine("[{0}] = [{1}]", arr[i], lst[i]);
}

As you can see in the code above I am taking a character and storing it into a list of int. That is possible people of what I mentioned above. A character can fit into a int, and an integer up to the value of 127 can fit into a char.

This is particularly useful when you are trying to hash a string, serialize a string over a serial port or UART or do a number of other things.

Leave a Reply

Your email address will not be published. Required fields are marked *