日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

C# 基本數(shù)據(jù)類型與字節(jié)流轉(zhuǎn)換

 羊玉wngbx 2019-06-22

聯(lián)網(wǎng)游戲的信息傳輸都是以字節(jié)流(字節(jié)數(shù)組)形式傳輸數(shù)據(jù),本文展示基本數(shù)據(jù)類型與字節(jié)流互相轉(zhuǎn)換

girl模型有四個信息,GetByteNets方法將信息轉(zhuǎn)化為字節(jié)流,GetGirlFormNets()方法展示如何將字節(jié)流轉(zhuǎn)化為信息

  1. using System;
  2. public class Girl
  3. {
  4. byte sex;
  5. short height;
  6. float weight;
  7. int age;
  8. public byte[] GetByteNets() //將這個對象轉(zhuǎn)化為字節(jié)流
  9. {
  10. //表示偏移量
  11. int offset = 0;
  12. //輸出的字節(jié)流
  13. byte[] result= new byte[11];
  14. //第一個字節(jié)放入數(shù)組
  15. result[0] = sex;
  16. offset++;
  17. //將身高參數(shù)轉(zhuǎn)化為字節(jié)流(short轉(zhuǎn)字節(jié)流)
  18. byte[] heightBytes = BitConverter.GetBytes(this.height);
  19. //字節(jié)流拷貝(源字節(jié)流,從第0個開始拷貝,目標字節(jié)流,拷貝到目標字節(jié)流第offset個,拷貝長度為原字節(jié)流長度)
  20. Buffer.BlockCopy(heightBytes,0,result,offset,heightBytes.Length);
  21. offset += heightBytes.Length;
  22. //將體重參數(shù)轉(zhuǎn)化為字節(jié)流
  23. byte[] weightBytes = BitConverter.GetBytes(this.weight);
  24. //字節(jié)流拷貝
  25. Buffer.BlockCopy(weightBytes, 0, result, offset, weightBytes.Length);
  26. offset += weightBytes.Length;
  27. //將年齡轉(zhuǎn)化為字節(jié)流
  28. byte[] ageBytes = BitConverter.GetBytes(this.age);
  29. //字節(jié)流拷貝
  30. Buffer.BlockCopy(ageBytes, 0, result, offset, ageBytes.Length);
  31. offset += ageBytes.Length;
  32. return result;
  33. }
  34. public Girl GetGirlFromNet(byte[] buffer) //輸入字節(jié)流 輸出對象
  35. {
  36. Girl result = new Girl();
  37. result.sex = buffer[0];
  38. result.height = BitConverter.ToInt16(buffer, 1);
  39. result.weight = BitConverter.ToSingle(buffer, 3);
  40. result.age = BitConverter.ToInt32(buffer, 7);
  41. return result;
  42. }
  43. }

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多