對象、字節流轉換

數據表示時間   長度(字節)   數據類型   描述及要求
平臺登入時間   ??6        ? BYTE[6] ? ? ?(每個字節分別代表:年、月、日、時、分、秒)
登入流水號 ? ? ? ? ? 2        ? WORD    ?每登入一次,登入流水號自動加1
平臺用戶名   ?? 12      ? ? ? STRING   ??平臺登入用戶名
平臺密碼     20 ? ? ? ? ? ? ? ? ? ? STRING  ?平臺登入密碼
加密規則 ? ? ? ? ? ? 1       ?? BYTE ? ? ? ? ?0x01:數據不加密;0x02:RSA加密;0x03:AES128加密...

?

//平臺登入類
public class EvPlatformLoginMsg
{public DateTime LoginTime{get;set;}public ushort SerialNumver{get;set;}public string Account{get;set}public string Password{get;set;}public EvEncryptionMode EncryptMode{get;set;}public EvPlatformLoginMsg(byte[] bytes, int offset){EvDateTime evDateTime = new EvDateTime(bytes, offset);LoginTime = evDateTime.DateTime;offset += evDateTime.BytesSize;NetBytesUtil.EnsureNetOrder(bytes, index, 2);SerialNumver = BitConverter.ToUint16(bytes, index);offset += 2;Account = Encoding.ASCII.GetString(bytes, index, 12).TrimEnd('\0');offset += 12;Password = Encoding.ASCII.GetString(bytes, index, 20);offset += 20;EncryptMode = (EvEncryptionMode)bytes[index];}public byte[] ToBytes(){byte[] bytes = new byte[41];int offset = 0;offset += new EvDateTime(LoginTime).WriteToBytes(bytes, offset);BitExtend.WriteUint16ToBytes(bytes, offset, SerialNumver);NetBytesUtil.EnsureNetOrder(bytes, index, 2);index += 2;byte[] accountBytes = Encoding.ASCII.GetBytes(Account);Buffer.BlockCopy(accountBytes, 0, bytes, index, accountBytes.Length > 12 ? 12 : accountBytes.Length);index += 12;byte[] passwordBytes = Encoding.ASCII.GetBytes(Password);Buffer.BlockCopy(passwordBytes, 0, bytes, index, passwordBytes.Length > 20 ? 20 : passwordBytes.Length);index += 20;bytes[index] = (byte)EncryptMode;return bytes;}
}//時間類型
public class EvDateTime
{private const int ConstBytesSize = 6;    //6個字節表示時間public DateTime DateTime {get;set;}public EvDateTime(DateTime dateTime){DateTime = dateTime;}public int BytesSize{get{return ConstBytesSize;}set{;}}public EvDateTime(byte[] bytes, int offset){int year = byte[offset] + 2000;int month = byte[offset + 1];int day = byte[offset + 2];int hour = byte[offset + 3];int miniute = byte[offset + 4];int second = byte[offset + 5];DateTime = new DateTime(year, month, day, hour, miniute, second);}public int WriteToBytes(byte[] bytes, int offset){bytes[offset] = (byte)(DateTime.Year - 2000);bytes[offset + 1] = (byte)DateTime.Month;bytes[offset + 2] = (byte)DateTime.Day;bytes[offset + 3] = (byte)DateTime.Hour;bytes[offset + 4] = (byte)DateTime.Minute;bytes[offset + 5] = (byte)DateTime.Second;return ConstBytesSize;}
}//BitExtend類
public class BitExtend
{public static unsafe void WriteCharToBytes(byte[] bytes, int start, char value){WriteInt16ToBytes(bytes, start, (short)val);}public static unsafe void WriteSingleToBytes(byte[] bytes, int start, float value){WriteInt32ToBytes(bytes, start, *((int*)&value));}public static unsafe void WriteDoubleToBytes(byte[] bytes, int start, double value){WriteInt64ToBytes(bytes, start, *((long*)&value));}public static unsafe void WriteBooleanToBytes(byte[] bytes, int start, boolean value){bytes[start] = value ? (byte)1 : (byte)0;}public static unsafe void WriteUint16ToBytes(byte[] bytes, int start, ushort value){WriteInt16ToBytes(bytes, start, (short)value);}public static unsafe void WriteUint32ToBytes(byte[] bytes, int start, uint value){WriteInt32ToBytes(bytes, start, (short)value);}public static unsafe void WriteUint64ToBytes(byte[] bytes, int start, ulong value){WriteInt64ToBytes(bytes, start, (long)value);}public static unsafe void WriteInt32ToBytes(byte[] bytes, int start, int value){fixed(byte* numRef = bytes){*((int*)(numRef = start)) = value;}}public static unsafe void WriteInt16ToBytes(byte[] bytes, int start, short value){fixed (byte* numRef = bytes){*((short*)(numRef + start)) = value;}}public static unsafe void WriteInt32ToBytes(byte[] bytes, int start, int value){fixed(byte* numRef = bytes){*((int*)(numRef + start)) = value;}}public static unsafe void WriteInt64ToBytes(byte[] bytes, int start, long value){fixed(long* numRef = bytes) {*((long*)(numRef + start)) = value;}}
}//網絡序和本機序
public static class NetBytesUtil
{public static int[] Byte2Bits(byte b){int[] bitArray = new int[8];bitArray[0] = b & 1;bitArray[1] = b & 2;bitArray[2] = b & 4;bitArray[3] = b & 8;bitArray[4] = b & 16;bitArray[5] = b & 32;bitArray[6] = b & 64;bitArray[7] = b & 128;return bitArray;}public static bool TestBit(long src, byte bitIndex){if (src == 0){return false;}else{return ((ulong)src & (ulong)Math.Pow(2, bitIndex)) == (ulong)Math.Pow(2, bitIndex);}}public static long SetBit(long src, byte bitIndex){return src | (long)Math.Pow(2, bitIndex);}public static void EnsureNetOrder(byte[] bytes, int start, int size){if (BitConverter.IsLittleEndian){if (bytes == null){throw new Exception("bytes is null");}if (start < 0){throw new Exception("start less than 0");}if (size < 0){throw new Exception("size less than 0");}if (bytes.Length < start + size){throw new Exception("bytes length less then that start plus size");}if (size > 1){byte tmp;for (int i = 0; i < size / 2; i++){tmp = bytes[start + i];bytes[start + i] = bytes[start + size - 1 - i];bytes[start + size - 1 - i] = tmp;}}}}public static void EnsureHostOrder(byte[] bytes, int start, int size){if (!BitConverter.IsLittleEndian){if (bytes == null){throw new Exception("bytes is null");}if (start < 0){throw new Exception("start less than 0");}if (size < 0){throw new Exception("size less than 0");}if (bytes.Length < start + size){throw new Exception("bytes length less then that start plus size");}if (size > 1){byte tmp;for (int i = 0; i < size / 2; i++){tmp = bytes[start + i];bytes[start + i] = bytes[start + size - 1 - i];bytes[start + size - 1 - i] = tmp;}}}}public static void WriteIp(byte[] bytes, int start, string ip){if (bytes == null){throw new Exception("bytes is null");}if (start < 0){throw new Exception("start less than 0");}if (bytes.Length < start + 4){throw new Exception("bytes length less then that start plus size");}if (string.IsNullOrEmpty(ip)){throw new Exception("ip is null or empty");}string[] array = ip.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);if (array.Length != 4){throw new Exception("ip format is error");}bytes[start] = byte.Parse(array[0]);bytes[start + 1] = byte.Parse(array[1]);bytes[start + 2] = byte.Parse(array[2]);bytes[start + 3] = byte.Parse(array[3]);}public static string ReadIp(byte[] bytes, int start){if (bytes == null){throw new Exception("bytes is null");}if (start < 0){throw new Exception("start less than 0");}if (bytes.Length < start + 4){throw new Exception("bytes length less then that start plus size");}return string.Format("{0}.{1}.{2}.{3}", bytes[start], bytes[start + 1], bytes[start + 2], bytes[start + 3]);}[Obsolete]public static void WriteInt16ToBytes(byte[] bytes, int start, short val){if (bytes == null){throw new Exception("bytes is null");}if (start < 0){throw new Exception("start less than 0");}int size = 2;if (bytes.Length < start + size){throw new Exception("bytes length less then that start plus size");}bytes[start] = (byte)(val & 0xff);bytes[start + 1] = (byte)(val >> 8 & 0xff);}[Obsolete]public static void WriteInt32ToBytes(byte[] bytes, int start, int val){if (bytes == null){throw new Exception("bytes is null");}if (start < 0){throw new Exception("start less than 0");}int size = 4;if (bytes.Length < start + size){throw new Exception("bytes length less then that start plus size");}bytes[start] = (byte)(val & 0xff);bytes[start + 1] = (byte)(val >> 8 & 0xff);bytes[start + 2] = (byte)(val >> 16 & 0xff);bytes[start + 3] = (byte)(val >> 24 & 0xff);}[Obsolete]public static void WriteInt64ToBytes(byte[] bytes, int start, long val){if (bytes == null){throw new Exception("bytes is null");}if (start < 0){throw new Exception("start less than 0");}int size = 8;if (bytes.Length < start + size){throw new Exception("bytes length less then that start plus size");}bytes[start] = (byte)(val & 0xff);bytes[start + 1] = (byte)(val >> 8 & 0xff);bytes[start + 2] = (byte)(val >> 16 & 0xff);bytes[start + 3] = (byte)(val >> 24 & 0xff);bytes[start + 4] = (byte)(val >> 32 & 0xff);bytes[start + 5] = (byte)(val >> 40 & 0xff);bytes[start + 6] = (byte)(val >> 48 & 0xff);bytes[start + 7] = (byte)(val >> 56 & 0xff);}[Obsolete]public static void WriteInt64To5Bytes(byte[] bytes, int start, long val){if (bytes == null){throw new Exception("bytes is null");}if (start < 0){throw new Exception("start less than 0");}int size = 5;if (bytes.Length < start + size){throw new Exception("bytes length less then that start plus size");}bytes[start] = (byte)(val & 0xff);bytes[start + 1] = (byte)(val >> 8 & 0xff);bytes[start + 2] = (byte)(val >> 16 & 0xff);bytes[start + 3] = (byte)(val >> 24 & 0xff);bytes[start + 4] = (byte)(val >> 32 & 0xff);//bytes[start + 5] = (byte)(val >> 40 & 0xff);//bytes[start + 6] = (byte)(val >> 48 & 0xff);//bytes[start + 7] = (byte)(val >> 56 & 0xff);
    }[Obsolete]public static void WriteUInt16ToBytes(byte[] bytes, int start, ushort val){if (bytes == null){throw new Exception("bytes is null");}if (start < 0){throw new Exception("start less than 0");}int size = 2;if (bytes.Length < start + size){throw new Exception("bytes length less then that start plus size");}//bytes[start] = (byte)(val & 0x11);//bytes[start + 1] = (byte)((val & 0x1100) >> 8);
bytes[start] = (byte)(0xff & val);bytes[start + 1] = (byte)((0xff00 & val) >> 8);}[Obsolete]public static void WriteUInt32ToBytes(byte[] bytes, int start, uint val){if (bytes == null){throw new Exception("bytes is null");}if (start < 0){throw new Exception("start less than 0");}int size = 4;if (bytes.Length < start + size){throw new Exception("bytes length less then that start plus size");}bytes[start] = (byte)(val & 0xff);bytes[start + 1] = (byte)(val >> 8 & 0xff);bytes[start + 2] = (byte)(val >> 16 & 0xff);bytes[start + 3] = (byte)(val >> 24 & 0xff);}[Obsolete]public static void WriteUInt64ToBytes(byte[] bytes, int start, ulong val){if (bytes == null){throw new Exception("bytes is null");}if (start < 0){throw new Exception("start less than 0");}int size = 8;if (bytes.Length < start + size){throw new Exception("bytes length less then that start plus size");}bytes[start] = (byte)(val & 0xff);bytes[start + 1] = (byte)(val >> 8 & 0xff);bytes[start + 2] = (byte)(val >> 16 & 0xff);bytes[start + 3] = (byte)(val >> 24 & 0xff);bytes[start + 4] = (byte)(val >> 32 & 0xff);bytes[start + 5] = (byte)(val >> 40 & 0xff);bytes[start + 6] = (byte)(val >> 48 & 0xff);bytes[start + 7] = (byte)(val >> 56 & 0xff);}public static int WriteAsc2Bytes(byte[] bytes, int start, string str){if (bytes == null){throw new Exception("bytes is null");}if (start < 0){throw new Exception("start less than 0");}int size = str.Length;if (bytes.Length < start + size){throw new Exception("bytes length less then that start plus size");}for (int i = 0; i < str.Length; i++){try{bytes[start + i] = Convert.ToByte(str[i]);}catch (Exception ex){ExceptionUtil.WriteLog(ex);DebugUtil.WriteLog("錯誤字符" + str[i]);}}return start + size;}
}

