-2

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.

Community
  • 1
  • 1
Love Python
  • 25
  • 2
  • 4
  • what do you mean by "binary"? Your example of changing a string to `*****` and magically transforming it back doesn't make any sense – JimB Jan 31 '17 at 15:03
  • like this https://gist.github.com/hutt/8978333 (using php) – Love Python Jan 31 '17 at 15:10
  • I still don't understand. A string is just a slice of bytes, and can be converted directly to `[]byte`, it already is as "binary" as you can get. Please show exactly the output you expect. – JimB Jan 31 '17 at 15:16
  • `1101000 1100101 1101100 1101100 1101111` == `hello` in this php source – Love Python Jan 31 '17 at 15:19
  • https://play.golang.org/p/5I2DIWisKF? – JimB Jan 31 '17 at 15:21
  • how store %b without [ and ] character? and also delete `\s`(space) ? not have optional then auto delete? replace may not good for speed. – Love Python Jan 31 '17 at 15:33
  • i think this is not good. i want a function then for all char of text and convert every char. then i can change , and add more hash on them. and speed important. – Love Python Jan 31 '17 at 15:34
  • What is the purpose of this? This isn't a "hash" as you've called it here, it's simply changing the string representation, so you're going to be limited by manipulating strings. – JimB Jan 31 '17 at 15:35
  • yes , is right. but next i want hash them.... – Love Python Jan 31 '17 at 15:38
  • i want two function like [PHP Script](http://gist.github.com/hutt/8978333) for Go. – Love Python Jan 31 '17 at 15:38
  • `` and `` how do this in GO? – Love Python Jan 31 '17 at 15:44
  • FYI, there's absolutely no useful reason to change the string representation like this for hashing, and looks like a gross misunderstanding of how to securely hash a string. Is this what you want to do? https://play.golang.org/p/SnyWzJlTTF – JimB Jan 31 '17 at 15:51
  • woow..tank you.very tank from you. – Love Python Jan 31 '17 at 15:54
  • but not work for utf-8 text!! how fix? – Love Python Jan 31 '17 at 15:55
  • You need to _not_ try to use it as utf8, and index the string bytes directly. I'll post it so you can see it exactly. – JimB Jan 31 '17 at 16:07

1 Answers1

2

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

JimB
  • 104,193
  • 13
  • 262
  • 255
  • `need try,catch to not show error if not binary`.. example : `fmt.Println(base2ToString("jdfgidjfigjdfidfg"))` how fix this? show error : `panic: strconv.ParseUint: parsing "jdfgidjf": invalid syntax`??? how fix? – Love Python Jan 31 '17 at 16:14
  • I purposely made it panic on invalid input. If you want to catch all errors, you are free to do so. – JimB Jan 31 '17 at 16:16
  • 1
    instead of `panic`, return the error and handle it. Have you gone over the basics of Go? Maybe take the [Tour of Go](https://tour.golang.org/) – JimB Jan 31 '17 at 16:22
  • 2
    @LovePython: `adler32` is a checksum, you can't "decode" it. Do you mean [`adler32.Checksum`](https://godoc.org/hash/adler32#Checksum)? Please take some time to learn the basics and how to check the documentation. – JimB Jan 31 '17 at 17:58
  • oh, Ok. i have a text(number). how can minify this? what hash is better and minify? – Love Python Feb 01 '17 at 11:57
  • and also about `stringToBase2()` you sure then all character utf-8 max have `%08b` 8 bit/byte? – Love Python Feb 01 '17 at 11:59
  • @LovePython: stringToBase2 doesn't have anything to do with utf8. A byte is 8 bits, and you're converting the string's bytes into a base 2 representation. Again, this is a pointless exercise, and amounts only to a minor obfuscation of some sort. – JimB Feb 01 '17 at 18:32
  • @LovePython: what do you mean by "minify"? Minify implies compression, but a hash is by definition a lossy one-way transformation. – JimB Feb 01 '17 at 18:33
  • yes. now i have a string , and i want compression them. what algorithm good for compression? – Love Python Feb 01 '17 at 22:35