# how do I print hex inside stings?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi All,

I am trying to print a number in hex format inside a string.
Using

http://forums.devshed.com/perl-programming-6/print-perl-integer-hex-file-809824.html

<code>
#!/usr/bin/perl -w
use strict;

my(\$a, \$b);

\$a = 25;
\$b = ~(1 - ( 32 - \$a) -1);

printf("The result of 32 - \$a >> is: '%#x'\$b in Hex\n");
</code>

The result gives
The result of 32 - 25 >> is: '0'6 in Hex

What am I doing wrong?

Many thanks,
-T

## Re: how do I print hex inside stings?

On 7/10/2015 8:28 PM, T wrote:

should this not be:

printf('The result of 32 - \$a >> is: %x in hex', \$b);

which gives

The result of 32 - \$a >> is: 6 in hex

## Re: how do I print hex inside stings?

On 07/10/2015 06:59 PM, Nasser M. Abbasi wrote:

<code>
#!/usr/bin/perl -w
use strict;

my(\$a, \$b);

\$a = 25;
\$b = \$a >> 4;

# printf("The result of 32 - \$a >> is: '%#x'\$b in Hex\n");
printf('The result of \$a >> 4 is: %x in hex', \$b);
</code>

gives:
The result of \$a >> 4 is: 1 in hex

Maybe they should be double quotes?

## Re: how do I print hex inside stings?

On 07/10/2015 07:29 PM, T wrote:

Double quotes works better, but the answer is wrong.

## Re: how do I print hex inside stings?

On 7/10/2015 19:35, T wrote:

my \$m = 25;        # 11001
my \$n = \$m >> 4;
printf "The result of '0x%X' >> 4 is: '0x%X'\n", \$m, \$n;
__END__

The result of '0x19' >> 4 is: '0x1'

## Re: how do I print hex inside stings?

On 07/10/2015 07:57 PM, \$Bill wrote:

Hi \$Bill,

Ding!  I get it.  You include the formatting in line, then
which variables goes with it in order at the end.

Thank you for helping me with this.

-T

The following worked:
<code>
#!/usr/bin/perl -w
use strict;

my(\$a, \$b, \$c);

\$a = 256;
\$b = 3;
\$c = \$a >> \$b;

printf("The result of '0x%X' >> '0x%X' is '0x%X'in hex\n", \$a, \$b, \$c);
</code>

Result:
\$ perl HexWriteTest.pl
The result of '0x100' >> '0x3' is '0x20'in hex

## Re: how do I print hex inside stings?

On 7/10/2015 20:06, T wrote:

printf "The result of '0x%X' >> %u is '0x%X' in hex\n", \$a, \$b, \$c;

# %u = unsigned - check sprintf in perlfunc pod for sprintf/printf codes

Try to avoid using \$a and \$b which are used in Perl sorts to
compare items in lists (you can use them, but if you forget and
have some sort logic you could get in trouble if not careful).

Convert and read the first 10 pods in the Perl docs in their
entirety which will give you a good start to Perl.

## Re: how do I print hex inside stings?

On 07/11/2015 07:04 AM, \$Bill wrote:

I switched to %s unless I just want a base 10 number.  Thank you!

Yes or course.  When I make it into real, not test code, I assign
names that are apropos.  And I will be careful.  Thank you!

## Re: how do I print hex inside stings?

On 7/13/2015 16:55, T wrote:

%s = string
%d = decimal
%u = unsigned decimal (which is what 3 is) or %d if your shift can go negative

perlfunc pod is a must read.

## Re: how do I print hex inside stings?

On 07/13/2015 05:31 PM, \$Bill wrote:

I have two different reference now documented in my perl
keepers file.  Thank you!

## Re: how do I print hex inside stings?

\$ perlfunc pod

Found this:
http://perldoc.perl.org/perlpod.html

## Re: how do I print hex inside stings?

'pod' means 'plain old documentation', a Perl markup language for
writing documentation. Conseqequently, the 'perlfunc pod' is a file:

