The design function, the parameter is an int value, returns the 16-way string of this number.

xiaoxiao2021-03-06  18

Today, I will make a small program under the guidance of the cockroach: the design function, the parameter is an int value, returns the 16-way string of this number.

Solution: #include

Char * func (int value) {char A [16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8' , '9', 'a', 'b', 'c', 'd', 'e', ​​'f'}; int b = 0x0f; char * ret = new char [11]; // This place can It is like it to be in the fineness of C, it is in a micro-carving for (INT i = 0; i <8; i) {// value & b is to use B as a mask, (Value & b) >> (i * 4) Is another shift, // a [(value & b) >> (i * 4)] is looking for the corresponding character RET [9-i] = a [(value & b) >> (i * 4)] ; // shift B left B = B << 4;} RET [0] = '0'; RET [1] = 'x'; RET [10] = '/ 0'; return RET;} main () {Char * pl = func (12); std :: cout << Pl << std :: end1; delete [] pl; // release stack memory PL = null; // After release PL, PL becomes a wild pointer, and then It is better to put him as NULL}

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

New Post(0)