string 转 byte[]

        /// <summary>
/// string 转 byte
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
byte[] Str_change_byte(string str)
{
byte[] Data = new byte[str.Length];
int count = ;
string strSource = str.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", ""); try
{
for (int i = ; i < (strSource.Length - strSource.Length % ) / ; i++)//取余3运算作用是防止用户输入的字符为奇数个
{
Data[count] = Convert.ToByte(strSource.Substring(i * , ), );
count++;
}
if (strSource.Length % != )//剩下一位单独处理
{
Data[count] = Convert.ToByte(strSource.Substring(strSource.Length - , ), );//单独处理B(0B)
count++;
}
}
catch (Exception)
{
MessageBox.Show("Error:数据包含非法字符\n", "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);//出错提示
return null;
}
byte[] buf = new byte[count];
Array.Copy(Data, , buf, , count); //复制原始数据
return buf;
}

string 转 ASCII

        public static byte[] str2ASCII(String xmlStr)
{
return Encoding.Default.GetBytes(xmlStr);
}

以下内容为转载:

https://www.cnblogs.com/Maxq/p/5953682.html

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 = ; 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 = ;

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(, newString.Length-);            }

int byteLength = newString.Length / ;byte[] bytes = newbyte[byteLength];string hex;int j = ;for (int i=; i<bytes.Length; i++){               

 hex = new String(new Char[] {newString[j], newString[j+]});               

 bytes[i] = HexToByte(hex);                j = j+;           

 }

return bytes;       

 }

C# string 转 byte[]的更多相关文章

  1. string转byte[]

    static byte[] GetBytes(string str) { byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buff ...

  2. java String与Byte[]和String 与InputStream转换时注意编码问题。。。

    前一段日子,我在做rsa加密和通过http get方式获取验证码图片通过BitmapFactory创建bitmap 出现了一系列的问题. 通过一系列的调试,发现有些问题原来是在进行String 与By ...

  3. java中string与byte[]的转换

    1.string 转 byte[] byte[] midbytes=isoString.getBytes("UTF8"); //为UTF8编码 byte[] isoret = sr ...

  4. C# double float int string 与 byte数组 相互转化

    在做通信编程的时候,数据发送多采用串行发送方法,实际处理的时候多是以字节为单位进行处理的.在C/C++中 多字节变量与Byte进行转化时候比较方便 采用UNION即可废话少说看示例:typedef u ...

  5. java Byte.toString 方法与String.ValueOf(Byte)效率比较

    int times = 10000000; Byte[] li = new Byte[times]; for (int i = 0; i < times; i++) { li[i] = (byt ...

  6. C# String 与 byte 互转

    String转换为byte数组用byte[] arr = System.Text.Encoding.Default.GetBytes("abcde") byte数组转换为Strin ...

  7. string和byte[]的转换 (C#)

    原文 string和byte[]的转换 (C#) string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes  ...

  8. C#中string和byte[]相互转换问题解决

    本来想讲string转换为byte数组,通过在VS上打 ‘str. “来找,结果半天没发现跳出来的函数中有想要的,哭瞎 /(ㄒoㄒ)/~~ 这回将两种情况都记下来了.... string ---> ...

  9. Go中string转[]byte的陷阱

    Go中string转[]byte的陷阱html {overflow-x: initial !important;}#write, body { height: auto; }#write, #writ ...

  10. golang string和[]byte的对比

    golang string和[]byte的对比 为啥string和[]byte类型转换需要一定的代价?为啥内置函数copy会有一种特殊情况copy(dst []byte, src string) in ...

随机推荐

  1. Visual Studio 2017 离线安装

    微软最近发布了正式版Visual Studio 2017并公开了其下载方式,不过由于VS2017采用了新的模块化安装方案,所以微软官方并未提供ISO镜像,但是官方提供了如何进行离线下载的方案给需要进行 ...

  2. [由于远程方关闭传输流,身份验证失败]一次处理支付接口bug记录

    因公司系统升级,出现突然有些银行卡不能支付的情况,最开始排查发现是第三方平台接口返回有问题: 返回如下: 从11月7日下午开始一直联系第三方,第三方开始排查,一直说是数据格式有问题. 修改格式以后问题 ...

  3. 如何理解“Unix 里一切都是文件”这句话-在 UNIX 中,一切都是字节流

    UNIX 操作系统的设计.用户界面.文化和演变都是建立在它的一套统一的想法和概念上.其中最重要的一点可能是“一切皆文件”,而这个概念被认为是 UNIX 的灵魂之一. 这一关键设计原则提供了一个统一的范 ...

  4. UVa 10003 - Cutting Sticks(区间DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. 【DP】:CF #319 (Div. 2) B. Modulo Sum

    [题目链接]:http://codeforces.com/contest/577/problem/B [相似题目]:http://swjtuoj.cn/problem/2383/ [题意]:给出n个数 ...

  6. UVA11987 【Almost Union-Find】

    这是一道神奇的题目,我调了大概一天多吧 首先hack一下翻译,操作3并没有要求查询后从其所在集合里删除该元素 于是我们来看一下这三个操作 第一个合并属于并查集的常规操作 第三个操作加权并查集也是可以解 ...

  7. thrift C++ Centos 安装

    1.在官方下载thrift http://thrift.apache.org/download 这里下载thrift-0.11.0.tar.gz版本 2.如果想支持安装Cpp版本就需要先安装boost ...

  8. MySQL数据库----------小知识点

    **********************补2016年5月23日的博客************************* MySQL数据库 常用数据类型: int                整数 ...

  9. Codeforces Round #527 (Div. 3) D2. Great Vova Wall (Version 2) 【思维】

    传送门:http://codeforces.com/contest/1092/problem/D2 D2. Great Vova Wall (Version 2) time limit per tes ...

  10. MyBatis(7)高级查询

    本次全部学习内容:MyBatisLearning 高级查询:   对于整体的工程是时候增加一点文件了: 具体用到那个类再去说明类的内容   一对一查询: 1.resultType进行实现: 执行的sq ...