生活随笔
收集整理的這篇文章主要介紹了
C# 中的char 和 byte
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在C#中,char代表一個Unicode的字符,占用的內存不是一個byte。而byte還是一個字節。
那么在char和byte之間copy操作時,可能結果不是我們想要的。
示例:
static void Main(
string[] args){
byte[] bytes = {
0x31,
0x32,
0x33,
0x34};
char[] chars1 =
new char[
4];
char[] chars2 =
new char[
4];System.Buffer.BlockCopy(bytes,
0,chars1,
0,
4);
for (
int i =
0; i<
4; i++){chars2[i] = (
char)bytes[i];}
string str1 =
new string(chars1);
string str2 =
new string(chars2); System.Console.Read();}
執行時的內存情況:
using System.Globalization;
using System.Runtime;
using System.Runtime.InteropServices;namespace System
{[ComVisible(
true)]
public struct Char : IComparable, IConvertible, IComparable<Char>, IEquatable<Char>{
public const Char MaxValue =
'\uffff';
public const Char MinValue =
'\0';
public static string ConvertFromUtf32(
int utf32);
public static int ConvertToUtf32(Char highSurrogate, Char lowSurrogate);}
}
[ComVisible(
true)]
public struct Byte : IComparable, IFormattable, IConvertible, IComparable<Byte>, IEquatable<Byte>{
public const Byte MaxValue =
255;
public const Byte MinValue =
0;}
}
總結
以上是生活随笔為你收集整理的C# 中的char 和 byte的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。