How to print a byte array as binary in golang? - arrays

How to print a byte array []byte{255, 253} as binary in Golang?
I.e.
[]byte{255, 253} --> 1111111111111101

Simplest way I have found:
package main
import "fmt"
func main() {
bs := []byte{0x00, 0xfd}
for _, n := range(bs) {
fmt.Printf("% 08b", n) // prints 00000000 11111101
}
}
Playground with this code: https://play.golang.org/p/eVez0vD4pJk

Or use this simple version
func printAsBinary(bytes []byte) {
for i := 0; i < len(bytes); i++ {
for j := 0; j < 8; j++ {
zeroOrOne := bytes[i] >> (7 - j) & 1
fmt.Printf("%c", '0'+zeroOrOne)
}
fmt.Print(" ")
}
fmt.Println()
}
[]byte{0, 1, 127, 255} --> 00000000 00000001 01111111 11111111

Related

How to sort an array

I have problem for sorting my array in Go,
Here is my code :
func main() {
fmt.Print("Masukkan Jumlah Data yang akan dimasukkan: ")
var jumlahdata int
fmt.Scanln(&jumlahdata)
var DataDiagram = make([]int, jumlahdata)
fmt.Print("Masukkan data secara berurutan dengan spasi sebagai pemisah antar angka: ")
for i := 0; i < jumlahdata; i++ {
fmt.Scanf("%d", &DataDiagram[i])
}
fmt.Print("\n")
var max int = DataDiagram[0]
for _, value := range DataDiagram { // Menemukan nilai maximum
if value > max {
max = value
}
}
var mem int
Sorting(DataDiagram, jumlahdata, mem, max)
}
func Grafik(jumlahdata int, max int, DataDiagram []int) {
for i := max; i >= 1; i-- { // membuat Data Diagram
for j := 0; j < jumlahdata; j++ {
if DataDiagram[j] >= i {
fmt.Print(" | ")
} else {
fmt.Print(" ")
}
}
fmt.Print("\n")
}
for i := 0; i < jumlahdata; i++ {
fmt.Print("---")
}
fmt.Print("\n")
fmt.Print(" ")
for i := 0; i < jumlahdata; i++ {
fmt.Print(DataDiagram[i], " ")
}
}
func Sorting(DataDiagram []int, jumlahdata int, mem int, max int) {
for langkah := 0; langkah < (jumlahdata-1) ; langkah++ {
Grafik(jumlahdata, max, DataDiagram)
for i := 0; i < jumlahdata - (langkah-1); i++ {
if DataDiagram[i] > DataDiagram[i + 1] {
mem := DataDiagram[i];
DataDiagram[i] = DataDiagram[i + 1]
DataDiagram[i + 1] = mem;
}
}
}
}
What i expect is look like this:
What I Expect
But the output said otherwise, it give me error : It give me error
Can someone give some guide how to fix this :) i just learn Go yesterday, it similiar to C, but keep giving me index out of range error
I understand your task is to sort an int "array" (slice, in go-speak), showing each step of your work as a graph. Because you must show your work, you can't use go's built-in sorting, e.g. sort.Ints(DataDiagram).
Your problems are with the Sorting function.
Step 1 Your immediate crash-causing problem is that i eventually iterates to a number larger than upper index of DataDiagram. That we fix in the commented line below.
// Step 1: fix the iterator
func Sorting(DataDiagram []int, jumlahdata int, mem int, max int) {
for langkah := 0; langkah < (jumlahdata-1) ; langkah++ {
Grafik(jumlahdata, max, DataDiagram)
for i := 0; i < jumlahdata - 1; i++ { // Was: for i := 0; i < jumlahdata - (langkah-1); i++ {
if DataDiagram[i] > DataDiagram[i + 1] {
mem := DataDiagram[i];
DataDiagram[i] = DataDiagram[i + 1]
DataDiagram[i + 1] = mem;
}
}
}
}
Step 2 The code no longer crashes, but is not guaranteed to sort, because it makes only one pass through the inputs. We need to continue looping until there's no more swapping taking place. That problem is fixed below. The code now produces the expected output on the playground.
// Step 2: loop until sorted
func Sorting(DataDiagram []int, jumlahdata int, mem int, max int) {
swapped := true
for swapped {
Grafik(jumlahdata, max, DataDiagram)
swapped = false
for i := 0; i < jumlahdata - 1; i++ {
if DataDiagram[i] > DataDiagram[i + 1] {
mem := DataDiagram[i];
DataDiagram[i] = DataDiagram[i + 1]
DataDiagram[i + 1] = mem;
swapped = true
}
}
}
}
Step 3 The above code works fine, but perhaps can use some tidying. The end result is unchanged on the playground.
// Step 3: make it prettier
func Sorting(data []int) {
max := data[0]
for _, value := range data { // Menemukan nilai maximum
if value > max {
max = value
}
}
swapped := true
for swapped {
Grafik(len(data), max, data)
swapped = false
for i := 0; i < len(data)-1; i++ {
if data[i] > data[i+1] {
data[i], data[i+1] = data[i+1], data[i]
swapped = true
}
}
}
}
It's much simpler if you would just use
sort.Ints(ints), which you can see here:
https://goplay.space/#i9VIrDG-vL-