?

轉載于:https://www.cnblogs.com/mahuanpeng/p/6380101.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/284697.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/284697.shtml
英文地址,請注明出處:http://en.pswp.cn/news/284697.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【BIM入門實戰】Revit 圖元分類有哪三種?Revit圖元分類圖文詳解

Revit在項目中使用3種類型的圖元:模型圖元、基準圖元和視圖專有圖元。 Revit中的圖元也稱為族。族包含圖元的幾何定義和圖元所使用的參數。圖元的每個實例都由族定義和控制。 1. 模型圖元 模型圖元表示建筑的實際三維幾何圖形,包括如下:墻、窗、門和屋頂,結構墻、樓板、坡…

跟益達學Solr5之solrconfig.xml配置詳解

solrconfig.xml配置文件中包含了很多solr自身配置相關的參數,solrconfig.xml配置文件示例可以從solr的解壓目錄下找到&#xff0c;如圖&#xff1a; 用文本編輯軟件打開solrconfig.xml配置&#xff0c;你將會看到以下配置內容&#xff1a; Xml代碼 <?xml version"1.…

.NET 7 新增速率限制 (Rate Limiting) 功能,輕松限制請求數量

前言.NET 7 內置了速率限制&#xff08;Rate Limiting&#xff09;功能&#xff0c;速率限制指的是限制可訪問資源的請求數。例如數據庫每分鐘可以安全處理 1000 個請求&#xff0c;再多不確定會不會崩。這時就可以在應用程序中放一個速率限制器&#xff0c;規定每分鐘只允許 …

Cmder集成到VS Code (新舊版設置不同)

1.55版本之前 "terminal.integrated.shell.windows": "cmd.exe","terminal.integrated.shellArgs.windows": ["/k", "d:\\cmder\\cmdermini\\vendor\\init.bat"],1.55版本之后 "terminal.integrated.profiles.windows&…

Linux Tomcat8 啟動堆內存溢出

今天在部署一個開源項目的時候&#xff0c;Tomcat8啟動異常&#xff0c;報錯信息&#xff1a; Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space 根據報錯信息我們可以看出是堆內存不夠。所以需要手動設置堆內存大小&…

【BIM入門實戰】Revit視圖中圖元看不見的原因總結

在Revit模型設計的過程中&#xff0c;有時會提示繪制的圖元不可見&#xff0c;通常情況下&#xff0c;可以采用以下三種方法讓隱藏的圖元顯示出來。 原因一&#xff1a;視圖范圍 平面視圖的形成是由操作平面對三維進行 水平切割的俯視圖&#xff0c;如果繪制的圖元不可見&…

Tabcontrol動態添加TabPage(獲取或設置當前選項卡及其屬性)

http://blog.csdn.net/xiongxyt2/article/details/6920575 ?MultiLine 屬性用true 或false來確定是否可以多行顯示 ?Appearance 屬性設置選項卡的顯示方式&#xff0c;Normal,Buttons和FlatButtons為三種不同的顯示方式。 ?TabPages屬性設置選項卡的一系列屬性&#xff0c;包…

