Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: differing output of printf %#010x with int and string types #28627

Open
Sebi2020 opened this issue Nov 6, 2018 · 6 comments
Open

fmt: differing output of printf %#010x with int and string types #28627

Sebi2020 opened this issue Nov 6, 2018 · 6 comments
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@Sebi2020
Copy link

Sebi2020 commented Nov 6, 2018

What did you do?

I wanted print a padded (zeros) and prepended (0x) string character as a hex number.

Link to example on play.golang.org

What did you expect to see?

The same result for a string character and an integer.
The string printed as an hexadecimal number should be prepended with '0x'.

What did you see instead?

> A hex Number (a int) with padding:    0x0000000030
> A hex Number (a string) with padding: 0000000x30
> 
> Program exited.

System details

go version go1.11.2 linux/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/sebi/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/sebi/go/"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
GOROOT/bin/go version: go version go1.11.2 linux/amd64
GOROOT/bin/go tool compile -V: compile version go1.11.2
uname -sr: Linux 4.15.0-38-generic
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.5 LTS
Release:	16.04
Codename:	xenial
/lib/x86_64-linux-gnu/libc.so.6: GNU C Library (Ubuntu GLIBC 2.23-0ubuntu10) stable release version 2.23, by Roland McGrath et al.
gdb --version: GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
@martisch
Copy link
Contributor

martisch commented Nov 7, 2018

This seems to be consistent with the documentation (https://godoc.org/fmt):

  • int: # for 0x for hex (%#x) and 0 for pad with leading zeros rather than spaces (padding after the sign).
  • string: %x base 16, lower-case, two characters per byte then # for 0x for hex (%#x) for each byte and pad the string with leading zeros rather than spaces.

The difference is that the verb %x applies to each byte in strings and # is an alternate format for the verb so also per byte but the padding is not described to be per byte but applies to the output as a whole.

An improvement for the documentation that could be made is for '0':
instead of 'for numbers, this moves the padding after the sign' it could say 'for numbers, this moves the padding after the sign and leading 0x'

@martisch martisch changed the title Inconsistent behaviour of printf %#0(insert number)x with different types fmt: differing output of printf %#010x with int and string types Nov 7, 2018
@Sebi2020
Copy link
Author

Sebi2020 commented Nov 7, 2018

But it does not add an '0x' for each byte:
Example on Playground

> 00000x3031

As far as I understood you it should print something like:

> 000x300x31

@martisch
Copy link
Contributor

martisch commented Nov 7, 2018

you are right it i was confusing this with ' ' where it applies per byte and is mentioned explicitly "% x" in documentation.

Referring back to the original issue of 0 padding I think its still in line with documentation that padding of 0 applies to the whole string after it has been formatted? Could maybe made explicit.

To format the string like the number in the first example you could do:
fmt.Printf("0x%08x\n", "0");

If its not an obvious bug or contradicting documentation its likely not to change how fmt formats this as its established behavior by now that other programs will rely on.

@Sebi2020
Copy link
Author

Sebi2020 commented Nov 8, 2018

Referring back to the original issue of 0 padding I think its still in line with documentation that padding of 0 > applies to the whole string after it has been formatted? Could maybe made explicit.

I cannot find this in the documentation for printf.

This is what I've found:

 ' '	(space) leave a space for elided sign in numbers (% d);
	put spaces between bytes printing strings or slices in hex (% x, % X)
0	pad with leading zeros rather than spaces;
	for numbers, this moves the padding after the sign

It does only say what happens if you pad it with spaces, so I agree it should be more explicitly stated what happens if you use zeros instead of spaces.

@bcmills
Copy link
Contributor

bcmills commented Nov 19, 2018

CC @robpike

@bcmills bcmills added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 19, 2018
@bcmills bcmills added this to the Unplanned milestone Nov 19, 2018
@robpike
Copy link
Contributor

robpike commented Nov 19, 2018

Padding applies to the whole result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants