当没有数据类型可以容纳完整数字时,将十进制转换为十六进制

发布于 2024-08-01 13:29:34 字数 445 浏览 6 评论 0原文

这几乎与我几周前提出的问题一模一样。

当没有数据类型可以时将十六进制转换为十进制持有全数

这次,情况正好相反。 我有这个数字(在一个方便的空终止字符串中),我需要组成这个数字的字节。 然而,我正在微控制器的 32 位架构中工作,因此我不可能使用 atoi,因为数字大于 32 位。

有谁知道如何反转第一个链接中提供的算法,以恢复原始结果? 我的模算术技能让我很失败。

简单示例:155.207.231.1350x[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 技术交流群。

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

发布评论

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

评论(3

橘虞初梦 2024-08-08 13:29:34

您可以执行类似于 BigInt 除法的操作。

a = atoi of lower 7 decimal digits
b = atoi of remaining upper decimal digits


for (int i = 0; i < 5; i++)
{
    a += 10000000 * (b % 256);
    b /= 256;
    Result[i] = a % 256;
    a /= 256;
}

You can do something similar to BigInt division.

a = atoi of lower 7 decimal digits
b = atoi of remaining upper decimal digits


for (int i = 0; i < 5; i++)
{
    a += 10000000 * (b % 256);
    b /= 256;
    Result[i] = a % 256;
    a /= 256;
}
南薇 2024-08-08 13:29:34

你需要这个的汇编程序。 伪代码:

int low = 0 // lower 32 bit
int high = 0 // higher 32 bit

for (int i=0; i<string.length(); i++) {
    int digit = string.get(i) - '0';
    int a = low;
    int b = high;
    a <<= 1; b += overflow;             // *2
    a <<= 1; b += overflow;             // *4
    a += low; b += overflow; b += high; // *5
    a <<= 1; b += overflow;             // *10
    a += digit; b += overflow;          // +digit
    low = a; high = b;
}

基本上,您使用两个 32 位整数创建一个 64 位寄存器。 对于每个循环,您:

    value *= 10 + digit;

然后,您只需跳过结果值开头的 0 个字节即可获取您要查找的字节。

You'll need assembler for this one. Pseudocode:

int low = 0 // lower 32 bit
int high = 0 // higher 32 bit

for (int i=0; i<string.length(); i++) {
    int digit = string.get(i) - '0';
    int a = low;
    int b = high;
    a <<= 1; b += overflow;             // *2
    a <<= 1; b += overflow;             // *4
    a += low; b += overflow; b += high; // *5
    a <<= 1; b += overflow;             // *10
    a += digit; b += overflow;          // +digit
    low = a; high = b;
}

So basically, you create a 64bit register using two 32bit ints. For each loop, you:

    value *= 10 + digit;

Afterwards, you only need to skip the 0 bytes at the beginning of the resulting value to get the bytes you seek.

等待圉鍢 2024-08-08 13:29:34

只需从左到右解析字符串,将之前的结果乘以 10,然后加上数字即可。

下面是一些 C# 代码来展示这个概念。 对数组进行数学运算的前两种方法:

static void Mul(byte[] data, int num) {
   int n = 0;
   for (int i = data.Length - 1; i >= 0; i--) {
      n += (int)data[i] * num;
      data[i] = (byte)n;
      n >>= 8;
   }
}

static void Add(byte[] data, int num) {
   for (int i = data.Length - 1; num > 0; i-- ) {
      num += (int)data[i];
      data[i] = (byte)num;
      num >>= 8;
   }
}

然后您只需执行以下操作:

string s = "155207231135";
byte[] result = new byte[16];
foreach (char c in s) {
   Mul(result, 10);
   Add(result, c - '0');
}

结果位于 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:

static void Mul(byte[] data, int num) {
   int n = 0;
   for (int i = data.Length - 1; i >= 0; i--) {
      n += (int)data[i] * num;
      data[i] = (byte)n;
      n >>= 8;
   }
}

static void Add(byte[] data, int num) {
   for (int i = data.Length - 1; num > 0; i-- ) {
      num += (int)data[i];
      data[i] = (byte)num;
      num >>= 8;
   }
}

Then you just do:

string s = "155207231135";
byte[] result = new byte[16];
foreach (char c in s) {
   Mul(result, 10);
   Add(result, c - '0');
}

The result is in the result array, padded with zero bytes to the left.

It shouldn't bee to hard to translate to C... :)

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