📄 pstrconv.cxx
字号:
/* * * C++ Portable Types Library (PTypes) * Version 1.7.5 Released 9-Mar-2003 * * Copyright (c) 2001, 2002, 2003 Hovik Melikyan * * http://www.melikyan.com/ptypes/ * http://ptypes.sourceforge.net/ * */#include <string.h>#include "ptypes.h"PTYPES_BEGINstatic char* _itobase(large value, char* buf, int base, int& len){ // internal conversion routine: converts the value to a string // at the end of the buffer and returns a pointer to the first // character. this is to get rid of copying the string to the // beginning of the buffer, since finally the string is supposed // to be copied to a dynamic string in itostring(). the buffer // must be at least 65 bytes long. static char digits[65] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; char* pdigits; if (base > 16) pdigits = digits; else pdigits = digits + 2; int i = 64; buf[i] = 0; bool neg = false; ularge v = value; if (base == 10 && value < 0) { v = -value; // since we can't handle the lowest signed 64-bit value, we just // return a built-in string. if (large(v) < 0) // the LLONG_MIN negated results in the same value { len = 20; return "-9223372036854775808"; } neg = true; } do { buf[--i] = pdigits[v % base]; v /= base; } while (v > 0); if (neg) buf[--i] = '-'; len = 64 - i; return buf + i;}string itobase(large value, int base, int width, char padchar) { if (base < 2 || base > 64) return ""; char buf[65]; // the longest possible string is when base=2 int reslen; char* p = _itobase(value, buf, base, reslen); if (width > reslen) { if (padchar == 0) { if (base == 10) padchar = ' '; else if (base > 16) padchar = '.'; else padchar = '0'; } string res; setlength(res, width); bool neg = *p == '-'; width -= reslen; memset(pchar(pconst(res)) + neg, padchar, width); memcpy(pchar(pconst(res)) + width + neg, p + neg, reslen - neg); if (neg) *pchar(pconst(res)) = '-'; return res; } else return string(p, reslen);}string itostring(int v) { return itobase(v, 10, 0, ' ');}string itostring(large v) { return itobase(v, 10, 0, ' ');}large stringtoi(const char* p){ if (p == 0) return -1; if (*p == 0) return -1; large r = 0; do { char c = *p++; if (c < '0' || c > '9') return -1; // invalid character large t = r * 10; if (t < r) return -1; // overflow r = t + (c - '0'); } while (*p != 0); return r;}PTYPES_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -