通信协议 数据写入字节数组的辅助类ProtocolHelper2
2011-10-29 09:36
351 查看
public static int AddUTF8StringToBuf1(ref Byte[] buf, int start, string str) { byte[] bytes; bytes = Encoding.UTF8.GetBytes(str); return AddDataToBuf1(ref buf, start, bytes, (Byte)bytes.Length); } public static int AddStringToBuf1(ref Byte[] buf, int start, string str) { int i, p; Byte len; p = start; len = (Byte)str.Length; buf[p++] = len; for (i = 0; i < len; ) { buf[p++] = System.Convert.ToByte(str[i++]); } return (int)(len + 1); } public static int AddDataToBuf1(ref Byte[] buf, int start, Byte[] data, int len) { int i, p; p = start; buf[p++] = (byte)len; for (i = 0; i < len; ) { buf[p++] = data[i++]; } return (int)(len + 1); } public static int AddDataToBuf2(ref Byte[] buf, int start, Byte[] data, int len) { int i, p; p = start; if (len > 1024) return start; buf[p++] = (Byte)(len >> 8); buf[p++] = (Byte)len; for (i = 0; i < len; ) { buf[p++] = data[i++]; } return (int)(len + 2); } public static int AddValueToBuf(ref Byte[] buf, int start, uint value, int len) { Byte i; int p; if (len < 1 || len > 4) return start; p = start; for (i = 0; i < len; i++) { buf[p++] = (Byte)(value >> ((len - i - 1) * 8)); } return len; } public static int AddUlongValueToBuf(ref Byte[] buf, int start, ulong value, int len) { Byte i; int p; if (len < 1 || len > 8) return 0; p = start; for (i = 0; i < len; i++) { buf[p++] = (Byte)(value >> ((len - i - 1) * 8)); } return len; } public static int AddDateTimeToBuf(ref Byte[] buf, int start, DateTime time, int len) { int p; p = start; buf[p++] = (Byte)(time.Year-2000); buf[p++] = (Byte)(time.Month); buf[p++] = (Byte)(time.Day); buf[p++] = (Byte)(time.Hour); buf[p++] = (Byte)(time.Minute); buf[p++] = (Byte)(time.Second); return 6; }
相关文章推荐
- 通信协议 数据转换辅助类ProtocolHelper1
- Unity3D中的socket通信之八(字节数组与数据转换dll-byte int string float)
- [置顶] 通信协议中多字节数据的解析
- 通信协议中多字节数据的解析
- 编写简单一个程序 将一个长整型数据 写入字节数组中,在从字节数组中读出来
- Netty 快速入门系列 - Chapter 6 自定义数据协议【第十三讲】 通过大端序列方法将4个字节int转成 byte数组
- 通信(Netty、Mina2)【通信粘包的处理】、【数据协议】、【网络系统的安全性】
- Java学习日志(21-2-IO流-基本数据类型与字节数组对象与、编码解码)
- HTTP(二)http协议通信步骤及数据解析
- JAVA IO分析二:字节数组流、基本数据&对象类型的数据流、打印流
- 数组数据循环写入数据库的问题
- 从调试数据分析USB通信协议——UVC摄像头【UVC类设备】(五)
- Java中字节数组与基本数据类型的转换
- float型数据与字节数组的转化
- 【语言基础】c++ 基本数据类型与字节数组(string,char [] )之间的转化方法
- 从调试数据分析USB通信协议——USB键盘鼠标【HID类设备】(四)
- 关于float型数据与int型,字节数组的转化
- 压缩文本、字节或者文件的压缩辅助类-GZipHelper 欢迎收藏
- Java基础小知识——基本数据类型和字符串以及字节数组的转换
- C++ 字节数组的转换(C++编程思想-数据抽象例子分析)