基礎(chǔ)數(shù)據(jù)與Byte數(shù)據(jù)的轉(zhuǎn)換在Socket通訊中用的非常的多.我想任何Game都不大會希望直接用XML和Json字符串直接進行數(shù)據(jù)傳遞,而是在Client端和Server端對基礎(chǔ)數(shù)據(jù)進行解析.當然,如果你執(zhí)行用"Encoding.UTF8.GetBytes"之類的話,我也沒有辦法.好了,進入正題:在C#中要進行基礎(chǔ)數(shù)據(jù)和Byte的轉(zhuǎn)換要用到:"BitConverter"類
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),吉林企業(yè)網(wǎng)站建設(shè),吉林品牌網(wǎng)站建設(shè),網(wǎng)站定制,吉林網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,吉林網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。這里我用了一個實例:
BitConverToByte : 將基礎(chǔ)類型變成Bytes[]
BitBitConverterTest : 將Bytes[]解析成基礎(chǔ)類型
另外:簡易的模仿一下Socket通訊協(xié)議(注意:只是簡易,真正用在Socket里面,需要另外加協(xié)議號等等):
包頭只有一個 int32類型用于包體的length
包體:
[
int32 : 內(nèi)容的長度
string : 內(nèi)容
]
int32 , 這里故意加的(免得成光棍內(nèi)容)( 在實際Socket中 , 你可以表示年齡啥的!!!)
代碼如下:
BitConverToByte :
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BitBitConverterTest.ainy { /// <summary> /// 將內(nèi)容變成規(guī)則的二進制數(shù)據(jù),以便發(fā)送 /// </summary> public class BitConverToByte { private readonly string context; private readonly Int32 mark; public BitConverToByte( string context , Int32 mark ) { this.context = context; this.mark = mark; } /// <summary> /// 返回二進制數(shù)組 /// </summary> /// <returns> int32長度 + context + mark</returns> public byte[] GetContextBytes() { // 內(nèi)容的二進制 byte[] myContext = Encoding.UTF8.GetBytes(this.context); // 標示的二進制( 測試BitConverter ) byte[] myMark = BitConverter.GetBytes(this.mark); // 關(guān)于內(nèi)容長度 myContext + myMark byte[] myBytesLenght = BitConverter.GetBytes(myContext.Length + myMark.Length); // 關(guān)于內(nèi)容的長度 byte[] myContextLength = BitConverter.GetBytes(myContext.Length); byte[] reslut = new byte[myContext.Length + myContextLength.Length + myMark.Length + myBytesLenght.Length]; myBytesLenght.CopyTo(reslut, 0); myContextLength.CopyTo(reslut, myBytesLenght.Length); myContext.CopyTo(reslut, myBytesLenght.Length + myContextLength.Length); myMark.CopyTo(reslut, myBytesLenght.Length + myContext.Length + myContextLength.Length); return reslut; } } }
BitBitConverterTest :
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BitBitConverterTest.ainy { /// <summary> /// 將二進制轉(zhuǎn)化為內(nèi)容 /// </summary> public class BitConverToContext { //整個二進制數(shù)據(jù) private readonly byte[] target; //文本二進制(String)數(shù)據(jù) public string Context { get ; private set; } //標記數(shù)據(jù) public Int32 Mark { get; private set; } //文本 "I Love U"的二進制數(shù)據(jù) public byte[] ContextBytes { get; private set; } public BitConverToContext( byte[] target ) { this.target = target; this.AnalysisFromBytes(); } /// <summary> /// 解析Byte[] /// int + [string的length + string] + int /// </summary> private void AnalysisFromBytes() { // 得到文本的總長度 : ( 文本長度 + Context.Length + Mark.Length ) Int32 contextLength = BitConverter.ToInt32(this.target, 0); // 得到文本的長度 Int32 len = BitConverter.ToInt32(this.target, 4); // 獲取文本 this.Context = BitConverter.ToString(this.target, 8, len); // 獲取后面的Mark this.Mark = BitConverter.ToInt32(this.target, 8 + len); this.ContextBytes = this.target.Skip(8).Take(len).ToArray(); } } }
測試:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BitBitConverterTest.ainy; namespace BitBitConverterTest { class Program { static void Main(string[] args) { BitConverToByte bitConverToByte = new BitConverToByte("I Love U", 1314); BitConverToContext bitConverToContext = new BitConverToContext(bitConverToByte.GetContextBytes()); Console.WriteLine("內(nèi)容二進制:{0} , 內(nèi)容:{1} , 標記:{2}", bitConverToContext.Context, Encoding.UTF8.GetString(bitConverToContext.ContextBytes), bitConverToContext.Mark); Console.ReadKey(); } } }
結(jié)果:
附件:http://down.51cto.com/data/2367312另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當前名稱:C#基礎(chǔ)數(shù)據(jù)與Byte-創(chuàng)新互聯(lián)
鏈接地址:http://muchs.cn/article12/dchcdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、網(wǎng)站維護、域名注冊、全網(wǎng)營銷推廣、企業(yè)網(wǎng)站制作、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容