用C#為國產智能手表寫“Hello, China. ”

在此之前&#xff0c; 我寫過幾篇如何使用C#編寫STM32程序的例子&#xff0c; 那么同樣&#xff0c; ESP32下我們也可以使用C#&#xff0c;我們依然仰仗于一直在發展壯大的 .Net nanoFramework , 目前他支持的開發板越來越多 &#xff0c; 支持的芯片種類也越來越多&#xff0c…

Python將list存為csv文件

#!/usr/bin/env python # -*- encoding: utf-8 -*-import sys import json import os import pandas as pd""" description:將list存為csv文件 param {*} return {*} """staticmethod def list_to_csv(list_data, csv_file):if len(list_data) &…

【BIM入門實戰】Revit入門基礎知識選擇題帶答案解析(116題)

1、在Revit同一個界面同時打開多個視圖的快捷鍵是( )。 A、 WT B、 WA C、 WC D、 WD 答案: A 2、Revit樣板文件的后綴名是( )。 A、 .rvt B、 .rte C、 .rfa D、 .ifc 答案: B 3、標高、軸網創建的快捷鍵分別是( )。 A、 AL LL B、 LL GR C、 AR MM D、 LL TR 答案…

數據遷移 (選做)

1. pip install flask-migrate #Flask-Migrate 是一個數據遷移框架,需要通過Flask-script庫來操作. 2. pip install flask-script #通過命令行來操作Flask 3. 新建模型更改文件&#xff1a;manage.py from flask_script import Managerfrom flask_migrate import Migrate, Mi…

