在 C 中操作十六进制

发布于 2024-10-20 02:35:11 字数 318 浏览 2 评论 0原文

我有一个 C 代码,在此期间我打开一个文件来输入一个十六进制数。然后我想将获得的数字添加到另一个一位十六进制数,最后以十六进制显示该数字。例如,我输入AC65E1,然后添加E并显示AC65EF。

有没有一种方法可以做到这一点,而无需转换为十进制并添加十进制,然后转换回十六进制?如果不是,时间最佳的方法是什么?

编辑:我认为我的问题被误解了,我想知道我是否可以使用十六进制的“+”?如果我不能,那么我将不得不使用更改为十进制来使用“+”运算符。对于任何打字错误,深表歉意!

现在我正在将文件中的数据读入 char hex_num[10];

I have a C code during which I open a file to input a hexadecimal number. Then I want to add the number obtained it to another single digit hexadecimal and finally display the number in hex. e.g. I input AC65E1 and I add E to it and display AC65EF.

Is there a way to do it without going through the conversion to decimal and adding in decimal and then converting back to hexadecimal? If no what is the time-optimum way to do it?

EDIT: I think my question my misinterpreted, I wanted to know if I can use the '+' with Hex? if I couldnt then I would have to use change to decimal to use the '+' operator. Sorry for any mis-typing!

Now I am reading the data from the file into a char hex_num[10];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

随波逐流 2024-10-27 02:35:11

实际上,计算机以二进制形式完成所有工作,因此除非您想花费大量无意义的努力来强制它对原始十六进制数字进行算术运算,否则您将需要解析这些数字,将它们相加并渲染结果再次以十六进制形式返回。

unsigned int value1, value2;
fscanf(fp, "%x %x", &value1, &value2)

unsigned int result = value1 + value2;
printf("%x\n", result);

Actually, computers do all their work in binary, so unless you want to go to a great deal of pointless effort to force it to do the arithmetic on the raw hex digits, you'll want to parse the numbers, add them, and render the result back out again as hex.

unsigned int value1, value2;
fscanf(fp, "%x %x", &value1, &value2)

unsigned int result = value1 + value2;
printf("%x\n", result);
土豪 2024-10-27 02:35:11

C 和其他语言中的数学并不是在任何特定的基础上完成的——除了最低级别的处理,当然,它们以二进制形式进行。

我认为您的问题已被破坏,因为您想将 E 添加到 AC65E1 并再次获得 .. AC65E1 。想必您指的是 AC65EF

我建议从文件中读取数据并将看起来像数字的字符串解析为整数,执行算术,然后以您希望的任何表示形式再次输出它们。

为此,请使用 strtol 传递 16 作为 base 参数,或使用 fscanf 读取第一个中的整数地方。

Mathematics in C and other languages are not done in any particular base — except at the lowest level of processing where, of course, they take place in binary.

I think your question is broken because you want to add E to AC65E1 and get .. AC65E1 again. Presumably you meant AC65EF.

I suggest read the data from the file and parse strings that look like numbers into integers, perform your arithmetic, then output them again in whatever representation you wish.

For this, use strtol passing 16 as the base parameter, or fscanf to read into an integer in the first place.

素食主义者 2024-10-27 02:35:11

您可以编写一个逐字符加法器,就像您在纸上自然地执行此操作一样,即比较最后一位数字“1”+“E”=“F”,无进位等。

这可能不会显着提高效率不过,假设您的代码以十六进制数进行解析是有效的(这比十进制数容易得多),并且假设您的数字足够小,可以容纳一个或两个机器寄存器 - 如果您有 1000+ 位十六进制字符串,那么逐个字符的方式可能是最简单的。

无论如何,这些都是从文件中读取的:IO 会比加法慢得多。所以我不会担心该添加的每一点性能。

You could write a character-by-character adder, the same way you would do this naturally on paper, i.e. compare last digits '1' + 'E' = 'F', no carry, etc.

That may not be significantly more efficient, though, assuming your code to parse in a hexadecimal number is efficient (it's a lot easier to do than decimal) and assuming your numbers are small enough to fit into a machine register or two - if you had 1000+ digit hex strings then the character-by-character way may be simplest.

And in any case these are read from a file: the IO will be much slower than the addition. So I wouldn't worry about every last bit of performance for the add.

迟到的我 2024-10-27 02:35:11

你看起来有点困惑。

如果您这样做:

int a = 43, b = 12;

printf("%d+%d=%d\n", a, b, a + b);

计算机没有执行任何“十进制加法”。加法以二进制形式完成,在编译时将文字转换为十进制文本,并在运行时将其转换为十进制文本以打印输出。

您应该简单地使用 strtol 来解析数字,或使用 sscanf() 并添加。

手动编写基于文本的例程来“添加十六进制”无论如何都不会更好。

You seem a bit confused.

If you do:

int a = 43, b = 12;

printf("%d+%d=%d\n", a, b, a + b);

the computer is not doing any "adding in decimal". The addition is done in binary, which is converted to/from decimal text at compile-time for the literals, and at run-time for the printout.

You should simply use strtol to parse the numbers, or sscanf(), and add.

Manually writing text-based routines to "add in hexadecimal" will not be better in any way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文