Finding the sum of nearest smallest and greatest number in an array

The question is straightforward that we want to find the sum of the nearest smallest and greatest number for every index of an array. But the part which I am not able to get is how to return the resulting array with the sum values on the same index values as the numbers.
Example: [1,5,2,3,8]
The sum for each indexes is as follows:
for index 0 the element is 1 hence sum = (0 + 2) = 2
since it has no smallest number therefore taking the nearest smallest number to be 0.
similarly for index 1 sum = (3 + 8 ) = 11 {3 - nearest smallest number to 5 and 8 nearest largest number}
and so on.
What I did was sort the given array then iterate through it and in every iteration take the sum of arr[i-1] + arr[i+1] elements and storing them in the result/answer array.{ with 0th and last element being dealt with separately }
So basically
If the input array is -> [1,5,2,3,8]
the resultant array will be -> [2,4,7,11,5]
but it is required to be as -> [2,11,4,7,5]
that is the sum of each index element to be at the same index as was the initial number
(I am using C++)
You will get the desired output by running this. It is written in Go.
Copy the original input array
Sort the copied array
Create a Map Sorted Value -> Sorted Index
make an output array with same length as input
Iterate through the Input Array and check the value is largest or smallest and Do the Sum as described in the question
store the Sum in the output array in the same index as input array
package main
import (
"fmt"
"sort"
)
func main() {
indexes := []int{1, 5, 2, 3, 8}
output := findSum(indexes)
fmt.Println(output)
}
func findSum(input []int) []int {
if len(input) < 2 {
return input
}
sorted := make([]int, len(input))
copy(sorted, input)
sort.Slice(sorted, func(i, j int) bool {
return sorted[i]< sorted[j]
})
sortedMap := make(map[int]int)
for i, i2 := range sorted {
sortedMap[i2] = i
}
output := make([]int, len(input))
for j, index := range input {
i := sortedMap[index]
if i == 0 {
output[j] = sorted[i+1]
continue
}
if i == len(input) - 1 {
output[j] = sorted[i-1]
continue
}
output[j] = sorted[i - 1] + sorted[i + 1]
}
return output
}
You can run here
The trick is to create an array of indexes into the original array and sort this, rather than sorting the original array.
You don't mention what language you're using, but here's some Java code to illustrate:
Integer[] arr = {1,5,2,3,8};
System.out.println(Arrays.toString(arr));
Integer[] idx = new Integer[arr.length];
for(int i=0; i<idx.length; i++) idx[i] = i;
System.out.println(Arrays.toString(idx));
Arrays.sort(idx, (a, b) -> arr[a] - arr[b]);
System.out.println(Arrays.toString(idx));
Integer[] res = new Integer[arr.length];
for(int i=0; i<arr.length; i++)
{
res[idx[i]] = (i > 0 ? arr[idx[i-1]] : 0) + (i < arr.length - 1 ? arr[idx[i+1]] : 0);
}
System.out.println(Arrays.toString(res));
Or translated into (possibly non-idiomatic) C++ (Ideone):
int len = 5;
array<int, 5> arr{1,5,2,3,8};
for(auto i : arr) cout << i << " ";
cout << endl;
array<int, 5> idx;
for(int i=0; i<len; i++) idx[i] = i;
for(auto i : idx) cout << i << " ";
cout << endl;
sort(idx.begin(), idx.end(), [&arr](int a, int b) {return arr[a] < arr[b];});
for(auto i : idx) cout << i << " ";
cout << endl;
array<int, 5> res;
for(int i=0; i<len; i++)
res[idx[i]] = (i > 0 ? arr[idx[i-1]] : 0) + (i < len - 1 ? arr[idx[i+1]] : 0);
for(auto i : res) cout << i << " ";
cout << endl;
Output:
[1, 5, 2, 3, 8]
[0, 1, 2, 3, 4]
[0, 2, 3, 1, 4]
[2, 11, 4, 7, 5]