[rw@doppelsaurus]/tmp#locate perlfunc
/usr/local/src/perl-5.14.2/pod/perlfunc.pod
/usr/share/man/man1/perlfunc.1.gz
/usr/share/perl/5.14.2/pod/perlfunc.pod

The portable way to get that formatted is the command

perldoc perlfunc

'perldoc perl' should display an overview of all available
documentation.

## Re: how do I print hex inside stings?

On 7/13/2015 20:13, T wrote:

I posted this before, but my idea of a manual is to combine the
pods and read them in gvim after converting to text.

Save the below as perlman.pl (or whatever).
Then run like 'perl perlman.pl >perl.man'
And you will have your manual to read and search called perl.man .

To find a section like perlfunc, look for perlfunc in column 1.
(in gvim use /^perlfunc).  To find a specific function, search
in column 5 or one indent in (eg: /^    sprintf) may not be first
match.

#!perl -sw --

use strict;
use Config;
use Pod::Text;
use Data::Dumper; \$Data::Dumper::Indent=1; \$Data::Dumper::Sortkeys=1;

# make perl manual from pods into single text file
# Usage: perl perlman.pl >perl.man
#
# copyright 2001 \$Bill - you may use/modify freely for personal use
#
# Ignore any errors you may get from pod2text conversion module

use vars qw(\$h \$help \$s \$d);

my \$pod_dir = "\$Config/pod";    # directory where core pod files are
\$pod_dir = "\$Config/pods" if \$] > 5.008;

my @extra_pods = ();        # other pod file dirs to check - add your own
# push @extra_pods, "\$Config/pod" if \$] <= 5.008;
# push @extra_pods, "\$Config/pods" if \$] > 5.008;

print <<EOD and exit 0 if \$h or \$help;

Combines all of the Perl man pages into a single file for searching
and reference.  Should generate a lot of format errors.

Usage: \$0 [-s] [<pod-dir>] [>perl.man]
-s        silent (no progress lines)
pod-dir        optional directory in which to find pod files

EOD

\$| = 1;

# core sections and ones to skip
my @sections = ();    # built automatically in main if empty (section order)
my %sections = ();    # built automatically in main if empty (section hdrs)
my %do_not_want = ();    # Ones I don't want in my manual

# get core sections (not language or platform specific)
get_sections () if not @sections;

# section - see perl.pod source for choices - win32 is done here allready
if (\$^O =~ /Win32/i) {
push @sections, 'perlwin32';
\$sections = 'Perl notes for Windows';
}

my %pods;
opendir DIR, \$pod_dir or die "Error on opendir \$pod_dir: \$!\n";
while (defined (\$_ = readdir DIR)) {
next if !/\.pod\$/;
s/\.pod\$//i;
\$pods = 1;
}
closedir DIR;

# get the core sections in order
foreach my \$section (@sections) {

my \$sect_hdr = \$sections;

# skip sections I don't want
if (exists \$do_not_want) {
print STDERR "Skipping \$section Section\n" if !\$s;
next;
}
print STDERR "Doing \$section '\$sect_hdr'\n" if !\$s;

# skip if couldn't find pod for section
if (not exists \$pods) {
print STDERR "\$section section missing\n";
next;
}

printf "\n%-20.20s  %s\n\n", \$section, \$sect_hdr;
pod2text ("-80", "\$pod_dir/\$section.pod");
delete \$pods;
}

}

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub get_sections {

open IN, "\$pod_dir/perl.pod" or die "open \$pod_dir/perl.pod: \$! (\$^E)";
my \$lines;
{ undef \$/; \$lines = <IN>; }
close IN;

# strip front and back of perl.pod
print (Data::Dumper->Dump([\$lines], [qw(\$lines)])) if \$d;

# grab sections from text from Overview down to before Language-Specific
foreach (split /\n/, \$lines) {
next if /^\s*\$/;