simple sprintF output question

51 views
Skip to first unread message

David Renne

unread,
Feb 21, 2018, 6:06:00 AM2/21/18
to golang-nuts
https://play.golang.org/p/XZimA47p9qM

Why isnt this formatted with two bytes below 10 and is merely 0-9 instead of 00, 01, 02 etc etc with a left padded zero?   

The documentation of %x for sprintf made me think that this would keep a left padded zero on my string for 0-9, so I merely abstracted my sprintf to check if it's lower than 10 and then pad it to make it look like a proper mac address.  I would love to come up with some better verbiage for the description in the documentation for 0-9 so people arent surprised and need to left pad it like I had to for a mac address

%x	base 16, lower-case, two characters per byte


Jakob Borg

unread,
Feb 21, 2018, 6:28:01 AM2/21/18
to David Renne, golang-nuts
You’re quoting the help for %x for strings and byte slices. For integers it simply says "base 16, with lower-case letters for a-f” and thus behaves like any other number base usually does. In this case, you want “%02x”.

//jb

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages