- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是一个家庭作业,在终端调用命令时,我必须将十六进制转换为二进制。我的“老师”不是真正的“老师”,所以我完全迷路了。我必须包含一个过程void printBits(unsigned long I),它在I中打印位。它将使用'-p'开关从命令行调用,后跟一个32位的十六进制无符号长整数。示例:$lab3-p 0x5
产量:0000 0000 0000 0000 0000 0000 0101
请不要只给我密码。我要明白这一点。
void printBits(unsigned long number) {
// Declare Array to hold our binary number
char binaryNumber[32];
// For Loop Index
int i;
for (i = 31; i >= 0; --i)
{
binaryNumber[i] = (number & 1);
number >>= 1;
}
最佳答案
有很多方法可以打印任何数字的二进制表示。首先,您可以直接输出移位和索引操作的结果(到stdout
,一个文件,等等…)。这似乎是您开始使用的方法,但随后您声明了一个32位缓冲区。虽然您当然可以这样做,但如果不返回指向已完成缓冲区的指针,则不需要缓冲结果。(这就引出了我下面的第三点)
简单地输出位而不存储/返回指向以nul结尾的字符串中的位的指针,有它的位置,但通常用途有限。然而,这是一个共同的问题,包含了所有方法的基础。创建未添加的二进制表示法的方法如下:
/** unpadded binary representation of 'v'. */
void binprn (const unsigned long v)
{
if (!v) { putchar ('0'); return; }; /* if v = 0 output '0' */
size_t sz = sizeof v * CHAR_BIT; /* get the number of bits in v */
unsigned long rem = 0; /* variable to hold shifted result */
while (sz--) /* for each bit (in decreasing order) */
if ((rem = v >> sz)) /* if bits exist in the shifted value */
putchar ((rem & 1) ? '1' : '0'); /* output '1' or '0' */
}
rem
中找到一个位,在循环迭代的其余部分中总会有位要打印,因为您正在以递减的量移动。从最有意义的位开始(首先打印),你的位以正确的顺序打印出来,并且只打印组成这个数字的位数。
1
”,使事情变得一团糟。)
0
位,但希望每次都有固定位数的表示,那么这非常有用。在这里,您只需传递希望查看的位数。然后,您的函数将循环遍历您的数字中的位数位置,并输出结果:
/** binary representation of 'v' padded to 'sz' bits.
* the padding amount is limited to the number of
* bits in 'v'. valid range: 0 - sizeof v * CHAR_BIT.
*/
void binprnpad (const unsigned long v, size_t sz)
{
if (!sz) putchar ((v & 1) ? '1' : '0'); /* if no sz, '0' is fine */
if (sz > sizeof v * CHAR_BIT) /* if sz exceed # of bits, limit */
sz = sizeof v * CHAR_BIT;
while (sz--) /* for sz positions in decreasing order, '1' or '0' */
putchar ((v >> sz & 1) ? '1' : '0');
}
8, 16, 32, ...
控制位的数量。(如果通过
sz
大小,由您决定如何操作,我只选择输出
0
)
'0'
等更有用。现在,您要么必须将大小适当的数组作为参数传递,声明
printf
数组,以便在函数返回时不会破坏数组,或者为函数内的数组动态分配存储空间。所有这些都有优缺点,根据代码的需要,您必须权衡它们。例如。:
/** returns pointer to binary representation of 'v' zero padded to 'sz'.
* returns pointer to string contianing binary representation of
* unsigned 64-bit (or less ) value zero padded to 'sz' digits.
*/
char *binpad (const unsigned long v, const size_t sz)
{
static char s[BITS_PER_LONG + 1] = {0};
char *p = s + BITS_PER_LONG;
register size_t i;
for (i = 0; i < sz; i++)
*--p = (v>>i & 1) ? '1' : '0';
return p;
}
static
如何返回缓冲区中
p
位数开始的起始位置。还要注意,您需要一个常量来表示硬件上a
sz
中的位数。(通常以类似于
BITS_PER_LONG
的方式处理)
long
声明的一个限制是转换函数在任何一个
BUILD_64
调用中(或在任何一行代码中)只能使用一次,因为只有一个存储数组用于二进制转换。(在进行
static
调用之前,您始终可以进行任意数量的调用,并将结果存储在不同的位置)
printf
和
printf
序列时。例如:
hexval : 0xdeadbeef => 11011110-10101101-10111110-11101111
0
基本相同,但添加了静态缓冲区以容纳分隔符,并额外检查位位置以确定何时应向缓冲区添加分隔符:
/** returns pointer to formatted binary representation of 'v' zero padded to 'sz'.
* returns pointer to string contianing formatted binary representation of
* unsigned 64-bit (or less ) value zero padded to 'sz' digits with char
* 'sep' placed every 'szs' digits. (e.g. 10001010 -> 1000-1010).
*/
char *binfmt (const unsigned long v, const unsigned char sz,
const unsigned char szs, const char sep)
{
static char s[BITS_PER_LONG * 2 + 1] = {0};
char *p = s + 2 * BITS_PER_LONG;
register size_t i;
*p = 0;
for (i = 0; i < sz; i++) {
p--;
if (i > 0 && szs > 0 && i % szs == 0)
*p-- = sep;
*p = (v >> i & 1) ? '1' : '0';
}
return p;
}
1
处理参数,也可以使用循环处理少量简单选项。在Linux上,您的代码应该响应的唯一必需参数是
binpad
以获取帮助,而
getops
以获取版本。虽然没有人这样做的简短例子等,它至少是好的,有这些信息。请看下面的示例,它将所有部分组合在一起,如果您有任何问题,请告诉我:
#include <stdio.h>
#include <stdlib.h> /* for strtoul */
#include <errno.h> /* for errno */
#include <limits.h> /* for UINT_MAX, ULONG_MAX, CHAR_BIT */
#define PACKAGE "hex2bin"
#define VERSION "0.01"
/* BUILD_64 - Check x86/x86_64 */
#if defined(__LP64__) || defined(_LP64)
# define BUILD_64 1
#endif
/* BITS_PER_LONG */
#ifdef BUILD_64
# define BITS_PER_LONG 64
#else
# define BITS_PER_LONG 32
#endif
unsigned long processopts (int argc, char **argv);
unsigned long xstrtoul (char *s);
void binprn (const unsigned long v);
void binprnpad (const unsigned long v, size_t sz);
char *binpad (const unsigned long v, const size_t sz);
char *binfmt (const unsigned long v, const unsigned char sz,
const unsigned char szs, const char sep);
void help (int xcode);
int main (int argc, char **argv) {
unsigned long hexval = processopts (argc, argv);
/* print unpadded binary */
printf ("\n hexval : 0x%lx (%lu) => ", hexval, hexval);
binprn (hexval);
printf ("\n");
/* print padded to 32-bits */
printf ("\n hexval : 0x%lx (%lu) => ", hexval, hexval);
binprnpad (hexval, sizeof (int) * CHAR_BIT);
printf ("\n");
/* padded binary returned as formatted string
* with '-' separators every 8 bits
*/
printf ("\n hexval : 0x%lx (%lu) => %s\n\n", hexval, hexval,
binfmt (hexval, sizeof (int) * CHAR_BIT, CHAR_BIT, '-'));
return 0;
}
/* quick custom argument handler */
unsigned long processopts (int argc, char **argv)
{
size_t i = 1;
unsigned long val = 0;
if (argc < 2) help (0); /* insufficient arguments */
for (; argv[i]; i++) { /* for each argument */
if (*argv[i] == '-') { /* for each beginning with '-' */
switch (argv[i][1]) {
case 'h': /* respond to '-h' help */
help (0);
case 'p': /* handle '-p' convert value */
if (!argv[i+1]) { /* if '-p' w/o next arg */
fprintf (stderr, "error: insufficient input.\n");
help (1);
}
if (*argv[i+1] != '0' || /* validate hex input */
(argv[i+1][1] != 'x' && argv[i+1][1] != 'X')) {
fprintf (stderr, "error: invalid 'hex_value' input.\n");
help (1);
}
val = xstrtoul (argv[i+1]); /* convert to ulong */
if (val > UINT_MAX) { /* validate 32-bits */
fprintf (stderr, "error: input value exceeds 32-bits.\n");
help (1);
}
break;
case 'v': /* respond to '-v' version */
printf ("%s, version %s\n", PACKAGE, VERSION);
exit (0);
default :
fprintf (stderr, "error: invalid/unrecognized option '%s'.\n",
argv[i]);
help (1);
}
}
}
return val; /* return val */
}
unsigned long xstrtoul (char *s)
{
unsigned long v = 0;
errno = 0;
/* test for hex or decimal conversion */
if (*s == '0' && (s[1] == 'x' || s[1] == 'X'))
v = strtoul (s, NULL, 16);
else
v = strtoul (s, NULL, 10);
/* check for various possible errors */
if ((errno == ERANGE && v == ULONG_MAX) || (errno != 0 && v == 0)) {
perror ("strtoul");
exit (EXIT_FAILURE);
}
return v;
}
/** unpadded binary representation of 'v'. */
void binprn (const unsigned long v)
{
if (!v) { putchar ('0'); return; };
size_t sz = sizeof v * CHAR_BIT;
unsigned long rem = 0;
while (sz--)
if ((rem = v >> sz))
putchar ((rem & 1) ? '1' : '0');
}
/** binary representation of 'v' padded to 'sz' bits.
* the padding amount is limited to the number of
* bits in 'v'. valid range: 0 - sizeof v * CHAR_BIT.
*/
void binprnpad (const unsigned long v, size_t sz)
{
if (!sz) putchar ((v & 1) ? '1' : '0');
if (sz > sizeof v * CHAR_BIT)
sz = sizeof v * CHAR_BIT;
while (sz--)
putchar ((v >> sz & 1) ? '1' : '0');
}
/** returns pointer to binary representation of 'v' zero padded to 'sz'.
* returns pointer to string contianing binary representation of
* unsigned 64-bit (or less ) value zero padded to 'sz' digits.
*/
char *binpad (const unsigned long v, const size_t sz)
{
static char s[BITS_PER_LONG + 1] = {0};
char *p = s + BITS_PER_LONG;
register size_t i;
for (i = 0; i < sz; i++)
*--p = (v>>i & 1) ? '1' : '0';
return p;
}
/** returns pointer to formatted binary representation of 'v' zero padded to 'sz'.
* returns pointer to string contianing formatted binary representation of
* unsigned 64-bit (or less ) value zero padded to 'sz' digits with char
* 'sep' placed every 'szs' digits. (e.g. 10001010 -> 1000-1010).
*/
char *binfmt (const unsigned long v, const unsigned char sz,
const unsigned char szs, const char sep)
{
static char s[BITS_PER_LONG * 2 + 1] = {0};
char *p = s + 2 * BITS_PER_LONG;
register size_t i;
*p = 0;
for (i = 0; i < sz; i++) {
p--;
if (i > 0 && szs > 0 && i % szs == 0)
*p-- = sep;
*p = (v >> i & 1) ? '1' : '0';
}
return p;
}
void help (int xcode)
{
xcode = xcode ? xcode : 0; /* set default exit code */
printf ("\n %s, version %s\n\n"
" usage: %s -p hex_value (32-bit)\n\n"
" converts 'hex_value' to its binary representation.\n\n"
" Options:\n\n"
" -h this help.\n"
" -p hex_value display binary representation of 'hex_value'.\n"
" -v display version information.\n\n",
PACKAGE, VERSION, PACKAGE);
exit (xcode);
}
$ ./bin/hex2bin -p 0xe7
hexval : 0xe7 (231) => 11100111
hexval : 0xe7 (231) => 00000000000000000000000011100111
hexval : 0xe7 (231) => 00000000-00000000-00000000-11100111
$ ./bin/hex2bin -p 0xdeadbeef
hexval : 0xdeadbeef (3735928559) => 11011110101011011011111011101111
hexval : 0xdeadbeef (3735928559) => 11011110101011011011111011101111
hexval : 0xdeadbeef (3735928559) => 11011110-10101101-10111110-11101111
$ ./bin/hex2bin -h
hex2bin, version 0.01
usage: hex2bin -p hex_value (32-bit)
converts 'hex_value' to its binary representation.
Options:
-h this help.
-p hex_value display binary representation of 'hex_value'.
-v display version information.
$ ./bin/hex2bin -v
hex2bin, version 0.01
关于c - 从命令行参数C将十六进制转换为二进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35500171/
我有一个消息 static int[] message = { 0x01, 0x10, 0x00, 0x01, // port addres 01 - 08
如何将十进制转换为以下格式的十六进制(至少两位,零填充,不带 0x 前缀)? 输入:255 输出:ff 输入:2 输出:02 我尝试了 hex(int)[2:] 但它似乎显示了第一个示例而不是第二个示
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: Large numbers in Pascal (Delphi) 我正在尝试将 66 位值转换为十进制。 我注意到d
给定一个十进制数字列表,如何将每个数字转换为其等效的十六进制值,反之亦然? 例如: (convert2hex 255 64 64); ->(FF 40 40) (convert2dec FF 40 4
var color = Math.floor(Math.random() * 16777215).toString(16); var hex = Number.parseInt(col
我一直被教导 0-9 代表 0 到 9 的值,A、B、C、D、E、F 代表 10-15。 我看到这种格式 0x00000000,它不适合十六进制模式。有没有导游或导师可以解释一下? 我在谷歌上搜索了十
我目前正尝试像十六进制编辑器一样将文件读取为十六进制值。为了解释这个问题,让我们假设我有一个test.txt,里面有一个简单的“Hello world”。我正在尝试使用接近以下代码的程序以十六进制形式
我正在尝试获取元素背景颜色 $(document).ready(function(){ $.each('.log-widget',function(){ console.log($(t
0x40130020的十六进制值是 2.296883 的浮点值, 使用本网站 http://gregstoll.dyndns.org/~gregstoll/floattohex/ .这如何实现到 Lu
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,vis
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
谁能解释一下我们如何计算十六进制浮点常量的值。我在看书,发现0x0.3p10代表值192。 最佳答案 指数仍以十进制表示,但底数为二,尾数为十六进制。 所以 0.3P10 是 (3 × 16−1) ×
我正在尝试创建一个标签云,需要帮助来创建一个函数来计算应用于每个标签链接所需的颜色。 我有 3 个变量: 单个标签重要性(从 0.1 到 1) 最大(最重要)的标签颜色(十六进制代码,例如“fff00
大家好,我想发送尽可能短的字符串/值。如果我有以下内容 1)l23k43i221j44h55uui6n433bb4 2)124987359824369785493584379 3)kla^askdja
我知道你会写... background-color: #ff0000; ...如果你想要红色的东西。 你可以写... background-color: rgba(255, 0, 0, 0.5);
我有一些传递地理位置坐标的二进制数据流 - 纬度和经度。我需要找到它们编码的方法。 4adac812 = 74°26.2851' = 74.438085 2b6059f9 = 43°0.2763'
我想从 my_table 中选择 family,其中 family LIKE '%HEX(9D)' 家庭十六进制格式以 9D 十六进制结尾 我将excel文件转换为sqlite数据库但是 我的一些数据
我有一组二进制配置文件,每个文件有三个版本——每个文件的原始版本和两个不同修改的版本。我需要能够同时看到两个版本和原始版本之间的差异。 我需要的是一个二进制文件的三向差异工具。通过相当费力的谷歌搜索,
我正在尝试将(可变长度)十六进制字符串转换为带符号整数(我需要正值或负值)。 [Int16] [int 32]和[int64] 似乎可以在2,4+字节长的十六进制字符串上正常工作,但我在使用3个字节的
如何将十六进制的 unicode 写入 Facebook“您在想什么”框? 我尝试过写: \u00B9 "\u00B9" ¹ "¹" 到目前为止没有任何效果 (让我补充一下,我是在 M
我是一名优秀的程序员,十分优秀!