package Foo::Bar; # use utf8; use strict; use warnings; use Exporter qw(import); use Encode qw(decode encode); use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = (); @EXPORT_OK = qw(ascii2hexEncode hex2ascciiDecode hexOutput); # binmode( STDOUT, ':utf8' ); sub _ascii2hex { return unpack("H*", $_[0]); } sub _hex2ascii { return pack("H*", $_[0]); } sub hexOutput { my ( $flag , $data ) = @_; my $octet = ascii2hexEncode( $flag , $data ); # trim leading and trailing white space # split string every two characters # join the splitted characters with white space $octet = join(' ', split(/(..)/, $octet)) =~ s/^\s+|\s+$//r =~ y/ / /rs; # insert new line character every 30 characters # join("\n", unpack('(A30)*', $octet)); push my @aref, , unpack('(A30)*', $octet); return \@aref; } sub ascii2hexEncode { my ( $flag , $data ) = @_; my $octet = encode( $flag , $data ); return _ascii2hex( $octet ); } sub hex2ascciiDecode { my ( $flag , $data ) = @_; my $hex2ascciiOctet = _hex2ascii( $data ); return decode( $flag , $hex2ascciiOctet ); } 1;