douwen5985 2017-01-31 15:00
浏览 48
已采纳

如何将StringText转换为二进制并反之使用Go

i want Convert Text(type=String) To Binary(type=String) And Conversely Using Go

some userfull link : Golang: How to convert String to binary representation & Convert string to binary in Go

but i need another.

i want example convert a text like hello to binary. and next can convert then binary to first text(hello).

var hash_text := hash("hello")//example return *****
var unhash_text := unhash(hash_text);//return hello

like this gist.github.com/hutt/8978333 (using php)

also speed for me is importamt.

  • 写回答

1条回答 默认 最新

  • dpndp64206 2017-01-31 16:08
    关注

    Convert each byte individually to its base 2 representation. You can use strconv.ParseUint to convert the base2 back to bytes.

    func stringToBase2(s string) string {
        var buf bytes.Buffer
        for i := 0; i < len(s); i++ {
            fmt.Fprintf(&buf, "%08b", s[i])
        }
        return buf.String()
    }
    
    func base2ToString(s string) string {
        var out []byte
        for i := 0; i+8 <= len(s); i += 8 {
            b, err := strconv.ParseUint(s[i:i+8], 2, 8)
            if err != nil {
                panic(err)
            }
            out = append(out, byte(b))
        }
        return string(out)
    }
    

    https://play.golang.org/p/cLvoPHZ-hH

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?