• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >ASP.NET > C#中string与byte[]的转换帮助类-.NET教程,C#语言

C#中string与byte[]的转换帮助类-.NET教程,C#语言

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-11

通过本文主要向大家介绍了c站,欲情 c max,c语言,crh2c,奔驰c200等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
 
主要实现了以下的函数 


代码中出现的sidle是我的网名。 

/**//* 
* @author wuerping 
* @version 1.0 
* @date 2004/11/30 
* @description: 
*/ 
using system; 
using system.text; 
namespace sidlehelper 
{ 
/**//// <summary> 
/// summary description for strhelper. 
/// 命名缩写: 
/// str: unicode string 
/// arr: unicode array 
/// hex: 二进制数据 
/// hexbin: 二进制数据用ascii字符表示 例 字符1的hex是0x31表示为hexbin是 31 
/// asc: ascii 
/// uni: unicode 
/// </summary> 
public sealed class strhelper 
{ 
hex与hexbin的转换#region hex与hexbin的转换 
public static void hexbin2hex(byte[] bhexbin, byte[] bhex, int nlen) 
{ 
for(int i=0; i<nlen/2; i++) 
{ 
if(bhexbin[2*i] <0x41) 
{ 
bhex[i] = convert.tobyte(((bhexbin[2*i] - 0x30)<<4) & 0xf0); 
} 
else 
{ 
bhex[i] = convert.tobyte(((bhexbin[2*i] - 0x37)<<4) & 0xf0); 
} 

if(bhexbin[2*i+1] <0x41) 
{ 
bhex[i] |= convert.tobyte((bhexbin[2*i+1] - 0x30) & 0x0f); 
} 
else 
{ 
bhex[i] |= convert.tobyte((bhexbin[2*i+1] - 0x37) & 0x0f); 
} 
} 
} 
public static byte[] hexbin2hex(byte[] bhexbin, int nlen) 
{ 
if(nlen%2 !=0) 
return null; 
byte[] bhex = new byte[nlen/2]; 
hexbin2hex(bhexbin, bhex, nlen); 
return bhex; 
} 
public static void hex2hexbin(byte[] bhex, byte[] bhexbin, int nlen) 
{ 
byte c; 
for(int i=0;i<nlen;i++) 
{ 
c = convert.tobyte((bhex[i]>>4) & 0x0f); 
if(c < 0x0a) 
{ 
bhexbin[2*i] = convert.tobyte(c + 0x30); 
} 
else 
{ 
bhexbin[2*i] = convert.tobyte(c + 0x37); 
} 
c = convert.tobyte(bhex[i]&0x0f); 
if(c < 0x0a) 
{ 
bhexbin[2*i+1] = convert.tobyte(c + 0x30); 
} 
else 
{ 
bhexbin[2*i+1] = convert.tobyte(c + 0x37); 
} 
} 
} 
public static byte[] hex2hexbin(byte[] bhex, int nlen) 
{ 
byte[] bhexbin = new byte[nlen*2]; 
hex2hexbin(bhex, bhexbin, nlen); 
return bhexbin; 
} 
#endregion 

数组和字符串之间的转化#region 数组和字符串之间的转化 
public static byte[] str2arr(string s) 
{ 
return (new unicodeencoding()).getbytes(s); 
} 
public static string arr2str(byte[] buffer) 
{ 
return (new unicodeencoding()).getstring(buffer, 0, buffer.length); 
} 

public static byte[] str2ascarr(string s) 
{ 
return system.text.unicodeencoding.convert(system.text.encoding.unicode, 
system.text.encoding.ascii, 
str2arr(s)); 
} 

public static byte[] str2hexascarr(string s) 
{ 
byte[] hex = str2ascarr(s); 
byte[] hexbin = hex2hexbin(hex, hex.length); 
return hexbin; 
} 
public static string ascarr2str(byte[] b) 
{ 
return system.text.unicodeencoding.unicode.getstring( 
system.text.asciiencoding.convert(system.text.encoding.ascii, 
system.text.encoding.unicode, 
b) 
); 
} 

public static string hexascarr2str(byte[] buffer) 
{ 
byte[] b = hex2hexbin(buffer, buffer.length); 
return ascarr2str(b); 
} 
#endregion 
} 
} 
</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#开发微信 二维码鼠标滑动 图像显示隐藏效果(推荐)
  • C#开发微信 二维码鼠标滑动 图像显示隐藏效果(推荐)
  • C#.net 微信公众账号接口开发
  • C#实现把图片下载到服务器代码
  • C#将DataTable转化为List<T>
  • C#中OpenFileDialog和PictrueBox的用法分析
  • c#网站WebConfig中域名引用示例介绍
  • C#中的switch case使用介绍
  • C#随机生成不重复字符串的两个不错方法
  • C# 判断两张图片是否一致的快速方法

相关文章

  • 2018-08-20Visual Studio Debugger七个鲜为人知的小功能
  • 2017-05-11ASP.NET内置对象之Application对象
  • 2017-05-11MVC HtmlHelper扩展类(PagingHelper)实现分页功能
  • 2018-08-20MVC+EasyUI+三层新闻网站建立 验证码生成(三)
  • 2017-05-11asp.net Ajax 安装与卸载方法
  • 2017-05-11关于Asp.net页面Page_Load被执行两次的问题分享
  • 2017-05-11js控制.net验证控件是否可用。
  • 2017-05-11ASP.NET中Application、Cookie、Session、Cache和ViewState
  • 2017-05-11C# 判断两张图片是否一致的快速方法
  • 2018-08-20ASP.NET Core 2.0 WebApi全局配置及日志实例

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • asp.net 安全、实用、简单的大容量存储过程分页第1/2页
    • asp.net基于Web Service实现远程上传图片的方法
    • 网页(aspx)与用户控件(ascx)交互逻辑处理实现
    • ASP.NET中使用开源组件NPOI快速导入导出Execl数据
    • ASP.NET Lable中进行换行解决方案
    • ASP.NET预备知识学习笔记
    • ASP.NET笔记之Calender的使用说明
    • ASP.NET 页面刷新的实现方法(包括html,js)
    • ASP.NET中日历控件和JS版日历控件的使用方法(第5节)
    • ASP.NET与MySQL数据库简明图示入门教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有