Converting something to bytes

I want to convert a variable (I took an int for this example) to a byte using this code that I have found:
func IntToByteArray(num int64) []byte {
size := int(unsafe.Sizeof(num))
arr := make([]byte, size)
for i := 0 ; i < size ; i++ {
byt := *(*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(&num)) + uintptr(i)))
arr[i] = byte(byt)
}
return arr
}
func main(){
println(IntToByteArray(1456))
}
But the output that it gives me is this one : [8/8]0xc00001a0d0
Can some one explain me why do I have this has a result?
And what is exactly a byte array?
package main
import "fmt"
func IntToByteArray(num int64) []byte {
r := make([]byte, 8)
for i := 0; i < len(r); i++ {
r[i] = byte(num >> (i * 8) & 255)
}
return r
}
func main() {
fmt.Println(IntToByteArray(65280))
}
This assumes little-endianness.
As others have suggested, the included packages are more flexible and tested.

How to derive an list of structs from any struct type - getting from interface{} to a variable length slice []interface{}

I try to implement a function taking (any) structure, returning an array of those structures. ReturnArrayOfStory show the idea with a fixed type struct type.
trying to do the same for any type with function ReturnArrayOfX and reflection fails at compile time.
package main
import (
"fmt"
"reflect"
)
type story_t struct {
LANGUAGE string
SPECIES string
}
func ReturnArrayOfStory(x story_t) []story_t {
x1 := x
var a1 []story_t
a1 = append(a1, x1)
a1 = append(a1, x1)
a1 = append(a1, x1)
return a1
}
func ReturnArrayOfX(x interface{}) []interface{} {
x1 := x
v1 := reflect.ValueOf(&x1).Elem()
a1 := []reflect.TypeOf(&x1)
// var a1 []x
a1 = append(a1, x1)
a1 = append(a1, x1)
a1 = append(a1, x1)
//return a1
return a1
}
func main() {
var as1 []story_t
s1 := story_t{"EN", "Prince of Persia"}
as1 = ReturnArrayOfStory(s1)
//as1 := ReturnArrayOfX(s1)
for i := 0; i < len(as1); i++ {
fmt.Printf("%02d %+v\n", i, as1[i])
}
as2 := ReturnArrayOfX(s1)
//as1 := ReturnArrayOfX(s1)
for i := 0; i < len(as2); i++ {
fmt.Printf("%02d %+v\n", i, as2[i])
}
}
a1 := []reflect.TypeOf(&x1)
main.go:25:8: reflect.TypeOf is not a type
This is a simplified scenario. In reality, I like to read a multitude of struct types from an external data source like a database.
How can I came to my goal with ReturnArrayOfX?
List item Is this possible? If not,why?
There are two solutions to your problem:
First: if you want to return a slice of a type using reflection:
// You cannot return []interface{}, because this function will return [](type of x), and that is not []interface{}
func ReturnArrayOfX(x interface{}) interface{} {
x1 := x
a1 :=
// this creates *[](typeOf x)
reflect.New(reflect.SliceOf(reflect.TypeOf(x)))
// Append the first element to *[](typeof x)
// after this, a1 now points to a slice, not to a slice *
a1 = reflect.Append(a1.Elem(), reflect.ValueOf(x1))
a1 = reflect.Append(a1, reflect.ValueOf(x1))
a1 = reflect.Append(a1, reflect.ValueOf(x1))
//return [](typeof x)
return a1.Interface()
}
You can use this as:
as2 := ReturnArrayOfX(s1)
arr:=as2.([]story_t)
for i := 0; i < len(arr); i++ {
fmt.Printf("%02d %+v\n", i, arr[i])
}
Second: you can return []interface{} without reflection:
func ReturnArrayOfX(x interface{}) []interface{} {
ret:=make([]interface{},0)
ret=append(ret,x)
ret=append(ret,x)
ret=append(ret,x)
}
Then you need to deal with each element of the array:
as2 := ReturnArrayOfX(s1)
for i := 0; i < len(as2); i++ {
fmt.Printf("%02d %+v\n", i, as2[i])
data:=as2[i].(story_t)
}
Here's a generic slice conversion function:
// convertSlice copies the slice in src to the slice pointed to by pdst.
// The concrete values in src must be assignable to the dst elements.
func convertSlice(pdst interface{}, src interface{}) {
dstv := reflect.ValueOf(pdst).Elem()
srcv := reflect.ValueOf(src)
dstv.Set(reflect.MakeSlice(dstv.Type(), srcv.Len(), srcv.Len()))
for i := 0; i < srcv.Len(); i++ {
dstv.Index(i).Set(reflect.ValueOf(srcv.Index(i).Interface()))
}
}
Use it like this:
// Convert []story_t to []interface{}
s0 := []story_t{{"EN", "Prince of Persia"}, {"EN", "Karateka"}}
var s1 []interface{}
convertSlice(&s1, s0)
// Convert []interface{} containing story_t to []story_t
var s2 []story_t
convertSlice(&s2, s1)
Run it on the playground.

Sum of squares in an array using recursion in golang

So my friend gave me this task where the sum of squares of positive numbers must be calculated using recursion.
Conditions - The input will be a string with space separated numbers
This is what I've come so far but this shows a runtime error.
Here is the full error https://ideone.com/53oOjN
package main
import(
'fmt',
'strings',
'strconv'
)
var n int = 4
var sum_of_squares int = 0
func sumOfSquares(strArray []string, iterate int) int{
number, _ := strconv.Atoi(strArray[iterate])
if number > 0 {
sum_of_squares += number*number
}
if iterate == n {
return 0 // just to end the recursion
}
return sumOfSquares(strArray, iterate+1)
}
func main() {
str := "1 2 3 4"
strArray := strings.Fields(str)
result := sumOfSquares(strArray, 0)
fmt.Println(sum_of_squares, result)
}
The rule of thumb in recursion is termination condition. It should exist and it should exist in the right place.
func sumOfSquares(strArray []string, iterate int) int{
if iterate >= len(strArray) {
return sum_of_squares
}
number, _ := strconv.Atoi(strArray[iterate]) //TODO: handle err here
sum_of_squares += number*number
return sumOfSquares(strArray, iterate+1)
}
Just for you information: canonical recursion should not save it's state into global fields. I would suggest using following function signature.
func sumOfSquares(strArray []string, iterate, currentSum int) int{
//...
return sumOfSquares(strArray, iterate+1, sum_of_squares)
}
So that you don't need to store sum_of_squares somewhere. You will just pass it to next function invocation.
package main
import (
"fmt"
"strconv"
"strings"
)
var n int
func sumOfSquares(strArray []string, iterate int) int {
number, _ := strconv.Atoi(strArray[iterate])
if iterate == n {
return number * number
}
return ((number * number) + sumOfSquares(strArray, iterate+1))
}
func main() {
str := "1 2 3 4"
strArray := strings.Fields(str)
n = len(strArray) - 1
result := sumOfSquares(strArray, 0)
fmt.Println(result)
}
Indexing starts from 0, so decrease the length by one.
As #peterSO have pointed out, if strings contain unusual characters, it doesn't work, I didn't post the right answer for getting input because you seem to be beginner, but you can read the input, like this instead.
var inp []byte
var loc int
inp, _ = ioutil.ReadFile(fileName)
//add \n so that we don't end up running out of bounds,
//if last byte is integer.
inp = append(inp, '\n')
func scanInt() (res int) {
if loc < len(inp) {
for ; inp[loc] < 48 || inp[loc] > 57; loc++ {
}
for ; inp[loc] > 47 && inp[loc] < 58; loc++ {
res = res<<3 + res<<1 + (int(inp[loc]) - 48)
}
}
return
}
This is faster and scans integers only, and skips all other unusual characters.
I like to keep it simple. I have some few if conditions as well, but hope you like it.
func sumOfSquares(numArr []string) int {
i, err := strconv.Atoi(numArr[0])
rest := numArr[1:]
//Error checking
if err != nil {
fmt.Println(err)
os.Exit(1)
return 0
}
square := i * i
// negative & last number
if i < 0 && len(rest) == 0 {
return square
}
// negative & not last number
if i < 0 && len(rest) > 0 {
return sumOfSquares(rest)
}
// last man standing
if i >= 0 && len(rest) == 0 {
return square
}
return square + sumOfSquares(rest)
}
DEMO : https://play.golang.org/p/WWYxKbvzanJ

Resources