设为首页 加入收藏
首 页 企业简介 项目案例 软件定制 行业软件 解决方案 企业资讯 服务专区 客服中心
服务项目
案例展示 更多 >>
·维修管理系统 2012/5/14
·园区游客服务管理系统 2012/5/10
·党风廉政信息公开平台 2012/5/10
·职工提案系统软件 2012/1/11
·电信客户资源管理系统软… 2012/1/11
·洗衣收银软件 2012/1/11
·触摸查询系统软件 2012/1/11
·西安来电弹屏软件 2011/8/31
·西安软件公司仓库管理软… 2011/6/9
·会员管理系统 2011/6/9
联系人:李先生
电  话:029-87878512
手  机:13468700578
地  址:西安市丝路国际创意梦工厂4号
在线咨询:  762176615
Email:junsoft@126.com
 
当前的位置 >> 返回首页 >> 解决方案
西安软件公司C#string类型和byte[]类型相互转换
发布者:西安软件公司   发布时间:2017/9/1   阅读:10次

string类型转成byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );

byte[]转成string:
string str = System.Text.Encoding.Default.GetString ( byteArray );

string类型转成ASCII byte[]:
("01" 转成 byte[] = new byte[]{ 0x30,0x31})
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str );

ASCIIbyte[]转成string:
(byte[] = new byte[]{ 0x30, 0x31} 转成"01")
string str = System.Text.Encoding.ASCII.GetString ( byteArray );

byte[]转16进制格式string:
new byte[]{ 0x30, 0x31}转成"3031":
publicstaticstring ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF "
{string hexString = string.Empty;
if ( bytes != null )
{                
StringBuilder strB = new StringBuilder ();
for ( int i = 0; i < bytes.Length; i++ )
{                    
strB.Append ( bytes[i].ToString ( "X2" ) );                
}                
hexString = strB.ToString ();            
}return hexString;        
}

16进制格式string 转byte[]:
publicstaticbyte[] GetBytes(string hexString, outint discarded)
{            
discarded = 0;
string newString = "";
char c;// remove all none A-F, 0-9, charactersfor (int i=0; i<hexString.Length; i++)
{              
  c = hexString[i];if (IsHexDigit(c))                    
newString += c;
else                    
discarded++;            
}// if odd number of characters, discard last characterif (newString.Length % 2 != 0){                discarded++;                
newString = newString.Substring(0, newString.Length-1);            }
int byteLength = newString.Length / 2;byte[] bytes = newbyte[byteLength];string hex;int j = 0;for (int i=0; i<bytes.Length; i++){               
 hex = new String(new Char[] {newString[j], newString[j+1]});               
 bytes[i] = HexToByte(hex);                j = j+2;           
 }
return bytes;       
 }
网站首页 | 关于我们 | 售后服务 | 网站地图 | 查看留言 | 在线留言 | 客服中心
© 版权所有:西安润宇软件科技有限公司 
公司地址:西安市浐灞生态区欧亚大道3639号丝路国际创意梦工厂4号 联系电话:029-87878512 手机:13468700578 联系人:李先生
Copyright ® 2011-2020 Xbwbw.com Inc. All Rights Reserved 
技术支持:西安润宇软件科技有限公司  陕ICP备11000720号-2