簡體   English   中英

不正確的十進制到十六進制轉換-C

[英]Incorrect decimal to hexadecimal conversion - C

我從找到的偽代碼編寫了此函數,該偽代碼應將十進制輸入轉換為十六進制數字。 可以做到這一點,但是順序不正確,例如,對於十進制數字195,我得到3C,由C3插入。

int temp=0;
int i=0;
while(decimal!=0)
{
    temp = decimal % 16;
    if( temp < 10)
        temp =temp + 48;
    else
        temp = temp + 55;
    array[i++]= temp;
    decimal = decimal / 16;
}

這樣可以節省一些時間

#include <stdio.h>
// ...
sprintf(hexStr,"%X",decimal);   // or, "%#X" if you want prefix

除非這是編程課程的功課。 在這種情況下,您實際上應該只在白板或紙上進行處理,我確定您會看到自己的錯誤。

暫無
暫無

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

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