Integer to ASCII string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include <stdio.h> #include <stdbool.h> char* Integer_To__ASCII__String(int integer, char* destination, int base); void Reverse_String(char* String, int length); void swap(char* current, char* next); int main(int argc, char *argv[]) { char String[100] = { 0, }; printf("%s\n", Integer_To__ASCII__String(123, String, 10)); return 0; } char* Integer_To__ASCII__String(int integer, char* destination, int base) { int index = 0; if (integer == 0) { *(destination + index++) = '0'; *(destination + index) = (char)0; return (char*)destination; } bool Is_Negative = false; if (integer < 0 && base == 10) { Is_Negative = true; integer = -integer; } while (integer != 0) { int remainder = integer % base; *(destination + index++) = (remainder > 9) ? (remainder - 10) + 'A' : remainder + '0'; integer /= base; } if (Is_Negative) *(destination + index++) = '-'; *(destination + index) = (char)0; Reverse_String((char*)destination, index); return (char*)destination; } void Reverse_String(char* String, int length) { int start = 0; int end = length - 1; while (start < end) { swap(String + start++, String + end--); } return; } void swap(char* current, char* next) { char temporary = 0; temporary = *current; *current = *next; *next = temporary; return; } | cs |
0 개의 댓글:
댓글 쓰기