bytes.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package common contains various helper functions.
  17. package common
  18. import "encoding/hex"
  19. func ToHex(b []byte) string {
  20. hex := Bytes2Hex(b)
  21. // Prefer output of "0x0" instead of "0x"
  22. if len(hex) == 0 {
  23. hex = "0"
  24. }
  25. return "0x" + hex
  26. }
  27. func FromHex(s string) []byte {
  28. if len(s) > 1 {
  29. if s[0:2] == "0x" || s[0:2] == "0X" {
  30. s = s[2:]
  31. }
  32. }
  33. if len(s)%2 == 1 {
  34. s = "0" + s
  35. }
  36. return Hex2Bytes(s)
  37. }
  38. // Copy bytes
  39. //
  40. // Returns an exact copy of the provided bytes
  41. func CopyBytes(b []byte) (copiedBytes []byte) {
  42. if b == nil {
  43. return nil
  44. }
  45. copiedBytes = make([]byte, len(b))
  46. copy(copiedBytes, b)
  47. return
  48. }
  49. func hasHexPrefix(str string) bool {
  50. return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
  51. }
  52. func isHexCharacter(c byte) bool {
  53. return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
  54. }
  55. func isHex(str string) bool {
  56. if len(str)%2 != 0 {
  57. return false
  58. }
  59. for _, c := range []byte(str) {
  60. if !isHexCharacter(c) {
  61. return false
  62. }
  63. }
  64. return true
  65. }
  66. func Bytes2Hex(d []byte) string {
  67. return hex.EncodeToString(d)
  68. }
  69. func Hex2Bytes(str string) []byte {
  70. h, _ := hex.DecodeString(str)
  71. return h
  72. }
  73. func Hex2BytesFixed(str string, flen int) []byte {
  74. h, _ := hex.DecodeString(str)
  75. if len(h) == flen {
  76. return h
  77. }
  78. if len(h) > flen {
  79. return h[len(h)-flen:]
  80. }
  81. hh := make([]byte, flen)
  82. copy(hh[flen-len(h):flen], h[:])
  83. return hh
  84. }
  85. func RightPadBytes(slice []byte, l int) []byte {
  86. if l <= len(slice) {
  87. return slice
  88. }
  89. padded := make([]byte, l)
  90. copy(padded, slice)
  91. return padded
  92. }
  93. func LeftPadBytes(slice []byte, l int) []byte {
  94. if l <= len(slice) {
  95. return slice
  96. }
  97. padded := make([]byte, l)
  98. copy(padded[l-len(slice):], slice)
  99. return padded
  100. }