Last active
January 7, 2018 07:24
-
-
Save johwanghee/d71cd54050bb923830df29d183c93402 to your computer and use it in GitHub Desktop.
golang hex sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/hex" | |
"fmt" | |
) | |
func main() { | |
bs := make([]byte, 7) | |
copy(bs[:], "0000003") | |
fmt.Println("END : " + hex.EncodeToString(bs)) | |
} | |
func GetString(data []byte) string { | |
return string(data) | |
} | |
func GetHexString(data []byte) string { | |
return hex.EncodeToString(data) | |
} | |
func IntegertoBytes(value int, lenth int) []byte { | |
result := make([]byte, lenth) | |
if lenth > 4 { | |
binary.BigEndian.PutUint64(result, uint64(value)) | |
} else if lenth > 2 { | |
binary.BigEndian.PutUint32(result, uint32(value)) | |
} else { | |
binary.BigEndian.PutUint16(result, uint16(value)) | |
} | |
return result | |
} | |
func StringtoBytes(value string, lenth int) []byte { | |
result := make([]byte, lenth) | |
copy(result[:], value) | |
return result | |
} | |
func appendBytes(datas ...[]byte) []byte { | |
result := datas[0] | |
i := 1 | |
for i < len(datas) { | |
result = append(result, datas[i]...) | |
i++ | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment