Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add null-character to string in Perl?

Tags:

perl

I want to create string in perl with length, for example 7, but "visible" contents, for example "a".

my $test = ...;

print $test result: "a" print length($test) result: 7

like image 251
Bdfy Avatar asked Dec 16 '22 19:12

Bdfy


1 Answers

You add null characters to the string. Why you want to this is beyond me, but how you do it is shown below.

{ow-loopkin:tmp:->perl
$string = "e\0\0\0\0";
print length $string;
[ctrl+d]
5
{ow-loopkin:tmp:->

You can also use pack() to pad it with nulls:

ow-loopkin:tmp:->perl
$string = pack("Z6", 42);
print length $string;
[ctrl+d]
6
{ow-loopkin:tmp:->
like image 143
Knut Haugen Avatar answered Jan 11 '23 07:01

Knut Haugen