c# 整數類型轉byte
Here is the list of the built-in integral types of data types in C#, sbyte, byte, char, short, ushort, int, uint, long and ulong
這是C#, sbyte , byte , char , short , ushort , int , int , uint , long和ulong中數據類型的內置整數類型的列表
C#數據類型的積分類型 (C# Integral types of Data types)
Type | System type | Size (in bits) | Value Range | Value type |
---|---|---|---|---|
sbyte | System.SByte | 8-bits | -128 to 127 | Signed integer |
byte | System.Byte | 8-bits | 0 to 255 | Unsigned integer |
char | System.Char | 16-bits | U+0000 to U+ffff | Unicode character |
short | System.Int16 | 16-bits | -32,768 to 32,767 | Signed integer |
ushort | System.Int16 | 16-bits | 0 to 65,535 | Unsigned integer |
int | System.Int32 | 32-bits | -2,147,483,648 to 2,147,483,647 | Signed integer |
uint | System.Int32 | 32-bits | 0 to 4,294,967,295 | Unsigned integer |
long | System.Int64 | 64-bits | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | Signed integer |
ulong | System.Int64 | 64-bits | 0 to 18,446,744,073,709,551,615 | Unsigned integer |
類型 | 系統類型 | 大小(以位為單位) | 取值范圍 | 值類型 |
---|---|---|---|---|
兆字節 | 系統字節 | 8位 | -128至127 | 有符號整數 |
字節 | 系統字節 | 8位 | 0至255 | 無符號整數 |
燒焦 | 系統字符 | 16位 | U + 0000至U + ffff | Unicode字符 |
短 | System.Int16 | 16位 | -32,768至32,767 | 有符號整數 |
超短 | System.Int16 | 16位 | 0至65,535 | 無符號整數 |
整型 | System.Int32 | 32位 | -2,147,483,648至2,147,483,647 | 有符號整數 |
int | System.Int32 | 32位 | 0至4,294,967,295 | 無符號整數 |
長 | System.Int64 | 64位 | -9,223,372,036,854,775,808至9,223,372,036,854,775,807 | 有符號整數 |
烏龍 | System.Int64 | 64位 | 0至18,446,744,073,709,551,615 | 無符號整數 |
Example:
例:
In this example, we are declaring variables of different integral types of data types, initializing with the different value, printing the system types of the variables, size of the types and min, max values of the types.
在此示例中,我們聲明了不同整數類型的數據類型的變量,使用不同的值進行初始化,打印了變量的系統類型,類型的大小以及類型的最小值,最大值。
using System;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
//sbyte
sbyte a = -10;
Console.WriteLine("sbyte...");
Console.WriteLine("a = " + a);
Console.WriteLine("type of variable = " + a.GetType());
Console.WriteLine("size of sbyte = " + sizeof(sbyte));
Console.WriteLine("Min value of sbyte = " + sbyte.MinValue);
Console.WriteLine("Max value of sbyte = " + sbyte.MaxValue);
Console.WriteLine();
//byte
byte b = 10;
Console.WriteLine("byte...");
Console.WriteLine("b = " + b);
Console.WriteLine("type of variable = " + b.GetType());
Console.WriteLine("size of byte = " + sizeof(byte));
Console.WriteLine("Min value of byte = " + byte.MinValue);
Console.WriteLine("Max value of byte = " + byte.MaxValue);
Console.WriteLine();
//char
char c = 'P';
Console.WriteLine("char...");
Console.WriteLine("c = " + c);
Console.WriteLine("type of variable = " + c.GetType());
Console.WriteLine("size of char = " + sizeof(char));
Console.WriteLine("Min value of char = " + (int)(char.MinValue));
Console.WriteLine("Max value of char = " + (int)(char.MaxValue));
Console.WriteLine();
//short
short d = -18910;
Console.WriteLine("short...");
Console.WriteLine("d = " + d);
Console.WriteLine("type of variable = " + d.GetType());
Console.WriteLine("size of short = " + sizeof(short));
Console.WriteLine("Min value of short = " + short.MinValue);
Console.WriteLine("Max value of short = " + short.MaxValue);
Console.WriteLine();
//ushort
ushort e = 18910;
Console.WriteLine("ushort...");
Console.WriteLine("e = " + e);
Console.WriteLine("type of variable = " + e.GetType());
Console.WriteLine("size of ushort = " + sizeof(short));
Console.WriteLine("Min value of ushort = " + ushort.MinValue);
Console.WriteLine("Max value of ushort = " + ushort.MaxValue);
Console.WriteLine();
//int
int f = -893818910;
Console.WriteLine("int...");
Console.WriteLine("f = " + f);
Console.WriteLine("type of variable = " + f.GetType());
Console.WriteLine("size of int = " + sizeof(int));
Console.WriteLine("Min value of int = " + int.MinValue);
Console.WriteLine("Max value of int = " + int.MaxValue);
Console.WriteLine();
//uint
int g = 893818910;
Console.WriteLine("uint...");
Console.WriteLine("g = " + g);
Console.WriteLine("type of variable = " + g.GetType());
Console.WriteLine("size of uint = " + sizeof(uint));
Console.WriteLine("Min value of uint = " + uint.MinValue);
Console.WriteLine("Max value of uint = " + uint.MaxValue);
Console.WriteLine();
//long
long h = -90909893818910;
Console.WriteLine("long...");
Console.WriteLine("h = " + h);
Console.WriteLine("type of variable = " + h.GetType());
Console.WriteLine("size of long = " + sizeof(long));
Console.WriteLine("Min value of long = " + long.MinValue);
Console.WriteLine("Max value of long = " + long.MaxValue);
Console.WriteLine();
//ulong
ulong i = 90909893818910;
Console.WriteLine("ulong...");
Console.WriteLine("i = " + i);
Console.WriteLine("type of variable = " + i.GetType());
Console.WriteLine("size of ulong = " + sizeof(ulong));
Console.WriteLine("Min value of ulong = " + ulong.MinValue);
Console.WriteLine("Max value of ulong = " + ulong.MaxValue);
Console.WriteLine();
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output
輸出量
sbyte...
a = -10
type of variable = System.SByte
size of sbyte = 1
Min value of sbyte = -128
Max value of sbyte = 127
byte...
b = 10
type of variable = System.Byte
size of byte = 1
Min value of byte = 0
Max value of byte = 255
char...
c = P
type of variable = System.Char
size of char = 2
Min value of char = 0
Max value of char = 65535
short...
d = -18910
type of variable = System.Int16
size of short = 2
Min value of short = -32768
Max value of short = 32767
ushort...
e = 18910
type of variable = System.UInt16
size of ushort = 2
Min value of ushort = 0
Max value of ushort = 65535
int...
f = -893818910
type of variable = System.Int32
size of int = 4
Min value of int = -2147483648
Max value of int = 2147483647
uint...
g = 893818910
type of variable = System.Int32
size of uint = 4
Min value of uint = 0
Max value of uint = 4294967295
long...
h = -90909893818910
type of variable = System.Int64
size of long = 8
Min value of long = -9223372036854775808
Max value of long = 9223372036854775807
ulong...
i = 90909893818910
type of variable = System.UInt64
size of ulong = 8
Min value of ulong = 0
Max value of ulong = 18446744073709551615
Ref: Integral types table
參考: 整體類型表
翻譯自: https://www.includehelp.com/dot-net/integral-types-of-data-types-in-c-sharp.aspx
c# 整數類型轉byte