How to convert an ASCII character into a hex value in Go

2 Answers

0 votes
package main

import (
    "fmt"
)

func main() {
    ch := 'A' 
    
    hexValue := fmt.Sprintf("%x", ch)
    
    fmt.Printf("The hexadecimal value of '%c' is 0x%s", ch, hexValue);
}



/*
run:

The hexadecimal value of 'A' is 0x41

*/

 



answered Dec 1, 2024 by avibootz
0 votes
package main

import (
    "encoding/hex"
    "fmt"
)

func main() {
    ch := 'A' 
    
    byteValue := []byte(string(ch))
    
    hexValue := hex.EncodeToString(byteValue)
    
    fmt.Printf("The hexadecimal value of '%c' is 0x%s", ch, hexValue);
}



/*
run:

The hexadecimal value of 'A' is 0x41

*/

 



answered Dec 1, 2024 by avibootz

Related questions

1 answer 131 views
1 answer 119 views
1 answer 135 views
1 answer 129 views
1 answer 109 views
...