-
大小: 0.35M文件類型: .7z金幣: 1下載: 0 次發(fā)布日期: 2024-04-27
- 語言: C#
- 標簽: 數(shù)據(jù)傳輸??傳輸??數(shù)據(jù)??工具??HJ212??
資源簡介
子站端發(fā)送工具,自動補全包尾。協(xié)議包數(shù)據(jù)放在發(fā)送數(shù)據(jù)部分即可,返回數(shù)據(jù)為平臺返回的數(shù)據(jù)包
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Text;
namespace?Wt.FormUI
{
????public?class?ByteConvert
????{
????????///???
????????///?字符串轉16進制字節(jié)數(shù)組??
????????///???
????????///???
????????///???
????????public?static?byte[]?strToToHexByte(string?hex)
????????{
????????????//hexString?=?hexString.Replace(“?“?““);
????????????//if?((hexString.Length?%?2)?!=?0)
????????????//????hexString?+=?“?“;
????????????//byte[]?returnBytes?=?new?byte[hexString.Length?/?2];
????????????//for?(int?i?=?0;?i?????????????//????returnBytes[i]?=?Convert.ToByte(hexString.Substring(i?*?2?2)?16);
????????????//return?returnBytes;
????????????if?(hex?==?null)
????????????????throw?new?ArgumentNullException(“hex“);
????????????hex?=?hex.Replace(““?““);
????????????hex?=?hex.Replace(“\n“?““);
????????????hex?=?hex.Replace(“\\“?““);
????????????hex?=?hex.Replace(“?“?““);
????????????if?(hex.Length?%?2?!=?0)
????????????{
????????????????hex?+=?“20“;//空格?
????????????}
????????????//?需要將?hex?轉換成?byte?數(shù)組。?
????????????byte[]?bytes?=?new?byte[hex.Length?/?2];
????????????for?(int?i?=?0;?i?????????????{
????????????????try
????????????????{
????????????????????//?每兩個字符是一個?byte。?
????????????????????bytes[i]?=?byte.Parse(hex.Substring(i?*?2?2)
????????????????????System.Globalization.Numberstyles.HexNumber);
????????????????}
????????????????catch
????????????????{
????????????????????//?Rethrow?an?exception?with?custom?message.?
????????????????????throw?new?ArgumentException(“hex?is?not?a?valid?hex?number!“?“hex“);
????????????????}
????????????}
????????????//System.Text.Encoding?chs?=?System.Text.Encoding.GetEncoding(charset);
????????????//return?chs.GetString(bytes);
????????????return?bytes;
????????}
????????///???
????????///?字節(jié)數(shù)組轉16進制字符串??
????????///???
????????///???
????????///???
????????public?static?string?byteToHexStr(byte[]?bytes)
????????{
????????????StringBuilder?builder?=?new?StringBuilder();?;
????????????if?(bytes?!=?null)
????????????{
????????????????for?(int?i?=?0;?i?????????????????{
????????????????????builder.AppendFormat(“{0}?“?bytes[i].ToString(“X2“));
????????????????}
????????????}
????????????string?returnStr?=?builder.ToString();
????????????return?returnStr;
????????}
????????///???
????????///?從漢字轉換到16進制??
????????///???
????????///???
????????///?編碼如“utf-8““gb2312“??
????????///?是否每字符用逗號分隔??
????????///???
????????public?static?string?ToHex(string?s?string?charset?bool?fenge)
????????{
????????????if?((s.Length?%?2)?!=?0)
????????????{
????????????????s?+=?“?“;//空格??
?????????????????????????//throw?new?ArgumentException(“s?is?not?valid?chinese?string!“);??
????????????}
????????????System.Text.Encoding?chs?=?System.Text.Encoding.GetEncoding(charset);
????????????byte[]?bytes?=?chs.GetBytes(s);
????????????string?str?=?““;
????????????for?(int?i?=?0;?i?
評論
共有 條評論