【转】go里面字符串转成 字节slice, 字节slice转成字符串
原文: https://yourbasic.org/golang/convert-string-to-byte-slice/#convert-string-to-bytes
---------------------------------------------------------------------------
Basics
When you convert between a string and a byte slice (array), you get a brand new slice that contains the same bytes as the string, and vice versa.
- The conversion doesn’t change the data;
- the only difference is that strings are immutable, while byte slices can be modified.
If you need to manipulate the characters (runes) of a string, you may want to convert the string to a rune slice instead. See Convert between rune array/slice and string.
Convert string to bytes
When you convert a string to a byte slice, you get a new slice that contains the same bytes as the string.
b := []byte("ABC€")
fmt.Println(b) // [65 66 67 226 130 172]
Note that the character € is encoded in UTF-8 using 3 bytes. See the Go rune articlefor more on UTF-8 encoding of Unicode code points.
Convert bytes to string
When you convert a slice of bytes to a string, you get a new string that contains the same bytes as the slice.
s := string([]byte{65, 66, 67, 226, 130, 172})
fmt.Println(s) // ABC€
Performance
These conversions create a new slice or string, and therefore have time complexityproportional to the number of bytes that are processed.
More efficient alternative
In some cases, you might be able to use a string builder, which can concatenate strings without redundant copying:
【转】go里面字符串转成 字节slice, 字节slice转成字符串的更多相关文章
- Python入门篇-基础数据类型之整型(int),字符串(str),字节(bytes),列表(list)和切片(slice)
Python入门篇-基础数据类型之整型(int),字符串(str),字节(bytes),列表(list)和切片(slice) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Py ...
- Java字节、十进制、十六进制、字符串之间的相互转换
1. 字节转10进制 直接使用(int)类型转换. /* * 字节转10进制 */ public static int byte2Int(byte b){ int r = (int) b; retur ...
- java中字节数组byte[]和字符(字符串)之间的转换
转自:http://blog.csdn.net/linlzk/article/details/6566124 Java与其他语言编写的程序进行tcp/ip socket通讯时,通讯内容一般都转换成by ...
- C#//字节数组转16进制字符串
//字节数组转16进制字符串 private static string byteToHexStr(byte[] bytes,int length) { string returnStr = &quo ...
- 《将一个字符串转换成datetime时,先分析该字符串以获取日期,然后再将每个变量放置到datetime对象中》的解决办法
我们在写代码时,稍不注意就收到VS那文不对题的错误提示. 最近在项目上碰到了“将一个字符串转换成datetime时,先分析该字符串以获取日期,然后再将每个变量放置到datetime对象中”的这个错误提 ...
- C++:怎样把一个int转成4个字节?
大家都知道,一个int 或 unsigned int是由4个字节组成的,(<C/C++学习指南>,第3章,第3.2.3节:变量的内存视图) 比如, int n = sizeof( ...
- 字符串A转换到字符串B,只能一次一次转换,每次转换只能把字符串A中的一个字符全部转换成另一个字符,是否能够转换成功
public class DemoTest { public static void main(String[] args) { System.)); } /** * 有一个字符串A 有一个字符串B ...
- JS的slice、substring、substr字符串截取
JS中截取一个字符串的三种方法:字符串.slice(开始索引,结束索引)字符串.substring(开始索引,结束索引)字符串.substr(开始索引,截取的长度) 如果需要截取到该字符串的最后,可以 ...
- 给定一个字符串,把字符串内的字母转换成该字母的下一个字母,a换成b,z换成a,Z换成A,如aBf转换成bCg, 字符串内的其他字符不改变,给定函数,编写函数 void Stringchang(const char*input,char*output)其中input是输入字符串,output是输出字符串
import java.util.Scanner; /*** * 1. 给定一个字符串,把字符串内的字母转换成该字母的下一个字母,a换成b,z换成a,Z换成A,如aBf转换成bCg, 字符串内的其他字 ...
- js字符串长度计算(一个汉字==两个字符)和字符串截取
js字符串长度计算(一个汉字==两个字符)和字符串截取 String.prototype.realLength = function() { return this.replace(/[^\x00-\ ...
随机推荐
- 经典PID控制及应用体会总结
经典PID控制及应用体会总结 PID控制原理 PID是一种线性控制器,它根据给定值rin(t)与实际输出值yout(t)构成控制方案: 重点关注相关算法是如何对偏差进行处理的: PID控制器各校正环节 ...
- Java 最常见 200+ 面试题 + 全解析
本文分为十九个模块,分别是: Java 基础.容器.多线程.反射.对象拷贝.Java Web .异常.网络.设计模式.Spring/Spring MVC.Spring Boot/Spring Clou ...
- (三)Servlet 知识点总结(来自那些年的笔记)
(史上最全知识汇总)转载请贴上原文链接! 作者:淮左白衣 写于 2018年4月15日20:14:55 如果,碰巧你打开了本篇博客,相信我,你想要的servlet知识,这里应该都能找到!! 目录 (史上 ...
- Spring Boot系列教程十四:Spring boot同时支持HTTP和HTTPS
自签证书 openssl生成服务端证书,不使用CA证书直接生成 -in server.csr -signkey server.key -out server.crt # 5.server证书转换成ke ...
- laravel6.0路由
1.基本路由路由定义在routes目录下,路由执行是在控制器之前,路由路径 routes目录下api.php 关于接口路由定义文件包含的路由位于 api 中间件组约束之内,支持频率限制功能,这些路由是 ...
- 在订单服务中使用Hystrix进行熔断设置
使用Hystrix熔断(上) 在一个分布式系统里,一个服务依赖多个服务,可能存在某个服务调用失败, 比如超时.异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败, ...
- WINDOWS记事本的换行\r\n
今天发现,\r\n才能换行,好像记事本不能改.
- asp.net core-11.WebHost的配置
1.添加空的web网站 ,在目录下添加settings.json文件,在控制台上输出json的信息 public class Program { public static void Main(str ...
- array_chunk — 将一个数组分割成多个
说明 array_chunk ( array $array , int $size [, bool $preserve_keys = false ] ) : array 将一个数组分割成多个数组,其中 ...
- MD5加密处理
无论传送过程和存储方式,都是以明文的方式,很不安全!一旦泄漏,将会造成很大的损失! 插件名称jQuery.MD5.js: /** * jQuery MD5 hash algorithm functio ...