soapbar
soapbar

Reputation: 2451

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?

Upvotes: 0

Views: 72

Answers (2)

Ainar-G
Ainar-G

Reputation: 36199

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)

Upvotes: 1

OneOfOne
OneOfOne

Reputation: 99224

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)
}

Upvotes: 2

Related Questions