簡體   English   中英

如何將5字節的十六進制數轉換為10個基數並以C型字符串表示?

[英]How to convert a 5-bytes hex number to 10base number and represent it in C-type string?

我有一個由5個字節組成的十六進制數字:0xEEDDCCBBAA。 此數字存儲在數組中:[0xEE 0xDD,0xCC,0xBB,0xAA]。 我想將其轉換為10base十進制數,並將其表示為C型字符串:“ 1025923398570”。 (0xEEDDCCBBAA等於1,025,923,398,570)

一個問題是我的系統僅支持最高。 32位類型變量,因此我不能使用單個變量來存儲此數字或執行大於32位的任何類型的操作。 例如,我不能:

unsigned long long val = 0xeeddccbbaa;
unsigned char buf[50];
sprintf(buf, "%d", val);

非常感謝你。

編輯:由於我的問題似乎令人困惑,所以我再次寫了它。

可以創建一個通用例程來處理任何大小的字節數組。

#include <string.h>

char *convert_10base_decimal_number(char *dest, size_t dsize, unsigned char *a,
    size_t asize) {

  if (dsize == 0)
    return NULL;
  char *d = dest + dsize - 1;
  *d = '\0';
  size_t ai = 0;
  do {
    // mod 10
    unsigned carry = 0;
    for (size_t i = ai; i < asize; i++) {
      carry = carry * 256 + a[i];
      a[i] = carry / 10;
      carry %= 10;
    }
    if (d == dest)
      return NULL;
    d--;
    *d = carry + '0';
    if (a[ai] == 0)
      ai++;
  } while (ai < asize);
  memmove(dest, d, dsize - (d - dest));
  return (dest);
}

int main(void) {
  unsigned char a[] = { 0xEE, 0xDD, 0xCC, 0xBB, 0xAA };
  char buf[50];

  puts(convert_10base_decimal_number(buf, sizeof buf, a, sizeof a));
  // 1025923398570

  return 0;
}

此方法確實消耗了數組a

long x,y;
x = a[0] | (a[1] << 8) | (a[2] << 16) || (a[3] << 24)
y = a[4]

因此, x0xEEDDCCBBy0xAA您可以使用sprintf將x轉換為字符串,然后將其串聯為y。

這里真正的挑戰是僅使用32位算術轉換為基數10

令您的64位數字為N = M.2^32 + L ,然后使用分解X = 10 (X/10) + (X%10) = 10 Xq + Xr

然后,

N = (10.Mq + Mr)(10.429496729 + 6) + (10.Lq + Lr) = 100.429496729.Mq + 10.6.Mq + 10.429496729.Mr + 6.Mr + 10.Lq + Lr.

由此我們得出單位數字

Nr = N%10 = (6.Mr + Lr) % 10,

和商

Nq = N/10 = 10.429496729.Mq + 6.Mq + 429496729.Mr + Lq + (6.Mr + Lr) / 10.

在后一個表達式中,前兩項只是Mq.2^32 ,因此您將最高有效的雙字除以10 下一個項將適合32位,因為Mr最多為9429496729.9小於2^32 我認為從LSD到MSD不會產生進位,但是應該仔細檢查。

您可以繼續該除法過程以提取下一個數字,直到MSD變為0(然后繼續單獨使用LSD)。

在你的例子中

M = 0xEE = 238 = 10 * 23 + 8, N = 0xDDCCBBAA = 3721182122 = 10 * 372118212 + 2

單位數字為Nr = (6 * 8 + 2) % 10 = 0

10的商是

M' = 238 / 10 = 23, L' = 429496729 * 8 + 372118212 + (6 * 8 + 2) / 10 = 3808092049.

(如您所查, 23 * 2^32 + 3808092049= 102592339857

下一個數字是N'r = (6 * 3 + 9) % 10 = 7

10的商是

M" = 23 / 10 = 2, L" = 429496729 * 3 + 380809204 + (6 * 3 + 9) / 10 = 1669299393.

(您可以檢查2 * 2^32 + 1669299393 = 10259233985

下一個數字是N"r = (6 * 2 + 3) % 10 = 5

10的商是

M"' = 2 / 10 = 0, L"' = 429496729 * 2 + 166929939 + (6 * 2 + 3) / 10 = 1025923398.

M"' = 0 ,您現在可以繼續以純32位進行轉換。

char a[5] = {0xEE, 0xDD, 0xCC, 0xBB, 0xAA};

// @Shmoopy's piece of code
int x = a[0] | (a[1] << 8) | (a[2] << 16) || (a[3] << 24);
int y = a[4];

// making long long from x and y, where y is the mo;
long long z = (long long)y << 32 | x;

// conversion of long long to string with base 10 (2^64 has max 19 decimal digits)
char str[20] = {};
lltoa(z, str, 10);
printf("The number is %s.\n", str);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM