Implement String tohex (int) converts a decimal to hexadecimal. (Completely used algorithm)

xiaoxiao2021-03-05  32

The topic is very simple, that is, the decimation of the decimation is 16, and all the remaining amounts are tired. I use C simple implementation, below is code (VC . Net / Windows XP): #include

#include

Using namespace std; string tohex (int dec) {unsigned int udec = (dec> = 0)? (DEC): (- DEC), YUSHU; INT POS = 8; char HEX [11]; MEMSET (HEX, '0 ', 9); HEX [9] =' h '; HEX [10] =' / 0 '; do // Resudient number {YUSHU = UDEC% 16; HEX [POS ---] = (YUSHU> 9)? YUSHU 55) :( YUSHU 48); udec = udec / 16;} while (udec! = 0); if (HEX [POS 1]> 64) // If the highest bit is a letter, the string is in front of the string 0 HEX [POS] = '0'; ELSE POS; if (DEC <0) // If it is negative, load the number Hex [- POS] = '-'; return (String (HEX POS )));} int _tmain (int Argc, _tchar * argv []) {INT i; cin >> i; string shex = tohex (i); cout <

<

转载请注明原文地址:https://www.9cbs.com/read-34010.html

New Post(0)