Admittedly, C and Python work the same way, which is different to Go.

#include <stdio.h>

int main(void) {
    printf("%07x\n", 42);
    printf("%0#7x\n", 42);
    printf("0x%07x\n", 42);
    return 0;
}

// Output
000002a
0x0002a
0x000002a

On Wednesday 31 January 2024 at 13:56:07 UTC Brian Candler wrote:

> https://pkg.go.dev/fmt#hdr-Printing
>
> The '#' means you want the alternate format with the 0x prepended, and the 
> '7' means you want the number itself padded to 7 digits 
>
> x := fmt.Sprintf("%07x", 42)   // 000002a
> y := fmt.Sprintf("%0#7x", 42)   // 0x000002a
> z := fmt.Sprintf("0x%07x", 42)   // 0x000002a
>
> Seems pretty logical to me. If Python chooses to do it a different way, 
> that's because Python is a different language.
>
> On Wednesday 31 January 2024 at 13:23:01 UTC Lukas Toral wrote:
>
>> Hello,
>>
>> I am working on code that formats strings. I have an issue with 
>> formatting the alternate form with zero padding of signed hexadecimals.
>>
>> I have a format string like this: "%#07x", I would expect the zero 
>> padding to make sure the total width is 7. However, when I format the 
>> string using fmt.Sprintf I get the following results: 0x000002a which has 
>> the length of 9 characters instead of the expected result: 0x0002a with the 
>> width of 7 characters.
>>
>> Example code: x := fmt.Sprintf("%#07x", 42) 
>> x will be equal to: 0x000002a
>>
>> Example of python code that works as I would expect: result = 
>> "{:#07x}".format(42)
>> results will be equal to: 0x0002a
>>
>> I am suspicious that this might be a bug where the 0x is not accounted in 
>> the width but maybe I am doing something wrong.
>> Thanks for any help :)
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ff60170d-47d8-4ce2-be88-48d398740a18n%40googlegroups.com.

Reply via email to