binary string to readable string

blov · · 390 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello Guys,</p> <p>do you know a way to parse a binary string into a readable string? Example: &#34;01010100 01100101 01110011 01110100&#34; into &#34;Test&#34;</p> <hr/>**评论:**<br/><br/>popillol: <pre><p>Not sure if this can be shortened, but <a href="https://play.golang.org/p/0PLBonPTm9u" rel="nofollow">here&#39;s an option</a></p> <pre><code>package main import ( &#34;fmt&#34; &#34;strconv&#34; &#34;strings&#34; ) func main() { input := &#34;01010100 01100101 01110011 01110100&#34; b := make([]byte, 0) for _, s := range strings.Fields(input) { n, _ := strconv.ParseUint(s, 2, 8) b = append(b, byte(n)) } fmt.Println(string(b)) } </code></pre></pre>aNNdii: <pre><p>Thank you, had the same approach. I thought there is a native functions to do this kind of things. :-) </p> <p><a href="https://play.golang.org/p/V1JPZ1zI8Pb" rel="nofollow">Here is my solution, based on yours.</a></p> <pre><code>package main import ( &#34;fmt&#34; &#34;regexp&#34; &#34;strconv&#34; ) func main() { input := &#34;01010100011001010111001101110100&#34; r, _ := regexp.Compile(&#34;[0|1]{8}&#34;) match := r.FindAllString(input, -1) b := make([]byte, 0) for _, s := range match { n, _ := strconv.ParseUint(s, 2, 8) b = append(b, byte(n)) } fmt.Println(string(b)) } </code></pre></pre>templarrei: <pre><p>A slightly revised (optimised) version would be: <a href="https://play.golang.org/p/tRQP9WnOe40" rel="nofollow">https://play.golang.org/p/tRQP9WnOe40</a></p> <p>EDIT: Updated the link to drop my own experiments :D</p></pre>TUSF: <pre><p>Just letting you know, but:</p> <blockquote> <p>prog.go:12:7: t declared and not used</p> </blockquote></pre>templarrei: <pre><p>Thanks, mate, dropped :D</p></pre>robpike: <pre><p>This is one of the few occasions when Scanf (actually Sscanf) is worthwhile. <a href="https://play.golang.org/p/EhhBfcaqA9b" rel="nofollow">https://play.golang.org/p/EhhBfcaqA9b</a></p></pre>nikajon_es: <pre><p>You can also use the big number library. Which may be more memory, but easier to follow.</p> <pre><code>func main() { i := new(big.Int) i.SetString(&#34;0b01010100011001010111001101110100&#34;, 0) // inferred fmt.Println(string(i.Bytes())) } </code></pre> <p><a href="https://play.golang.org/p/wUWbCJ556A1" rel="nofollow">https://play.golang.org/p/wUWbCJ556A1</a></p></pre>claudiodcsilva: <pre><p>And for least clear code of the day:</p> <pre><code>package main import ( &#34;fmt&#34; ) func main() { input := &#34;01010100011001010111001101110100&#34; b := []byte(input) a := make([]byte, (len(b)/8)+ len(b)%8 - int(uint(len(b)%8 - 1))) for i := 0; i &lt; len(b); i++ { a[i/8] |= ((b[i] - 48) &lt;&lt; uint(7-i%8)) } fmt.Println(string(a)) } </code></pre></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

390 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传