String Byte 互转
string类型转成byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
byte[]转成string:
string str = System.Text.Encoding.Default.GetString ( byteArray );
string类型转成ASCII byte[]:
("" 转成 byte[] = new byte[]{ 0x30,0x31})
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str );
ASCIIbyte[]转成string:
(byte[] = new byte[]{ 0x30, 0x31} 转成"")
string str = System.Text.Encoding.ASCII.GetString ( byteArray );
byte[]转16进制格式string:
new byte[]{ 0x30, 0x31}转成"":
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;
}
String Byte 互转的更多相关文章
- 字符串string和内存流MemoryStream及比特数组byte[]互转
原文:字符串string和内存流MemoryStream及比特数组byte[]互转 字符串string和内存流MemoryStream及比特数组byte[]互转比较 定义string变量为str, ...
- Go语言网络通信---string与int互转,int64与[]byte互转,int直接互转,string与[]byte互转
string与int互转 #string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt( ...
- 用java String类的getBytes(String charsetName)和String(byte[] bytes, String charsetName)解决乱码问题
Java中String的数据是如何存储的,查看源代码就可以知道,String的数据是存储在char[] value这样一个成员变量中的,char类型的大小在java中是2个字节 我们还知道,现在普遍使 ...
- new String(byte[])和byte[]toString() 的区别
byte[]字节数组的toString()获得的字符串和使用new String(byte[])构造一个新的字符串,这两个字符串是不一样的.Java对象都继承于Object,Object中提供了toS ...
- byte[]->new String(byte[]) -> getByte()引发的不一致问题
今天接短信接口,短信接口提供了sdk,我们可以直接用sdk发送请求然后发送对应短信. 但是想使用我们平台自定义的httpUtil实现. 然而忙了1天半,才解决这个问题,还是我同事帮忙找出问题并解决的. ...
- Json、JavaBean、String等互转
Json.JavaBean.String等互转 本文介绍简单的Json.JavaBean.String互换(下文JavaBean简称Object对象,这里不是很严谨) 转换关系如下: 其中String ...
- InputStream转换为String, byte[] data = new byte[1024]详解
/** * This file created at 2018年2月28日. * * Copyright (c) 2002-2018 Bingosoft, Inc. All rights reserv ...
- C#string byte[] base64位互相转换
byte表示字节,byte[]则表示存放一系列字节的数组 1个字符=2个字节(byte) 1个字节=8个比特(bit) 网速上所说的1M其实是指1兆的小b,1M= 1024b/8 = 128kb 下面 ...
- byte[] 的toString() 和 new String(byte[]) 的区别
今天在Android上测试压缩和解压缩. 获得压缩后的byte[]数组后,直接用 byte[].toString()方法取得字符串. 然后用这个字符串再反向来解压缩,还原数据.却发现还原回来的字符串有 ...
随机推荐
- .Net Core 在Linux服务器下部署程序--(1). Windows 连接 Linux服务器
下载Linux服务器连接软件,市面上有Putty,FinalShell等,我以FinalShell为例,下载地址为 :http://www.hostbuf.com/t/988.html,软件安装结束后 ...
- kvm虚拟机管理基础
部署 KVM 虚拟机 a.kvm 安装 环境:centos7,cpu 支持虚拟化,关闭 selinux,关闭 firewalld yum install libvirt virt-install qe ...
- Delphi 数据转换
指针转换 Pointer——string string:=PChar(Pointer);{ Pointer指向的数据要以#0结尾.使用System.AllocMem(Size)分配的内存是用#0填 ...
- 解释型语言VS编译型语言
前言 计算机不能直接理解除机器语言以外的语言,所以只有把程序员编写的程序翻译成机器语言,计算机才能够执行程序. 将其他语言翻译成机器语言的工具,被称之为:编译器. 编译器的翻译方式有两种:编译和解释. ...
- vue源码分析—Vue.js 源码构建
Vue.js 源码是基于 Rollup 构建的,它的构建相关配置都在 scripts 目录下.(Rollup 中文网和英文网) 构建脚本 通常一个基于 NPM 托管的项目都会有一个 package.j ...
- jQuery中$.each()方法的使用
$.each()是对数组,json和dom结构等的遍历,说一下他的使用方法吧. 1.遍历一维数组 var arr1=['aa','bb','cc','dd']; $.each(arr1,functio ...
- photoshop实例实战(入门级)教程
PS对街拍女孩照片增加质感(2019-03-19 17:57) PS制作水火相溶特效文字图片(2019-03-19 17:49) PS制作简洁漂亮的立体抽丝文字(2019-03-19 17:16) P ...
- [转帖]Windows7/2008中批量删除隧道适配器的方法
https://www.jb51.net/os/windows/479838.html 客户现场的硬件信息总是发生变化 这里查找一下资料 尝试一下. 1.在网卡属性的“网络”中,将“Internet协 ...
- Iterator和Enumeration的区别
从源码可以看出,Iterator除了能读取集合的数据之外,也能数据进行删除操作:而Enumeration只能读取集合的数据,而不能对数据进行修改. Iterator支持fail-fast机制,而Enu ...
- Python——pyqt5——消息框(QMessageBox)
一.提供的类型 QMessageBox.information 信息框 QMessageBox.question 问答框 QMessageBox.warning 警告 QMessageBox.ctit ...