printing format for hexadecimal signed number

Hello Experts,,

   I m facing a problem as follows..  need help..

When I am giving the below command it gives me three digit hexadecimal number , for ex :-

printf("%0.3x", 10);
Output: 00a

But I want the same for signed number also.

Now when I am specifiying the same format for singed number like

printf("%0.3x", -1 );

It gives me the output ffffffff

But I want only last 3 bytes ,ie fff

Thanks in Advance .......
Prady..

Your conversion modifier is the problem. The 'x' conversion modifier takes an integer argument and converts it to unsigned lowercase hexadecimal notation.

Thank's for your kind reply ..

But I think its converting to a signed Hexadecimal number not to "unsigned lowercase hexadecimal " my friend..

as -1 in singed hexadecimal number is equal to ffffffff
and like wise if I will give printf("%x", -10);
which will give me a output fffffff6 . As -10 in signed hexadecimal number is equal to fffffff6 .

I think its depend on machine whether it is a 32 bit or 64 bit machine ,
But my problem is I want to take only the last 3 digits of the hexadecimal number ..
Ex: printf("%x", -10); sholud give me output ff6 not fffffff6 .

I am curious to know is there any format specifier in nawk or gawk in which I can get only the last three digits.

There is no format in awk that permits to get only the last three last digit.
But you can write your own function, for example :

function toHexa(number, len      ,str) {
   if (len == 0) len = 2;
   str = sprintf("%0" len "." len "x", number);
   return substr(str, length(str)-len+1) "";
}
{
   print $1, "=>", toHexa($1,3);
}

Input:

0
1
27
3210
-1
-30

Output:

0 => 000
1 => 001
27 => 01b
3210 => c8a
-1 => fff
-30 => fe2

Jean-Pierre.

Thanks Genious Jean .. Where do you get these ideas..

May I know the reason why did you use the below line
str = sprintf("%0" len "." len "x", number);
instead of just str = sprint("%x", number )

Thanks in Advance..

Or directly:

x=sprintf("%x",-10);print substr(x,length(x)-2)

For positive numbers lower than 256, the result is not 3 digits.

Jean-Pierre.

So what?
You'll get <3 chars output.

That was the initial requirement.

Jean-Pierre.

OK.

x=sprintf("%.3x",1);print substr(x,length(x)-2)