## Dec to Hex

Use function sprintf(“%x”, DECNUMBER) or sprintf(“%X”, DECNUMBER) for hexadecimal numbers with capital letters.

``````\$decvalue = 200;
\$hexvalue = sprintf("%x", \$decvalue);  ## gives c8
\$hexvalue = sprintf("%X", \$decvalue);  ## gives C8``````

## Dec to Oct

Use sprintf(“%o”, DECNUMBER )

``````\$decvalue = 200;
\$octval = sprintf( "%o", \$decvalue );``````

The program generates
317
If you want a leading zero for the octal number use **sprintf(“%#o”, DECNUMBER);

``````\$decvalue = 200;
\$octval = sprintf( "%#o", \$decvalue );``````

The program now generates
0317

## Dec to Bin

Use sprintf( “%b”, DECNUMBER)

``````\$decvalue = 200;
\$binvalue = sprintf( "%b", \$decvalue  );``````

## Hex to Dec

Use function hex( HEXNUMBER )
All variants below gives the same result. All formats of hexadecimal numbers are accepted.

``````\$hexvalue = 'FC';
\$decvalue =  hex(\$hexvalue);
\$hexvalue = 'fc';
\$decvalue =  hex(\$hexvalue);
\$hexvalue = '0xfc';
\$decvalue =  hex(\$hexvalue);``````

## Hex to Oct

Use a combination of functions sprintf(“%o”, hex( HEXNUMBER )) or sprintf(“%#o”, hex( HEXNUMBER )) for a leading zero.

``````\$hexvalue = "fcff";
\$octvalue = sprintf( "%o", hex( \$hexvalue ) );``````

## Hex to Bin

Use a combination of functions sprintf(“%b”, hex( HEXNUMBER ))

``````\$hexvalue = "fcff";
\$binvalue = sprintf( "%b", hex( \$hexvalue ) );``````

## Oct to Hex

Use a combination of functions sprintf(“%x”, oct( OCTNUMBER )) or sprintf(“%X”, oct( OCTNUMBER ))
The difference is if you want hex numbers “a-f” or “A-F”.

``````\$octvalue = "0176377";
\$hexvalue = sprintf( "%x", oct( \$octvalue ) );``````

## Oct to Dec

Use function oct( OCTNUMBER )

``````\$octvalue = "0176377";
\$decvalue = oct( \$octvalue );``````

## Oct to Bin

Use a combination of functions sprintf(“%b”, oct( OCTNUMBER ))

``````\$octvalue = "0176377";
\$binvalue = sprintf( "%b", oct( \$octvalue ) );``````

## Bin to Hex

Use a combination of functions remember to put in 0b before the binary number.
sprintf(“%x”, oct( “0bOCTNUMBER” )) or sprintf(“%X”, oct( “0bOCTNUMBER” ))

``````\$binvalue = "11001111";
\$hexvalue = sprintf("%x", oct( "0b\$binvalue" ) );
\$hexvalue = sprintf("%X", oct( "0b\$binvalue" ) );``````

## Bin to Dec

Use function oct and insert 0b before the binary number.

``````\$binvalue = "11001111";
\$decvalue = oct( "0b\$binvalue" );``````

## Bin to Oct

Use a combination of sprintf and oct
Remember that the function oct converts from bin to dec in this case.

``````\$binvalue = "11001111";
\$octvalue = sprintf("%o", oct( "0b\$binvalue" ) );
\$octvalue = sprintf("%#o", oct( "0b\$binvalue" ) );  ##Generates octal number with leading zero``````