原文: https://yourbasic.org/golang/convert-string-to-byte-slice/#convert-string-to-bytes

---------------------------------------------------------------------------

yourbasic.org/golang

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转成字符串的更多相关文章

  1. Python入门篇-基础数据类型之整型(int),字符串(str),字节(bytes),列表(list)和切片(slice)

    Python入门篇-基础数据类型之整型(int),字符串(str),字节(bytes),列表(list)和切片(slice) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Py ...

  2. Java字节、十进制、十六进制、字符串之间的相互转换

    1. 字节转10进制 直接使用(int)类型转换. /* * 字节转10进制 */ public static int byte2Int(byte b){ int r = (int) b; retur ...

  3. java中字节数组byte[]和字符(字符串)之间的转换

    转自:http://blog.csdn.net/linlzk/article/details/6566124 Java与其他语言编写的程序进行tcp/ip socket通讯时,通讯内容一般都转换成by ...

  4. C#//字节数组转16进制字符串

    //字节数组转16进制字符串 private static string byteToHexStr(byte[] bytes,int length) { string returnStr = &quo ...

  5. 《将一个字符串转换成datetime时,先分析该字符串以获取日期,然后再将每个变量放置到datetime对象中》的解决办法

    我们在写代码时,稍不注意就收到VS那文不对题的错误提示. 最近在项目上碰到了“将一个字符串转换成datetime时,先分析该字符串以获取日期,然后再将每个变量放置到datetime对象中”的这个错误提 ...

  6. C++:怎样把一个int转成4个字节?

    大家都知道,一个int 或 unsigned int是由4个字节组成的,(<C/C++学习指南>,第3章,第3.2.3节:变量的内存视图) 比如, int   n  =  sizeof( ...

  7. 字符串A转换到字符串B,只能一次一次转换,每次转换只能把字符串A中的一个字符全部转换成另一个字符,是否能够转换成功

    public class DemoTest { public static void main(String[] args) { System.)); } /** * 有一个字符串A 有一个字符串B ...

  8. JS的slice、substring、substr字符串截取

    JS中截取一个字符串的三种方法:字符串.slice(开始索引,结束索引)字符串.substring(开始索引,结束索引)字符串.substr(开始索引,截取的长度) 如果需要截取到该字符串的最后,可以 ...

  9. 给定一个字符串,把字符串内的字母转换成该字母的下一个字母,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, 字符串内的其他字 ...

  10. js字符串长度计算(一个汉字==两个字符)和字符串截取

    js字符串长度计算(一个汉字==两个字符)和字符串截取 String.prototype.realLength = function() { return this.replace(/[^\x00-\ ...

随机推荐

  1. IO流学习

    1,流是一组有顺序的,有起点和重点的字节集合,是对数据传输的总称和抽象.即数据在两个设备之间的传输称作流.流的本质就是数据传输,根据数据传输的特性,将流抽象为各种累,方便直观的进行数据操作. 2,根据 ...

  2. TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option

    先解决他 详细不详解 在初始化 加上 --explicit_defaults_for_timestamp=true 即可

  3. 502 BAD GATEWAY-k8s的cgroup限制了apache的可用内存

    1.release的组件逻辑图 2.表象:按F12,总是报502 BAD GATEWAY 3.nginx日志 [error] #: * upstream prematurely closed conn ...

  4. 学习Elasticsearch原理笔记

    Elasticsearch是一个分布式可拓展的实时搜索和分析引擎 分布式实时文件存储,并将每一个字段都编入索引,使其可以被搜索 实时分析的分布式搜索引擎 可以拓展到上百台服务器,处理PB级别的结构化或 ...

  5. In-App Purchase(iap)快速指南

      点击IOS IAP APP内支付 Java服务端代码直接跳转到示例点击直接跳转到示例 iap简介   在应用中内嵌Store,在iOS应用中使用Store Kit framework来实现In-A ...

  6. iOS开发支付篇——内购(IAP)详解

    1 <em>内购所需要的资料整理总结,史上最完整的,哈哈哈哈哈哈</em> 思维导图 重点总结: 1 2 3 4 5 6 7 8 9 10 11 12 13 1.获取内购列表( ...

  7. springboot用controller跳转html页面

    之前SSM框架,里面有webapps文件夹,用来存放前端页面和各种前端资源,现在SpringBoot中没有webapps文件夹,springboot结构如下: 第一.resourses下文件夹publ ...

  8. 开始使用 git(配置+常用命令)

    ▶ 注意 页面显示问题: -- 是两个短横线 - 是一个横短线 由于显示问题导致两个短横线之间没有空格,看起来像是一条横线,实则是两条短横线 ▶ git 常用命令 ◆ git add ● git ad ...

  9. 还是a+b

    题目描述: 给定 2 个正整数 a, b,a 和 b 最多可能有 40 位,求出 a + b 的和.输入描述: 两个正整数 a, b,a 和 b 最多可能有 40 位.一行表示一个数.输出描述: a ...

  10. C++中静态成员函数和普通成员函数存储方式相同

    先从一个示例查看类的创建过程中,静态成员函数和普通成员函数的存储区别. #include "stdafx.h" #include<iostream> #include& ...