bytes.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 (
  19. "encoding/hex"
  20. )
  21. func ToHex(b []byte) string {
  22. hex := Bytes2Hex(b)
  23. // Prefer output of "0x0" instead of "0x"
  24. if len(hex) == 0 {
  25. hex = "0"
  26. }
  27. return "0x" + hex
  28. }
  29. func FromHex(s string) []byte {
  30. if len(s) > 1 {
  31. if s[0:2] == "0x" || s[0:2] == "0X" {
  32. s = s[2:]
  33. }
  34. }
  35. if len(s)%2 == 1 {
  36. s = "0" + s
  37. }
  38. return Hex2Bytes(s)
  39. }
  40. // Copy bytes
  41. //
  42. // Returns an exact copy of the provided bytes
  43. func CopyBytes(b []byte) (copiedBytes []byte) {
  44. if b == nil {
  45. return nil
  46. }
  47. copiedBytes = make([]byte, len(b))
  48. copy(copiedBytes, b)
  49. return
  50. }
  51. func HasHexPrefix(str string) bool {
  52. l := len(str)
  53. return l >= 2 && str[0:2] == "0x"
  54. }
  55. func IsHex(str string) bool {
  56. l := len(str)
  57. return l >= 4 && l%2 == 0 && str[0:2] == "0x"
  58. }
  59. func Bytes2Hex(d []byte) string {
  60. return hex.EncodeToString(d)
  61. }
  62. func Hex2Bytes(str string) []byte {
  63. h, _ := hex.DecodeString(str)
  64. return h
  65. }
  66. func Hex2BytesFixed(str string, flen int) []byte {
  67. h, _ := hex.DecodeString(str)
  68. if len(h) == flen {
  69. return h
  70. } else {
  71. if len(h) > flen {
  72. return h[len(h)-flen:]
  73. } else {
  74. hh := make([]byte, flen)
  75. copy(hh[flen-len(h):flen], h[:])
  76. return hh
  77. }
  78. }
  79. }
  80. func RightPadBytes(slice []byte, l int) []byte {
  81. if l <= len(slice) {
  82. return slice
  83. }
  84. padded := make([]byte, l)
  85. copy(padded, slice)
  86. return padded
  87. }
  88. func LeftPadBytes(slice []byte, l int) []byte {
  89. if l <= len(slice) {
  90. return slice
  91. }
  92. padded := make([]byte, l)
  93. copy(padded[l-len(slice):], slice)
  94. return padded
  95. }