将包含字符串的[]字节转换为十进制值

时间:2016-04-02 23:25:12

标签: go

我正在尝试获取字符串并将字符串中的每个值转换为十进制ASCII值。我首先将字符串转换为[]字节类型,我想取[]字节的每个元素并将其转换为十进制ASCII值。这是我的代码:

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
}

编辑:字符串应该是密码,因此可以包含任何值

1 个答案:

答案 0 :(得分:0)

您可以将byte投射到int,如下所示:

NumArray[i] = int(myArray[i])