📜 ⬆️ ⬇️

Another way to display strings in Go

Once sitting at work, and writing code on Go, I remembered the old puzzle, the essence of which was that we needed to implement the division of any number by a predetermined in the condition of the problem. It would seem simple, but there were two limitations:

1. You cannot use numbers explicitly, except for 0.
2. The number of allowed mathematical operations was also limited.

I did not repeat it on Go, but decided to apply one of the solutions to display the string. This idea is not new, and I do not claim full-fledged authorship, I just decided to share it.

The bottom line is that the string is divided into bytes, and each byte is converted into a sequence of bit-wise shifts, and XOR and OR operations with one.
')
It looks like this:

EAX = uint8(unsafe.Sizeof(true)) (((EAX<<EAX^EAX)<<EAX|EAX)<<EAX<<EAX<<EAX^EAX)<<EAX) 

So, let's begin. The unsafe module is only needed for the sizeof function. If anyone knows how to get the size of the structure in Go in other ways, I will be glad if you share it.

We make out the sections Import and Const
 import ( "fmt" "math/rand" "time" "unsafe" ) const ( EAX = uint8(unsafe.Sizeof(true)) ONE = "EAX" ) 


Next, you need a function that will be for a given byte, to find and generate such a combination. The simplest and most obvious option was to first calculate the pattern, of the form [0,1,1,0,1,1, ...]. Where 0 means that the number is even, 1 means odd. Decreasing the number each time, performing a bitwise right shift by 1.

Write this in code
 func getNumber(n byte) (buf string) { var arr []byte for n > EAX { if n%2 == EAX { arr = append(arr, EAX) } else { arr = append(arr, 0) } n = n >> EAX } } 


Thus, the length of the slice arr will be equal to the number of steps that must be performed to obtain the initial number.

Let's add the getNumber function
 buf = ONE rand.Seed(time.Now().Unix()) for i := len(arr) - 1; i >= 0; i-- { buf = fmt.Sprintf("%s<<%s", buf, ONE) if arr[i] == EAX { if rand.Intn(2) == 0 { buf = fmt.Sprintf("(%s^%s)", buf, ONE) } else { buf = fmt.Sprintf("(%s|%s)", buf, ONE) } } } 


Running through the slice of arr, we generate a string of “shifts”, and if we meet an odd number, then randomly execute an OR or XOR operation with 1 on this number.

It remains to write the function, which in fact will turn the given string into a sequence of such shifts. As an example, I decided to display the code, executing which you can get the specified string.

Another function
 func TextToCode(txt string) string { b := []byte(txt) tmp := "var str []byte\n" for _, item := range b { tmp = fmt.Sprintf("%s\nstr = append(str, %s)", tmp, getNumber(item)) } tmp += "\nfmt.Println(string(str))" return tmp } 


That's all. After starting, for the line: “Author: GH0st3rs ” we get the following result:

not to look nervous
 var str []byte str = append(str, (EAX<<EAX<<EAX<<EAX<<EAX<<EAX<<EAX^EAX)) str = append(str, ((((EAX<<EAX^EAX)<<EAX|EAX)<<EAX<<EAX^EAX)<<EAX<<EAX^EAX)) str = append(str, (((EAX<<EAX^EAX)<<EAX|EAX)<<EAX<<EAX^EAX)<<EAX<<EAX) str = append(str, ((EAX<<EAX^EAX)<<EAX<<EAX|EAX)<<EAX<<EAX<<EAX) str = append(str, (((((EAX<<EAX^EAX)<<EAX<<EAX|EAX)<<EAX^EAX)<<EAX^EAX)<<EAX|EAX)) str = append(str, (((EAX<<EAX^EAX)<<EAX|EAX)<<EAX<<EAX<<EAX^EAX)<<EAX) str = append(str, (((EAX<<EAX^EAX)<<EAX|EAX)<<EAX<<EAX^EAX)<<EAX) str = append(str, EAX<<EAX<<EAX<<EAX<<EAX<<EAX) str = append(str, EAX<<EAX<<EAX<<EAX<<EAX<<EAX<<EAX) str = append(str, (((EAX<<EAX<<EAX<<EAX<<EAX^EAX)<<EAX|EAX)<<EAX^EAX)) str = append(str, (EAX<<EAX<<EAX<<EAX^EAX)<<EAX<<EAX<<EAX) str = append(str, (EAX<<EAX^EAX)<<EAX<<EAX<<EAX<<EAX) str = append(str, ((((EAX<<EAX^EAX)<<EAX|EAX)<<EAX<<EAX<<EAX^EAX)<<EAX^EAX)) str = append(str, (((EAX<<EAX^EAX)<<EAX|EAX)<<EAX<<EAX^EAX)<<EAX<<EAX) str = append(str, (((EAX<<EAX^EAX)<<EAX<<EAX<<EAX|EAX)<<EAX^EAX)) str = append(str, (((EAX<<EAX^EAX)<<EAX|EAX)<<EAX<<EAX<<EAX^EAX)<<EAX) str = append(str, ((((EAX<<EAX^EAX)<<EAX|EAX)<<EAX<<EAX<<EAX^EAX)<<EAX^EAX)) fmt.Println(string(str)) 


"The source code of the project is available on GitHub

Source: https://habr.com/ru/post/313486/


All Articles