已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
常用数据格式转换函数集合using System;using System.Collections.Generic;using System.Text;namespace LaisonTech.CommonBLL / / 数据类型转换 / public class DataFormatProcessor public const Byte ASCII_0 = 48; public const Byte ASCII_9 = 57; public const Byte ASCII_A = 65; public const Byte ASCII_F= 70; public const Byte ASCII_Z= 90; public const Byte ASCII_a= 97; public const Byte ASCII_f= 102; public const Byte ASCII_z= 122; / / 字节转换为字符串 / / / public static String ByteToHexChar(Byte val) return val.ToString(X2); / / 字节转换为字符串 / / / public static String ByteToBitString(Byte val) if (val = 0) return 00000000; if (val = 255) return 11111111; String ret = String.Empty; Int32 bitval = 0; for (int bitidx = 7; bitidx -1; -bitidx) bitval = val & (1 0) ret = ret + 1; else ret = ret + 0; return ret; / / 2个HEX字符转换为1个字节数组 / / A1 / 161 / public static Boolean HexStringToByte(String hexstr, out Byte byteval) byteval = 0; if (hexstr.Length != 2) return false; byteval = Convert.ToByte(hexstr, 16); return true; / / HEX字符串转换为字节数组 / / / / public static Boolean HexStringToBytes(String hexstr, out Byte bytes) bytes = null; if (hexstr = null) | (hexstr.Length 2) ) return false; if (hexstr.Length % 2 = 1) return false; int bytelen = hexstr.Length / 2; if (bytelen 1) return false; bytes = new Bytebytelen; String curbytestr = String.Empty; for (int bidx = 0; bidx bytelen; bidx+) curbytestr = hexstr.Substring(bidx * 2, 2); /数字的进制,它必须是 2、8、10和16. bytesbidx = Convert.ToByte(curbytestr, 16); if (bytes = null | bytes.Length 1) return false; return true; / / HEX字符串转换为字节数组 / / / / public static Boolean HexStringToBytesWithSplitter(String hexstr, out Byte bytes) bytes = null; if (hexstr = null | hexstr.Length 2) return false; int charidx = 0; StringBuilder strbuild = new StringBuilder(hexstr.Length); /对每个字符进行判断,过滤非法字符 while (charidx = DataFormatProcessor.ASCII_0 & hexstrcharidx = DataFormatProcessor.ASCII_A & hexstrcharidx = DataFormatProcessor.ASCII_a & hexstrcharidx = DataFormatProcessor.ASCII_f) strbuild.Append(hexstrcharidx); charidx+; String purehexstr = strbuild.ToString(); return HexStringToBytes(purehexstr, out bytes); / / 字节数组转换为字符串 / / / public static String BytesToHexString(Byte bytes) if (bytes = null | bytes.Length 1) return String.Empty; StringBuilder str = new StringBuilder(bytes.Length * 2); for (int i = 0; i bytes.Length; i+) str.Append(bytesi.ToString(X2); return str.ToString(); / / 字节数组转换为字符串 / / / 待转换的首个字节下标 / / 每隔多少字节,就插入一个换行符.0表示不换行 / public static String BytesToHexString(Byte bytes, Int32 start, Int32 len, Int32 eachlinebytes) if (bytes = null | bytes.Length 1 | start 0 | len bytes.Length) return String.Empty; Int32 curlinebytes = 0; StringBuilder str = new StringBuilder(bytes.Length * 2); for (int i = 0; i 0) curlinebytes+; if (curlinebytes = eachlinebytes) str.Append(Environment.NewLine); curlinebytes = 0; return str.ToString(); / / 字节数组转换为带间隔符的字符串,显示用 / / / 待转换的首个字节下标 / / / 每隔多少字节,就插入一个换行符.0表示不换行 / public static String BytesToHexString(Byte bytes, Int32 start, Int32 len, Char splitter, Int32 eachlinebytes) if (bytes = null | bytes.Length 1 | start 0 | len bytes.Length) return String.Empty; Int32 curlinebytes = 0; StringBuilder str = new StringBuilder(bytes.Length * 2); for (int i = 0; i 0) curlinebytes+; if (curlinebytes = eachlinebytes) str.Append(Environment.NewLine); curlinebytes = 0; str.Remove(str.Length - 1, 1); return str.ToString(); / / 字节数组转换为ASCII字符串 / / / public static String BytesToString(Byte bytes) String ret = Encoding.Default.GetString(bytes); return ret; / / 1个字节的BCD转换为HEX / 0x12-12 / / BCD字节 / Int32 public static Byte ByteBCDToHex(Byte bcd) Byte result = (Byte)(bcd & 0x0F); /个位 bcd = 4; result += (Byte)(bcd * 10); /十位 return result; /返回结果 / / 把数组里的每个字节,都从HEX 转换为BCD / / public static Boolean ByteArrayHexToBCD(Byte hexbytes,Int32 hexstart, Byte bcdbytes,Int32 bcdstart, Int32 len) if (hexbytes = null | bcdbytes = null | hexbytes.Length 1 | bcdbytes.Length 1| hexstart 0 | bcdstart hexbytes.Length | bcdstart + len bcdbytes.Length) return false; for (Int32 idx = 0; idx len; +idx) bcdbytesbcdstart + idx = ByteHexToBCD(hexbyteshexstart + idx); return true; / / 把数组里的每个字节,都从BCD转换为HEX / / public static Boolean ByteArrayBCDToHex(Byte bcdbytes, Int32 bcdstart, Byte hexbytes, Int32 hexstart, Int32 len) if (hexbytes = null | bcdbytes = null | hexbytes.Length 1 | bcdbytes.Length 1 | hexstart 0 | bcdstart hexbytes.Length | bcdstart + len bcdbytes.Length) return false; for (Int32 idx = 0; idx len; +idx) hexbyteshexstart + idx = ByteBCDToHex(bcdbytesbcdstart + idx); return true; / / 1个字节的HEX转换为BCD / 64-0x64 / / BCD字节 / Int32 public static Byte ByteHexToBCD(Byte val) /如果超过99,则转换99 if(val 99) return 0x99; Byte lowval = (Byte)(val % 10);/个位 Byte highval = (Byte)(val / 10) % 10);/十位 Byte ret = (Byte)(lowval + (highval 4); return ret; / / ASCII字符串转换为字节数组 / / / / public static Boolean StringToBytes(String asciistr, out Byte bytes) bytes = Encoding.Default.GetBytes(asciistr); if (bytes = null | bytes.Length 1) return false; return true; / / Boolean 转换为 Int32 / / / public static Int32 BooleanToInt32 (Boolean bl) return bl ? 1 : 0; / / Boolean 转换为 Int32 的字符串 / / / public static String BooleanToInt32String(Boolean bl) return bl ? 1 : 0; / / Int32 转换为 Boolean / / / public static Boolean Int32ToBoolean(Int32 intval) return (intval = 0) ? false : true; / / Int32 的字符串 转换为 Boolean / / / public static Boolean Int32StringToBoolean(String intstr) if (intstr.Length 1) return false; return (intstr0 = 0) ? false : true; / / 各种枚举值转换为Int32String / / / public static String Int32ToString(Int32 enumval) return enumval.ToString(); / / 使用指定间隔符,把字符串分割为多个子串,合并为List / / / / / public static Boolean StringToIntArray(String rawdata, char splitter, ref Int32 intarray) List strlst = null; Boolean bl = StringToList(rawdata, splitter, ref strlst); if (!bl) return false; List intlist = new List(); Int32 val; for (int i = 0; i strlst.Count; i+) bl = Int32.TryParse(strlsti, out val); if (bl) intlist.Add(val); if (intlist.Count 1) return false; intarray = intlist.ToArray(); return true; / / 使用指定间隔符,把List合并为一个字符串 / / / / / public static Boolean ListToString(List strlist, char splitter, out String result) result = String.Empty; if (strlist = null | strlist.Count 1) return false; foreach (String item in strlist) result = result + item + splitter; result = result.Remove(result.Length - 1, 1); return true; / / 使用指定间隔符,把字符串分割为多个子串,合并为BindingList / / / / / /public static Boolean StringToBindingList(String rawdata, char splitter, out ObservableCollection strlist) / / strlist = new ObservableCollection(); / if (String.IsNullOrEmpty(rawdata) / / return false; / / String ary = rawdata.Split(splitter); / if (ary = null) | (ary.Length 1) / / return false; / / for (int i = 0; i ary.Length; i+) / / strlist.Add(aryi); / / return true; / / / 使用指定间隔符,把BindingList合并为一个字符串 / / / / / /public static Boolean BindingListToString(ObservableCollection strlist, char splitter, out String result) / / result = String.Empty; / if (strlist = null | strlist.Count 1) / / return false; / / StringBuilder tmpstr = new StringBuilder(4096); / foreach (String item in strlist) / / tmpstr.Append(item); / tmpstr.Append(splitter); / / tmpstr.Remove(result.Length - 1, 1); / result = tmpstr.ToString(); / return true; / / / 使用指定间隔符,把字符串分割为多个子串,合并为List / / / / / public static Boolean StringToIntList(String rawdata, char splitter, ref List intlist) List strlst = null; Boolean bl = StringToList(rawdata, splitter, ref strlst); if (!bl) return false; intlist = new List(); Int32 val; for (int i = 0; i 0); / / 使用指定间隔符,把字符串分割为多个子串,合并为List / / / / / public static Boolean StringToList(String rawdata, char splitter, ref List strlist) if (String.IsNullOrEmpty(rawdata) return false; String ary = rawdata.Split(splitter); if (ary = null)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年下半年台州黄岩区公安消防大队文职雇员招考易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年台州市国资本运营集团限公司及其所属企业公开招聘工作人员易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年厦门翔安区文化馆见习生招考易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年厦门市产品质量监督检验院招考非在编工作人员易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年北京市通州区第二次事业单位招聘(153人)易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年内蒙古锡林郭勒盟阿巴嘎旗事业单位招考易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年内蒙古赤峰市克什克腾旗事业单位招聘87人易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年内蒙古和林格尔县政务服务局招聘服务大厅合同制人员30人易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年内蒙古呼伦贝尔满洲里事业单位人才回引22人易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年内蒙古呼伦贝尔学院引进人才28人易考易错模拟试题(共500题)试卷后附参考答案
- 2025重庆双福农产品批发市场有限公司招聘综合办公室文员冻库管理员招商员等岗位22人笔试历年参考题库附带答案详解
- 企业社会责任与可持续发展管理
- 2025年河北邯郸经济技术开发区公共事业发展有限公司公开招聘工作人员20名考试参考试题及答案解析
- “教、学、评”体系下的小学语文互动式教学-以《桥》的教学为例
- 2025年部编版新教材语文八年级上册第一单元教学设计
- 母乳喂养新进员工培训
- 高原蔬菜种植培训课件
- 2024年~2025年历年林草局面试真题及答案解析
- 机械装备制造课件
- 房地产开发项目质量、安全、进度和文明施工保证措施
- 跨境民族文化传播机制-洞察及研究
评论
0/150
提交评论