C# byte[]转string, string转byte[] 的四种方法
转载:https://blog.csdn.net/tom_221x/article/details/71643015
第一种
- string str = System.Text.Encoding.UTF8.GetString(bytes);
- byte[] decBytes = System.Text.Encoding.UTF8.GetBytes(str);
同样的,System.Text.Encoding.Default,System.Text.Encoding.ASCII也是可以的。还可以使用System.Text.Encoding.UTF8.GetString(bytes).TrimEnd('\0')给字符串加上结束标识。
第二种
- string str = BitConverter.ToString(bytes);
- String[] tempArr = str.Split('-');
- byte[] decBytes = new byte[tempArr.Length];
- for (int i = 0; i < tempArr.Length; i++)
- {
- decBytes[i] = Convert.ToByte(tempArr[i], 16);
- }
这种方法会给字符串加上 '-' 连字符,并且没有函数转换回去。所以需要手动转换为bytes。
第三种
- string str = Convert.ToBase64String(bytes);
- byte[] decBytes = Convert.FromBase64String(str);
这种方法简单明了,完美无问题。需要注意的是,转换出来的string可能会包含 '+','/' , '=' 所以如果作为url地址的话,需要进行encode。
第四种
- string str = HttpServerUtility.UrlTokenEncode(bytes);
- byte[] decBytes = HttpServerUtility.UrlTokenDecode(str);
这种方法会自动编码url地址的特殊字符,可以直接当做url地址使用。但需要依赖System.Web库才能使用。
C# byte[]转string, string转byte[] 的四种方法的更多相关文章
- 【Java必修课】判断String是否包含子串的四种方法及性能对比
1 简介 判断一个字符串是否包含某个特定子串是常见的场景,比如判断一篇文章是否包含敏感词汇.判断日志是否有ERROR信息等.本文将介绍四种方法并进行性能测试. 2 四种方法 2.1 JDK原生方法St ...
- C# byte[]数组和string的互相转化 (四种方法)
C# byte[]数组和string的互相转化 (四种方法) 第一种 [csharp] view plain copy string str = System.Text.Encoding.UTF8.G ...
- C# 字符串拼接性能探索 c#中+、string.Concat、string.Format、StringBuilder.Append四种方式进行字符串拼接时的性能
本文通过ANTS Memory Profiler工具探索c#中+.string.Concat.string.Format.StringBuilder.Append四种方式进行字符串拼接时的性能. 本文 ...
- Java 中转换为String类型的四种方法
1. 使用 String 的构造方法,用于 byte[], char[], StringBuffer, StringBuilder 类型 2. 使用 String 的静态方法 valueOf() 推荐 ...
- Map<String, String> 遍历的四种方法
Map<String, String> map = new HashMap<String, String>(); map.put("key1", " ...
- int转换为String,常用的四种方法。
int i = 100; 方法一:String s1 = i + " "; 方法二:String s2 = String.valueof(i); 方法三(先转换为Integer类型 ...
- C#中将string[] 转成 int[]的几种方法
C#中将string[] 转成 int[]的方式有很多种. 直接遍历转换 这是最简单粗暴的方式.实例代码: 使用LINQ遍历 本质和直接遍历没多大差异,但代码量更少,代码更美观.实例代码: 注意:需要 ...
- C# string类型和byte[]类型相互转换
string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转成string: ...
- Unity3D 集成 Face++ FacePlusPlus httpClient http协议 byte数组转string
//開始由于要实现跨平台.考虑过用curl封装c++的dll(android *.so)的方式,在c#Dllimport实现 //后来发现Unity3D本身支持http协议.且face++的api都是 ...
随机推荐
- react-父子子孙组件嵌套-context
方案一 import React from 'react' import ReactTypes from 'prop-types' /* // 最外层的父组件 export default class ...
- 在vue项目中显示实时时间(年月日时分秒)
1.在data中定义一个变量,存储时间 data(){ return { nowTime:'' } }, 2.给定一个div <div>{{nowTime}}</div> 3. ...
- 微信小程序CSS之Flex布局
转载:https://blog.csdn.net/u012927188/article/details/83040156 相信刚开始学习开发小程序的初学者一定对界面的布局很困扰,不知道怎么布局,怎么摆 ...
- Springboot 中的配置文件
Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值. 技术:yaml.properties语法,Con ...
- Selenium原理
from selenium import webdriver:导入webdriver模块 当导入webdriver模块时,会执行\selenium\webdriver目录下的__init__.py ...
- Django学习笔记3
From the last two parts, we know, by using the HttpResponse we can return text to the web page, and ...
- 【Unity|C#】基础篇(11)——内置的泛型委托(Action/Func/Predicate)
[Action] 无返回值 的泛型委托,可以有0~16个参数(函数重载) public delegate void Action(); // 无参数 public delegate void Acti ...
- 获取 Android APP 版本信息工具类(转载)
获取 Android APP 版本信息工具类 获取手机APP版本信息工具类 1.获取版本名称 2.获取版本号 3.获取App的名称 package com.mingyue.nanshuibeidiao ...
- 在 linux 中连接 mysql 数据库
命令格式 mysql -h主机地址 -u用户名 -p用户密码 登录本机 mysql mysql -u用户名 -p用户密码 实例 TD - X1数据库:/opt/lampp/bin/mysql -u r ...
- ORA-04089: cannot create triggers on objects owned by SYS
04089 问题原因 因为你在以sys用户创建触发器,但oracle却不建议在sys用户下创建触发器.