基礎(chǔ)數(shù)據(jù)與Byte數(shù)據(jù)的轉(zhuǎn)換在Socket通訊中用的非常的多.我想任何Game都不大會(huì)希望直接用XML和Json字符串直接進(jìn)行數(shù)據(jù)傳遞,而是在Client端和Server端對基礎(chǔ)數(shù)據(jù)進(jìn)行解析.當(dāng)然,如果你執(zhí)行用"Encoding.UTF8.GetBytes"之類的話,我也沒有辦法.
好了,進(jìn)入正題:在C#中要進(jìn)行基礎(chǔ)數(shù)據(jù)和Byte的轉(zhuǎn)換要用到:"BitConverter"類

這里我用了一個(gè)實(shí)例:
BitConverToByte : 將基礎(chǔ)類型變成Bytes[]
BitBitConverterTest : 將Bytes[]解析成基礎(chǔ)類型
另外:簡易的模仿一下Socket通訊協(xié)議(注意:只是簡易,真正用在Socket里面,需要另外加協(xié)議號(hào)等等):
包頭只有一個(gè) int32類型用于包體的length
包體:
[
int32 : 內(nèi)容的長度
string : 內(nèi)容
]
int32 , 這里故意加的(免得成光棍內(nèi)容)( 在實(shí)際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ī)則的二進(jìn)制數(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>
/// 返回二進(jìn)制數(shù)組
/// </summary>
/// <returns> int32長度 + context + mark</returns>
public byte[] GetContextBytes()
{
// 內(nèi)容的二進(jìn)制
byte[] myContext = Encoding.UTF8.GetBytes(this.context);
// 標(biāo)示的二進(jìn)制( 測試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>
/// 將二進(jìn)制轉(zhuǎn)化為內(nèi)容
/// </summary>
public class BitConverToContext
{
//整個(gè)二進(jìn)制數(shù)據(jù)
private readonly byte[] target;
//文本二進(jìn)制(String)數(shù)據(jù)
public string Context { get ; private set; }
//標(biāo)記數(shù)據(jù)
public Int32 Mark { get; private set; }
//文本 "I Love U"的二進(jìn)制數(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)容二進(jìn)制:{0} , 內(nèi)容:{1} , 標(biāo)記:{2}", bitConverToContext.Context, Encoding.UTF8.GetString(bitConverToContext.ContextBytes), bitConverToContext.Mark);
Console.ReadKey();
}
}
}結(jié)果:

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前名稱:C#基礎(chǔ)數(shù)據(jù)與Byte-創(chuàng)新互聯(lián)
鏈接地址:http://www.chinadenli.net/article12/dchcdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、網(wǎng)站維護(hù)、域名注冊、全網(wǎng)營銷推廣、企業(yè)網(wǎng)站制作、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容