当没有数据类型可以容纳完整数字时,将十进制转换为十六进制
这几乎与我几周前提出的问题一模一样。
这次,情况正好相反。 我有这个数字(在一个方便的空终止字符串中),我需要组成这个数字的字节。 然而,我正在微控制器的 32 位架构中工作,因此我不可能使用 atoi,因为数字大于 32 位。
有谁知道如何反转第一个链接中提供的算法,以恢复原始结果? 我的模算术技能让我很失败。
简单示例:155.207.231.135 到 0x[24][23][12][66][9F](括号分隔字节)
This is an almost exact duplicate of my own question a few weeks ago.
Convert Hex to Decimal when no datatype can hold the full number
This time, it is the reverse. I have the number (in a handy null terminated string) and I need the bytes that make this number. However, I am working in a 32 bits architecture for a micro controller, therefore I don't have the possibility of using the atoi, as the number is larger than 32 bits.
Does anyone have an idea on how to reverse the algorithm provided in the first link, as to get back the original result? My modulo arithmetic skills are failing me.
Quick Example: 155.207.231.135 to 0x[24][23][12][66][9F] (the brackets separate the bytes)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行类似于 BigInt 除法的操作。
You can do something similar to BigInt division.
你需要这个的汇编程序。 伪代码:
基本上,您使用两个 32 位整数创建一个 64 位寄存器。 对于每个循环,您:
然后,您只需跳过结果值开头的 0 个字节即可获取您要查找的字节。
You'll need assembler for this one. Pseudocode:
So basically, you create a 64bit register using two 32bit ints. For each loop, you:
Afterwards, you only need to skip the 0 bytes at the beginning of the resulting value to get the bytes you seek.
只需从左到右解析字符串,将之前的结果乘以 10,然后加上数字即可。
下面是一些 C# 代码来展示这个概念。 对数组进行数学运算的前两种方法:
然后您只需执行以下操作:
结果位于
result
数组中,并在左侧填充零字节。翻译成 C 应该不难...:)
Just parse the string from left to right, multiplying the previous result by ten and adding the digit.
Here's some code in C# to show the concept. First two methods to to math on an array:
Then you just do:
The result is in the
result
array, padded with zero bytes to the left.It shouldn't bee to hard to translate to C... :)