In golang, how can I convert a string to binary string? Example: 'CC' becomes 10000111000011
3条回答 默认 最新
- douzhun8615 2016-05-20 15:42关注
This is a simple way to do it:
func stringToBin(s string) (binString string) { for _, c := range s { binString = fmt.Sprintf("%s%b",binString, c) } return }
As I included in a comment to another answer you can also use the variant
"%s%.8b"
which will pad the string with leading zeros if you need or want to represent 8 bits... this will however not make any difference if your character requires greater than 8 bit to represent, such as Greek characters:Φ 1110100110
λ 1110111011
μ 1110111100
Or these mathematical symbols print 14 bits:
≠ 10001001100000
⊂ 10001010000010
⋅ 10001011000101
So caveat emptor: the example herein is meant as a simple demonstration that fulfills the criteria in the original post, not a robust means for working with base2 representations of Unicode codepoints.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
- ¥15 如何在scanpy上做差异基因和通路富集?
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配
- ¥15 Power query添加列问题
- ¥50 Kubernetes&Fission&Eleasticsearch
- ¥15 報錯:Person is not mapped,如何解決?