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. 你所不知道的C和C++运行库 标签: vc 2017-05-26 10:33 41人阅读 评论(0) 收藏

    在使用vs2013调用vc2005编译出的dll时出现错误,遂将源程序用vs2013编译出dll,再用vs2013调用错误消失,不解.寻找原因时有人说"VC库版本不一样",故查找C ...

  2. ubuntu桌面便签 sticky note, xpad

    sudo apt-get install xpad 软件有一个选项,叫做Show window decorations,显示窗口的标题栏.

  3. 一个U盘黑掉你:TEENSY实战

    从传统意义讲,当你在电脑中插入一张CD/DVD光盘,或者插入一个USB设备时,可以通过自动播放来运行一个包含恶意的文件,不过自动播放功能被关闭时,autorun.inf文件就无法自动执行你的文件了.然 ...

  4. vbox安装 ubuntu server 后 安装增强包

    用vbox安装虚拟机系统如果不装增强包, 有很多东西就有点不好用-用vbox安装ubuntu server时,点击菜单中的安装增强功能.因为ubuntu server版本没有ui,所以不能很方便滴找到 ...

  5. 元素设置float属性后,其后面的元素的位置问题

    分两种情况: .content{ width:350px; height:150px; color:#fff; } .content1,.content2{ background-color: #00 ...

  6. eclipse去掉xml验证的方法

    eclipse Multiple annotations found at this line错误,eclipse开发过程中,一些XML配置文件会报错,但是这些其实不是错,飘红的原因是因为eclips ...

  7. Spark系列-SparkSQL实战

    Spark系列-初体验(数据准备篇) Spark系列-核心概念 Spark系列-SparkSQL 之前系统的计算大部分都是基于Kettle + Hive的方式,但是因为最近数据暴涨,很多Job的执行时 ...

  8. C# 文件流FileStream 实现多媒体文件复制 StreamReader StreamWriter 读取写入文本

    #region 实现多媒体文件的复制 string source = @"F:\123\source.avi";//源文件路径 string target = @"F:\ ...

  9. idea删除module

    先remove , 再点击delete

  10. pat——1017. Queueing at Bank (java中Map用法)

    由PAT1017例题展开: Suppose a bank has K windows open for service. There is a yellow line in front of the ...