C# 中將數(shù)值型數(shù)據(jù)轉(zhuǎn)換為字節(jié)數(shù)組的方法
private void Value_ByteArray()
{
double doublevalue = 258.0;
Int32 intvalue = 258;
string strValue = "258";
Int16 int16value = 258;
MemoryStream doublemem = new MemoryStream();
BinaryWriter doublebyteWR = new BinaryWriter(doublemem);
doublebyteWR.Write(doublevalue);
//doublemem.Close();
BinaryReader doubleReader = new BinaryReader(doublemem);
doublemem.Seek(0, SeekOrigin.Begin);
double heh = doubleReader.ReadDouble();//heh確實(shí)是257.0
MemoryStream intmem = new MemoryStream();
BinaryWriter intbyteWR = new BinaryWriter(intmem);
intbyteWR.Write(intvalue);
MemoryStream strmem = new MemoryStream();
BinaryWriter strbyteWR = new BinaryWriter(strmem);
strbyteWR.Write(strValue);
MemoryStream int16mem = new MemoryStream();
BinaryWriter int16byteWR = new BinaryWriter(int16mem);
int16byteWR.Write(int16value);
byte[] bufferdouble = doublemem.ToArray();
byte[] bufferint = intmem.ToArray();
byte[] bufferint16 = int16mem.ToArray();
byte[] bufferstr = strmem.ToArray();
}
程序結(jié)果:
double =258.0 ;8位——0 0 0 0 0 32 112 64 字節(jié)數(shù)組
Int32 = 258 ; 4——2 1 0 0
string ="258" ; 4——3 50 53 55
Int16 = 258 ; 2——2 1
Int16[] ={0,258,1} ;0 0 2 1 1 0
通過上面的例子,可以看出由數(shù)值型轉(zhuǎn)換為字節(jié)數(shù)組后,程序是倒序讀入的,但是讀取的時(shí)候,VS會(huì)自動(dòng)反轉(zhuǎn)過來(lái)
Int16[] int16Array =new Int16[3]{0,258,1} ;
MemoryStream int16mem = new MemoryStream();
BinaryWriter int16byteWR = new BinaryWriter(int16mem);
foreach (Int16 hehe in int16Array)
{
int16byteWR.Write(hehe);
}
byte[] bufferint16 = int16mem.ToArray();//0 0 2 1 1 0
BinaryReader int16Reader = new BinaryReader(int16mem);
int16mem.Seek(0, SeekOrigin.Begin);
Int16[] int16Arraynew=new Int16[3];
for (int i = 0; i < int16mem.Length/2; i ++)
{
int16Arraynew[i] = int16Reader.ReadInt16();//[0,258,1]
}
- 總結(jié)C#刪除字符串?dāng)?shù)組中空字符串的幾種方法
- C# 字符串string和內(nèi)存流MemoryStream及比特?cái)?shù)組byte[]之間相互轉(zhuǎn)換
- C#字符串?dāng)?shù)組轉(zhuǎn)換為整形數(shù)組的方法
- C#實(shí)現(xiàn)字符串轉(zhuǎn)換成字節(jié)數(shù)組的簡(jiǎn)單實(shí)現(xiàn)方法
- asp.net(c#) 使用Rex正則來(lái)生成字符串?dāng)?shù)組的代碼
- C# 16進(jìn)制與字符串、字節(jié)數(shù)組之間的轉(zhuǎn)換
- C#將hashtable值轉(zhuǎn)換到數(shù)組中的方法
- C#將數(shù)字轉(zhuǎn)換成字節(jié)數(shù)組的方法
- C#將字節(jié)數(shù)組轉(zhuǎn)換成數(shù)字的方法
- C#實(shí)現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法
相關(guān)文章
基于WPF實(shí)現(xiàn)Message消息提醒控件
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)Meesage消息提醒控件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2023-07-07
深入U(xiǎn)nix時(shí)間戳與C# DateTime時(shí)間類型互換的詳解
本篇文章是對(duì)Unix時(shí)間戳與C# DateTime時(shí)間類型互換進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
基于WPF實(shí)現(xiàn)經(jīng)典紙牌游戲
這篇文章主要為大家詳細(xì)介紹了如何溧陽(yáng)WPF實(shí)現(xiàn)經(jīng)典紙牌游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)WPF有一定的幫助,需要的可以參考一下2023-02-02
C#通過Win32API設(shè)置客戶端系統(tǒng)時(shí)間的方法詳解
在日常工作中,有時(shí)可能會(huì)需要獲取或修改客戶端電腦的系統(tǒng)時(shí)間,比如軟件設(shè)置了Licence有效期,本文以一個(gè)簡(jiǎn)單的小例子,簡(jiǎn)述如何通過C#獲取和設(shè)置客戶端電腦的系統(tǒng)時(shí)間,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正,需要的朋友可以參考下2024-06-06

