C# byte
最后修改于 2023 年 7 月 5 日
本文介绍如何在 C# 中使用 byte 类型。
byte 类型是 C# 中一种简单的、数值型的、值类型。 byte 类型主要用于 IO 操作,例如处理文件和网络连接。
有两种基本的 byte 类型
keyword range size .NET type sbyte -128 to 127 Signed 8-bit integer System.SByte byte 0 to 255 Unsigned 8-bit integer System.Byte
下表展示了两种 byte 类型的关键字、范围、大小和 .NET 类型。
C# byte 和 sbyte
在第一个例子中,我们展示了 byte 类型的基本属性。
byte val1 = 5;
sbyte val2 = -4;
Console.WriteLine(val1);
Console.WriteLine(val2);
Console.WriteLine(val1.GetType());
Console.WriteLine(val2.GetType());
Console.WriteLine("------------------------");
Console.WriteLine(byte.MinValue);
Console.WriteLine(byte.MaxValue);
Console.WriteLine(sbyte.MinValue);
Console.WriteLine(sbyte.MaxValue);
Console.WriteLine("------------------------");
Console.WriteLine(typeof(byte));
Console.WriteLine(typeof(sbyte));
Console.WriteLine(default(byte));
Console.WriteLine(default(sbyte));
该程序定义了 byte 和 sbyte 类型,并打印它们的属性。
Console.WriteLine(val1.GetType()); Console.WriteLine(val2.GetType());
我们可以使用 GetType 确定变量的类型。
Console.WriteLine(byte.MinValue); Console.WriteLine(byte.MaxValue);
最小值和最大值分别使用 MinValue 和 MaxValue 属性确定。
Console.WriteLine(typeof(byte)); Console.WriteLine(typeof(sbyte));
底层 .NET 类型使用 typeof 关键字确定。
Console.WriteLine(default(byte)); Console.WriteLine(default(sbyte));
类型的默认值使用 default 关键字返回。
$ dotnet run 5 -4 System.Byte System.SByte ------------------------ 0 255 -128 127 ------------------------ System.Byte System.SByte System.Byte 0 0
C# 将字符串转换为字节
我们使用 Encoding 将字符串转换为字节,反之亦然。
using System.Text;
string word = "čerešňa";
byte[] data = Encoding.UTF8.GetBytes(word);
Console.WriteLine(string.Join(" ", data));
string word2 = Encoding.UTF8.GetString(data);
Console.WriteLine(word2);
我们有一个包含三个带重音字母的单词。
byte[] data = Encoding.UTF8.GetBytes(word);
要将字符串转换为字节,我们使用 Encoding.UTF8.GetBytes。
string word2 = Encoding.UTF8.GetString(data);
要从字节数组中获取字符串,我们使用 Encoding.UTF8.GetString 方法。
$ dotnet run 196 141 101 114 101 197 161 197 136 97 čerešňa
这个词有七个字母。 在数组中,我们有十个字节。 这意味着三个带重音的字母各用两个字节表示。
C# 将字节写入文件
在下一个例子中,我们将一些数据写入一个文本文件。
public override void Write(byte[] buffer, int offset, int count);
FileStream.Write 方法将一个字节块写入文件流。 该方法接受三个参数:字节数组、从零开始的字节偏移量(从该偏移量开始将字节复制到流中)以及要写入的最大字节数。
using System.Text;
var path = "words.txt";
using FileStream fs = File.Create(path);
byte[] data = Encoding.UTF8.GetBytes("falcon\nribbon\ncloud\nwater");
fs.Write(data, 0, data.Length);
Console.WriteLine("data written to file");
该示例将四个单词写入文件。
using FileStream fs = File.Create(path);
我们使用 File.Create 创建一个新文件。 该方法返回一个 FileStream。
byte[] data = Encoding.UTF8.GetBytes("falcon\nribbon\ncloud\nwater");
我们使用 Encoding.UTF8.GetBytes 获取文本的字节。
fs.Write(data, 0, data.Length);
我们使用 Write 将整个字节数组写入文件流。
获取 HTML 页面
HttpClient.GetByteArrayAsync 向指定的 Uri 发送 GET 请求,并以异步操作的形式返回响应正文作为字节数组。
string url = "http://webcode.me"; HttpClient client = new HttpClient(); byte[] data = await client.GetByteArrayAsync(url); string fname = "index.html"; File.WriteAllBytes(fname, data);
我们使用 GetByteArrayAsync 将一个 HTML 页面检索到字节数组中。 然后我们使用 File.WriteAllBytes 将字节数组写入文件。
C# File.ReadAllBytes
File.ReadAllBytes 打开一个二进制文件,将文件的内容读取到字节数组中,然后关闭该文件。
var path = "favicon.ico";
byte[] data = File.ReadAllBytes(path);
int i = 0;
foreach (byte c in data)
{
Console.Write("{0:X2} ", c);
i++;
if (i % 10 == 0)
{
Console.WriteLine();
}
}
该示例读取一个 favicon.ico 二进制文件。 数据以十六进制格式打印到控制台。
$ dotnet run 00 00 01 00 01 00 10 10 00 00 00 00 00 00 68 05 00 00 16 00 00 00 28 00 00 00 10 00 00 00 20 00 00 00 01 00 08 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 FF FF FF 00 4D 45 3D 00 00 00 00 00 00 00 ...
HttpServer 示例
HTTP 服务器将其数据写入响应对象的输出流。
using System.Net;
using System.Text;
using var listener = new HttpListener();
listener.Prefixes.Add("https://:8001/");
listener.Start();
Console.WriteLine("Listening on port 8001...");
while (true)
{
HttpListenerContext context = listener.GetContext();
using HttpListenerResponse resp = context.Response;
resp.Headers.Set("Content-Type", "text/plain");
string data = "Hello there!";
byte[] buffer = Encoding.UTF8.GetBytes(data);
resp.ContentLength64 = buffer.Length;
using Stream ros = resp.OutputStream;
ros.Write(buffer, 0, buffer.Length);
}
C# 有 HttpListener 来创建简单的 HTTP 服务器。 我们的服务器发送一条文本消息。
using HttpListenerResponse resp = context.Response;
resp.Headers.Set("Content-Type", "text/plain");
在响应头中,我们将 Content-Type 设置为 text/plain 以提示客户端期望的数据类型。
string data = "Hello there!"; byte[] buffer = Encoding.UTF8.GetBytes(data);
我们将消息转换为字节。
resp.ContentLength64 = buffer.Length;
我们设置内容长度。
using Stream ros = resp.OutputStream; ros.Write(buffer, 0, buffer.Length);
最后,我们使用 Write 将字节写入输出流。
$ curl localhost:8001 Hello there!
来源
在本文中,我们使用了 C# 中的 byte 类型。
作者
列出所有 C# 教程。