我想在perl中創建帶有長度的字符串,例如7,但是「可見」內容,例如「a」。如何在Perl中添加空字符到字符串?
my $ test = ...;
打印$測試結果: 「一」 打印長度($試驗)結果:7
我想在perl中創建帶有長度的字符串,例如7,但是「可見」內容,例如「a」。如何在Perl中添加空字符到字符串?
my $ test = ...;
打印$測試結果: 「一」 打印長度($試驗)結果:7
參見pack
功能,也許。
您將空字符添加到字符串。爲什麼你想要這個超越了我,但你如何做到這一點如下所示。
{ow-loopkin:tmp:->perl
$string = "e\0\0\0\0";
print length $string;
[ctrl+d]
5
{ow-loopkin:tmp:->
您還可以使用包()來填充它與空值:
ow-loopkin:tmp:->perl
$string = pack("Z6", 42);
print length $string;
[ctrl+d]
6
{ow-loopkin:tmp:->
的利用方法:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
require 'syscall.ph';
my $path = '/root/target';
my $buf = "\0"x512; # $buf (pointer) must be large enough to receive data
# See statf(2) on Linux
syscall(&SYS_statfs, $path, $buf) == 0 or die($!);
my @st = unpack 'L7', $buf;
print Dumper(\@st);
1;
可這有什麼可能的用途? – Ether 2010-12-03 15:55:27
@字符串的其他空填充可用於加密應用程序。 – RKeast 2014-07-30 16:45:49