size
Returns the size of a hex string or byte slice in bytes
size
Returns the size of a hex string or byte slice in bytes.
Import
import "github.com/ChefBingbong/viem-go/utils/data"Usage
import "github.com/ChefBingbong/viem-go/utils/data"
// Get size of hex stringsize := data.SizeHex("0x0102")// 2
// Get size of byte slicesize := data.SizeBytes([]byte{0x01, 0x02, 0x03})// 3
// Generic size function (works with both)size := data.Size("0x0102")// 2
size = data.Size([]byte{0x01, 0x02})// 2Returns
- Type:
int
The size in bytes. For hex strings, calculates the byte length from hex characters (rounds up for odd lengths).
Parameters
value (required)
- Type:
stringforSizeHex,[]byteforSizeBytes,anyforSize
The hex string or byte slice to measure.
// Hex string sizesize := data.SizeHex("0x0102")// 2
// Byte slice sizesize := data.SizeBytes([]byte{0x01, 0x02, 0x03})// 3
// Generic size (auto-detects type)size := data.Size("0x0102")// 2
size = data.Size([]byte{0x01, 0x02})// 2Notes
- For hex strings, the size is calculated as
(hexLength + 1) / 2to handle odd-length hex strings - Empty hex strings (
"0x") return0 - The hex string should have a
0xprefix