切片无法转换为字符串。

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

Slice can not convert to string

问题

为了更好地理解您的问题,请问您需要对代码中的哪一部分进行翻译?

英文:
func (this *AdminModel) Login(v_name string, v_pwd string) (bool, error, uint) {
	o := orm.NewOrm()
	v_pwd_encrypt_byte := md5.Sum([]byte(v_pwd))
	v_pwd_encrypt := string(v_pwd_encrypt_byte[:])
	t_admin := Admin{Name: v_name, Pwd: v_pwd_encrypt}
	fmt.Printf("username:%v  password:%v\n", v_name, v_pwd_encrypt_byte)
	fmt.Printf("username:%v  password:%v\n", v_name, v_pwd_encrypt_byte[:])
	fmt.Printf("username:%v  password:%v\n", v_name, v_pwd_encrypt)
	err := o.Read(&t_admin, "Name", "Pwd")
	if err != nil {
		return false, err, 0
	} else {
		return true, nil, t_admin.Id
	}
}

print result:

username:yuhaya  password:[97 22 175 237 203 11 195 16 131 147 92 28 38 47 244 201]
username:yuhaya  password:[97 22 175 237 203 11 195 16 131 147 92 28 38 47 244 201]
username:yuhaya  password:a???
                          ???\&/??

Why the last line of the printing results gibberish?

v_pwd_encrypt := string(v_pwd_encrypt_byte[:])

Is this position convert out of the question?

答案1

得分: 2

添加到@Ainar-G的答案中,hex.EncodeToString 是最高效的方法,因为它不涉及反射或类型猜测,像 fmt.Sprintf 一样。

func main() {
    sum := md5.Sum([]byte("meh"))
    stringSum := hex.EncodeToString(sum[:])
    fmt.Println(stringSum)
}
英文:

Adding to @Ainar-G's answer, hex.EncodeToString is the most efficient way to do it since it doesn't involve reflection or type guessing like fmt.Sprintf.

func main() {
	sum := md5.Sum([]byte("meh"))
	stringSum := hex.EncodeToString(sum[:])
	fmt.Println(stringSum)
}

答案2

得分: 1

md5.Sum() 返回的是字节,而不是可打印的 ASCII 字符。如果你想要看到这些字节的十六进制表示,你可以使用 fmt.Sprintf("%x", ...),像这样:

v_pwd_encrypt := fmt.Sprintf("%x", v_pwd_encrypt_byte)
英文:

md5.Sum() returns bytes, not printable ASCII characters. If you want to see hex representation of those bytes, you can use fmt.Sprintf("%x", ...), like this:

v_pwd_encrypt := fmt.Sprintf("%x", v_pwd_encrypt_byte)

huangapple
  • 本文由 发表于 2015年1月26日 22:39:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/28152276.html
匿名

发表评论

匿名网友

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

确定