Converting a []byte containing strings into decimal values - go

I am trying to take a string and convert each value in the string into the decimal ASCII value. I first converted the string into the []byte type and i want to take each element of the []byte and convert it into decimal ASCII value. Here is my code:
myArray := []byte(password) // convert string into []byte type
NumArray := [len(password)]int // create second []int type to store the converted []byte elements
for i := 0; i < len(myArray); i++{
/* I need some help to convert each element in myArray into ASCII decimal value and then store it into
NumArray.
*/
fmt.Printf("%d\n", myArray[i]) //prints out what the converted values should be
fmt.Print(NumArray[i]) //prints out the stored converted value for comparison
}
Edit: the string is supposed to be a password and so can contain any value

You can cast byte to int like this:
NumArray[i] = int(myArray[i])

Related

Convert single string which has an array of individual mixed data types

How to convert the single string which has an array of individual strings in it.
func main() {
stringArrayInString := "[\"Hello\",\"Hai\",\"How are you!\"]" //Single string which has string array in it
fmt.Println(stringArrayInString)
// code to convert to the string array
// convertedString
}
The output has to be like this
convertedString[0] = "Hello"
convertedString[1] = "Hai"
convertedString[2] = "How are you!"
Is this possible if the above input string has mixed data types such as int, string, JSON type
For example
stringArrayInString := "[\"Hello\",\"{\"msg\":\"Hai\"}\",123]"
//after converting
convertedString[0] = "Hello"
convertedString[1] = "{\"msg\":\"Hai\"}"
convertedString[2] = 123
The string array you have is a valid JSON array, so you can do this:
var convertedString []string
json.Unmarshal([]byte(str),&convertedString)
If you have multiple data types in that array, you can do that with a string array, you need an interface{} array:
var convertedData []interface{}
json.Unmarshal([]byte(str),&convertedData)
Then you need to check types of individual elements in that array to find out what they are.

How to convert int16 to hex-encoded string in Golang

I need to convert the data format of an int16 to a string representing its hexadecimal value.
I have tried some hex converters but they change the data instead of changing the formatting. I need it to be a string representation of its hexadecimal value.
data := (data from buffer)
fmt.Printf("BUFFER DATA : %X\n", data) // output print on screen D9DC (hex)
fmt.Println(("BUFFER DATA : ", string(data)) // output print on screen 55772 (dec)
fmt.Println(("BUFFER DATA : ", data) // output print on screen [?]
How can I convert the data format so it prints D9DC with fmt.Println?
Full code here https://play.golang.org/p/WVpMb9lh1Rx
Since fmt.Println doesn't accept format flags, it prints each variable depending on its type.
crc16.Checksum returns an int16, so fmt.Println will display the integer value of your hexadecimal string, which is 55772.
If you want fmt.Println to print D9DC instead of the integer value, you have multiple choices.
Convert your integer into a string that contains the hexadecimal value (which means if you change your integer, you will need to convert it into a string again before using it
Create your own type with a String() method, which is an integer but is represented by its hexadecimal value when printed.
For the second option, your type could be something like this:
type Hex int16
func (h Hex) String() string {
return strconv.FormatInt(int64(h), 16)
}
fmt.Println will automatically use this method because it means the Hex type implements the Stringer interface. For more info on this, here are some resources:
https://tour.golang.org/methods/17
https://golang.org/pkg/fmt/#Stringer

How to convert uint8 slice to string

What's the best way to convert from []uint8 to string?
I'm using http://github.com/confluentinc/confluent-kafka-go/kafka
To read events from kafka. But it does not return plain string event.
It returns event with type []uint8.
How can I convert this event from []uint8 to string?
byte is an alias for uint8, which means that a slice of uint8) (aka []uint8) is also a slice of byte (aka []byte).
And byte slices and strings are directly convertible, due to the fact that strings are backed by byte slices:
myByteSlice := []byte{ ... } // same as myByteSlice := []uint8{ ... }
myString := string(myByteSlice) // myString is a string representation of the byte slice
myOtherSlice := []byte(myString) // Converted back to byte slice

parenthesis and curly braces difference in golang

a := []byte("H") //works
a := []byte{"H"} //does not compile
What is the conceptual difference between () and {} as used above?
The reason is the difference between type conversions and slice literals.
_ = []byte("Hi!") // Converts a string literal to a []byte.
_ = []byte{'H', 'i', '!'} // Initializes a []byte literal
Your second example []byte{"H"} fails to compile because "H" is a string literal that is being used in place of a rune literal, it's comparable to trying to assign a string to a byte typed variable:
var y byte = 'H' // OK
var x byte = "H" // ERROR: cannot use "H" (type string) as type byte in assignment
In the first one a := []byte("H") you are type casting the string "H" into a byte array.
In the second one a := []byte{"H"} you are defining a byte array and assigning "H" as it's first value, which is invalid.
You can compare the second one with defining a string array:
s := []string{"hello","world",".."} // works
f := []string{1,2,4} // fails because the datatype is wrong

how to convert a string slice to rune slice

how can I convert type []string to []rune?
I know you can do it like this:
[]rune(strings.Join(array,""))
but is there a better way?
I would prefer not to use strings.Join(array,"") for this purpose because it builds one big new string I don't need. Making a big string I don't need is not space-efficient, and depending on input and hardware it may not be time-efficient.
So instead I would iterate through the array of string values and convert each string to a rune slice, and use the built-in variadic append function to grow my slice of all rune values:
var allRunes []rune
for _, str := range array {
allRunes = append(allRunes, []rune(str)...)
}

Resources