从输入中找到连续十六进制数字的最大数量
Find the maximum number of consecutive hex digits from input
我在编写一个程序时遇到问题,该程序以十六进制读取 64 位无符号长长,并找到最常见的十六进制数字并返回它出现的次数以及数字本身。例输入:0xABCD_FFFF_43FF_42CF输出:十六进制数字 F 出现 4 次
到目前为止,这是我的代码:
#include <stdio.h>
int main()
{
int i = 0;
char count = 0;
char max = 0;
char hexDigit = 0;
unsigned long long x = 0xABCDFFFF43FF42CF;
for ( i = 0; i < 16; i++)
{
if (( x >> i ) & 0xF) // checking bits to see if they match previous hex digit
{
count++; // count increases if they are the same
}
else
{
count = 0;
}
if (count > max)
{
max = count;
}
}
printf("Hex digit %x occured %d timesn", hexDigit, max);
}
我不确定如何实现找出哪个十六进制数字出现最多,因此非常感谢任何关于我可以去的方向的帮助。
我相信
这应该有效:
#include <stdio.h>
int main()
{
int i = 0;
char count = 0;
char max = 0;
char hexDigit = -1;
char answer;
unsigned long long x = 0xABCDFFFF43FF42CF;
for ( i = 0; i < 16; i++)
{
char curr = (x>>(4*i)) & 0xF;
if (curr==hexDigit) // checking bits to see if they match previous hex digit
{
count++; // count increases if they are the same
}
else
{
count = 1;
hexDigit = curr;
}
if (count > max)
{
max = count;
answer=hexDigit;
}
}
printf("Hex digit %x occured %d timesn", answer, max);
}
相关文章:
- 从char阵列中提取十六进制数字
- 如何在 c++ 中将十六进制数字转换为字符
- 使用字符串格式限制十六进制数字的数量
- 从输入中找到连续十六进制数字的最大数量
- 将十六进制数字从无符号字符转换为字符串
- 从 bin 文件 -c++ 中读取十六进制数字为每个字符 2 位数字
- 重载输出运算符给出十六进制数字而不是字符串
- 将十六进制数的 char[] 转换为对应于 ASCII 表中十六进制数字的字母的 char[] 并反转
- 从包含十六进制数字的std::string或QString创建std::bitset或QBitArray
- "first-chance exception..."消息中的十六进制数字是什么意思?
- 一次打印 2 个字节的十六进制数字
- 连接两个十六进制数字
- 分析64位十六进制数字
- 在一行代码中输入2个十六进制数字和一个字符串
- 十六进制数字系统中的指针值
- 将字符串转换为十六进制的最简单方法是什么?C++
- 将std::字符串数字转换为十六进制数字
- 格式化带符号十六进制数字的输出
- 如何使用stringstream格式化十六进制数字
- string控制字符:将十六进制数字赋给字符串