英文:
Converting a []byte containing strings into decimal values
问题
我正在尝试将一个字符串中的每个值转换为十进制ASCII值。我首先将字符串转换为[]byte类型,然后想将[]byte的每个元素转换为十进制ASCII值。以下是我的代码:
myArray := []byte(password) // 将字符串转换为[]byte类型
NumArray := [len(password)]int // 创建第二个[]int类型的数组来存储转换后的[]byte元素
for i := 0; i < len(myArray); i++ {
/* 我需要一些帮助将myArray中的每个元素转换为ASCII十进制值,然后存储到NumArray中 */
fmt.Printf("%d\n", myArray[i]) // 打印出转换后的值
fmt.Print(NumArray[i]) // 打印出存储的转换值以进行比较
}
编辑:该字符串应该是一个密码,因此可以包含任何值。
英文:
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
答案1
得分: 0
你可以像这样将 byte
转换为 int
:
NumArray[i] = int(myArray[i])
英文:
You can cast byte
to int
like this:
NumArray[i] = int(myArray[i])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论