我是Perl的新手,在使用不同类型时遇到困难。
我正在尝试获取一个十六进制寄存器,将其转换为二进制,使用它的字符串并从二进制字符串中获取子字符串。
我已经做了一些搜索,而我尝试的是:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous"
>
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> -->
<style>
.nav-pills .nav-link.active{
color: rgb(12, 11, 11) !important;
background-color: rgb(233, 233, 233);
}
html {
position: relative;
min-height: 100%;
}
body {
/* Margin bottom by footer height */
margin-bottom: 60px;
}
.footer {
position: absolute;
bottom: 0;
width: 100%;
/* Set the fixed height of the footer here */
height: 60px;
line-height: 60px; /* Vertically center the text there */
background-color: #1d1c1c;
}
/* Custom page CSS
-------------------------------------------------- */
/* Not required for template or sticky footer method. */
body > .container {
padding: 60px 15px 0;
}
.footer > .container {
padding-right: 15px;
padding-left: 15px;
}
code {
font-size: 80%;
}
</style>
</head>
<body>
<header>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
</header>
<main role="main" class="container">
<div class="row">
<div class="col-sm-3 text-white bg-secondary pt-2 pb-2" >
<div class="nav flex-column nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
<a class="nav-link text-light active" id="v-pills-home-tab" data-toggle="pill" href="#v-pills-home" role="tab" aria-controls="v-pills-home" aria-selected="true">Profile</a>
<a class="nav-link text-light " id="v-pills-messages-tab" data-toggle="pill" href="#v-pills-messages" role="tab" aria-controls="v-pills-messages" aria-selected="false">Messages</a>
<a class="nav-link text-light " id="v-pills-settings-tab" data-toggle="pill" href="#v-pills-settings" role="tab" aria-controls="v-pills-settings" aria-selected="false">Settings</a>
</div>
</div>
<div class="col-sm-8 mt-3 mt-md-0 p-0 px-md-3">
<div class="tab-content" id="v-pills-tabContent">
<div class="tab-pane fade show active " id="v-pills-home" role="tabpanel" aria-labelledby="v-pills-home-tab">
<div class="card">
<div class="card-header card-title"><b>Profile</b></div>
<div class="card-body">Panel Content</div>
</div>
</div>
<div class="tab-pane fade" id="v-pills-messages" role="tabpanel" aria-labelledby="v-pills-messages-tab">
<div class="card">
<div class="card-header card-title"><b>Messages</b></div>
<div class="card-body">Panel Content</div>
</div>
</div>
<div class="tab-pane fade" id="v-pills-settings" role="tabpanel" aria-labelledby="v-pills-settings-tab">
<div class="card">
<div class="card-header card-title"><b>Settings</b></div>
<div class="card-body">Panel Content</div>
</div>
</div>
</div>
</div>
</div>
</main>
<footer class="footer">
<div class="container">
<span class="text-muted">Place sticky footer content here.</span>
</div>
</footer>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
结果是“ 64031”。第一个惊喜:我不能在Perl中打印十六进制值,而不仅仅是十进制值吗?
my $hex = 0xFA1F;
print "$hex\n";
结果是409649。第二个惊喜:我希望结果也是64031,因为“十六进制”将十六进制转换为十进制。
$hex = hex($hex);
print "$hex\n";
它打印二进制值。有没有一种方法可以在不打印十六进制的情况下将其转换为bin?
谢谢, SLP
答案 0 :(得分:4)
十进制,二进制和十六进制都是数字的文本表示(即书写数字的方式)。计算机无法将它们视为数字。
my $num = 0xFA1F;
将指定的数字(6.431万)存储到$num
中。它以硬件可以理解的格式存储,但这不是很重要。重要的是将其存储为数字而不是文本。
当要求print
打印一个数字时,它将以十进制(或足够大/小的科学计数法)打印出来。它不知道如何创建数字(从一个十六进制常量开始吗?还是从加法开始?等等),因此它无法确定如何基于该结果来输出数字。
要将数字打印为十六进制,可以使用
my $hex = 'FA1F'; # $hex contains the hex representation of the number.
print $hex; # Prints the hex representation of the number.
或
my $num = 0xFA1F; # $num contains the number.
printf "%X", $num; # Prints the hex representation of the number.
答案 1 :(得分:3)
您正在使用十六进制格式分配整数值。默认情况下,print
以十进制格式打印数字,因此您得到的是64031。
您可以使用printf()给出不同的格式来验证这一点。
$ perl -e ' my $num = 0xFA1F; printf("%d %X %b\n", ($num) x 3 ) '
64031 FA1F 1111101000011111
$ perl -e ' my $num = 64031; printf("%d %X %b\n", ($num) x 3 ) '
64031 FA1F 1111101000011111
$ perl -e ' my $num = 0b1111101000011111; printf("%d %X %b\n", ($num) x 3 ) '
64031 FA1F 1111101000011111
$
要获取字符串0xFA1F的二进制格式,可以使用sprintf()
$ perl -e ' my $hex = 0xFA1F; my $bin=sprintf("%b",$hex) ; print "$bin\n" '
1111101000011111
$
答案 2 :(得分:2)
让顺序混乱的每一点
my $hex = 0xFA1F;
这会在$hex
中存储一个十六进制常量,但是Perl没有十六进制数据类型,因此尽管您可以编写十六进制常量以及二进制和八进制常量,但Perl会将它们全部转换为十进制。请注意
my $hex = 0xFA1F;
和
my $hex = '0xFA1F';
第一个将数字存储在$ hex中,当您打印出该数字时会得到一个十进制数字,第二个存储一个字符串,该字符串在打印时将给出0xFAF1
但可以传递给{{1} }函数将转换为十进制。
hex()
hex函数将 string 转换为十六进制数字,并返回十进制值,到目前为止,$hex = hex($hex);
仅用作Perl数字首先将$hex
进行字符串化,然后将字符串传递给$hex
函数,以将该值从十六进制转换为十进制。
所以解决。 hex()
几乎就在那儿,有一个名为printf()
的函数,该函数具有与sprintf()
相同的参数,但不是打印格式化的值,而是将其返回为字符串。所以您需要的是
printf()
技术说明: 是的,我知道Perl在内部将其所有数字都存储为二进制,但是不要回答这个问题,好吗?
答案 3 :(得分:2)
如果您可以使用发行版,那么我写了Bit::Manip来简化原型处理寄存器的工作(如果您在编译XS代码时遇到问题,也可以使用Pure Perl版) )。
它不仅可以从数字中提取位,还可以切换,清除,设置等:
use warnings;
use strict;
use Bit::Manip qw(:all);
my $register = 0xFA1F;
# fetch the bits from register using msb, lsb
my $msbyte = bit_get($register, 15, 8);
print "value: $msbyte\n";
print "bin: " . bit_bin($msbyte) . "\n";
# or simply:
# printf "bin: %b\n", $msbyte;
输出:
value: 250
bin: 11111010
这是我写的blog post,它显示了如何通过示例数据表寄存器使用软件的某些功能。