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

huangapple go评论62阅读模式
英文:

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 &lt; 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(&quot;%d\n&quot;, 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])

huangapple
  • 本文由 发表于 2016年4月3日 07:25:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/36379852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定