How to use $sprintf - with field width and not padding

HI,

I’m trying to use $sformatf(“0x%X”, my_number)

my_number is 8 bits number and i want sformatf to print this number is 2-digits width field and no leading “0”.

how can i make it ?

Thanks !

In reply to snognogt:

HI,
I’m trying to use $sformatf(“0x%X”, my_number)
my_number is 8 bits number and i want sformatf to print this number is 2-digits width field and no leading “0”.
how can i make it ?
Thanks !

Can you provide an example of what you want to display?
What you have is that if my_number = 8’h24. Your sformatf will return a string that is “0x24”.

In reply to snognogt:

You have to Modify your formatter like this

$sformatf("0x%0h", my_number)

%0h prints only the numbers really needed.

In reply to DVCoder:

Hi

For example - if my_number is 0x24 it prints ox24 (which is ok) but if my number is 0x8 - i want it to print 0x8 but keep the field width as 2 digits (which means that for example :

sprinft(“0x%X this is my number”, my_number)

i want to get for the 2 case :

0x24 this is my number
0x8(space) this is my number

as you can see i want to make the case in the second line that it will print 0x8 in 2 digits field width.

same question goes for the %d case…

In reply to snognogt:

You cannot suppress leading 0’s in the minimum field width needed to display in a radix other than decimal. What you are asking for is left justification with zero suppression. Some tools will let you do “%-2d”. But for other radixes, you need to strip the leading 0’s manually and add a trailing space for each that you find.