Flex4項目html-template文件夾解析

每個Flex的web應用程序項目都包含一個名為html-template文件夾。這個文件夾包含HTml模板和在瀏覽器中運行程序的支持文件。 每當你更改保存到你的源代碼&#xff0c;Flash Builder會自動重建應用程序使用的HTML模型文件并生成一個HTML包。同時&#xff0c;它把HTML模板文件夾的…

驅動之LCD的介紹與應用20170209

本文主要介紹的是LCD的介紹與應用&#xff0c;直接看個人筆記即可: 轉載于:https://www.cnblogs.com/yuweifeng/p/6382551.html

.NET 序列化枚舉為字符串

默認情況下&#xff0c;枚舉是以其整數形式進行 JSON 序列化&#xff0c;這通常會導致與消費者應用缺乏互操作性&#xff0c;因為他們需要事先了解這些數字的實際含義。因此&#xff0c;我們希望它們在一些情況下以字符串的形式進行序列化。本文將講解實現這一目標的各種方法。…

ArcGIS實驗教程——實驗四十四:ArcGIS地圖浮雕效果制作完整案例教程

ArcGIS制作地圖時可以制作出很多很炫的效果,比如地圖陰影、地圖暈渲效果、浮雕效果、三維效果等等。本實驗講解在ArcGIS中制作浮雕效果地圖,效果如下所示: 擴展閱讀:【ArcGIS Pro微課1000例】0016:ArcGIS Pro 2.8浮雕效果地圖制圖案例教程 1. 加載矢量數據 加載實驗數據包…

Mysql,SqlServer,Oracle主鍵自動增長的設置

參考文獻 http://blog.csdn.net/andyelvis/article/details/2446865 1、把主鍵定義為自動增長標識符類型 MySql 在mysql中&#xff0c;如果把表的主鍵設為auto_increment類型&#xff0c;數據庫就會自動為主鍵賦值。例如&#xff1a; create table customers(id int auto_incre…

Chapter 3 Phenomenon——19

His unfriendliness intimidated me. 他的不友好恐嚇到了我。 My words came out with less severity than Id intended. 我說出來的言辭比我打算的要不嚴厲一些。 我說出的話遠遠沒有達到我所想要的充滿火藥味的效果。 "You owe me an explanation," I reminded him…

Javascript 面向對象編程(一):封裝

Javascript是一種基于對象&#xff08;object-based&#xff09;的語言&#xff0c;你遇到的所有東西幾乎都是對象。但是&#xff0c;它又不是一種真正的面向對象編程&#xff08;OOP&#xff09;語言&#xff0c;因為它的語法中沒有class&#xff08;類&#xff09;。 那么&am…

【ArcGIS Pro微課1000例】0016:ArcGIS Pro 2.8浮雕效果地圖制圖案例教程

ArcGIS Pro制作地圖時可以制作出很多很炫的效果,比如地圖陰影、地圖暈渲效果、浮雕效果、三維效果等等。本實驗講解在ArcGIS Pro 2.8中制作浮雕效果地圖,效果如下所示: 【參考閱讀】:ArcGIS實驗教程——實驗四十四:ArcGIS地圖浮雕效果制作完整案例教程 1. 加載矢量數據 …

用正則實現多行文本合并,從而保存為csv文件

有如下文本&#xff0c;想實現每三行合并為一行&#xff0c;最終生成csv文件 分數 人數 累計人數 661及以上 23 23 660 3 26 659 5 31 658 5 36 657 9 45 656 10 55 655 4 59 654 6 65 653 15 80查找項&#xff1a; ^(.) ^(.) ^(.)替換項&#xff1a; $1,$2,$3替換結果&…