C++封装存储数据单位转换, 方便将输入数据以指定方式输出
main.cpp
#include <wtypesbase.h>
#include <string>
#include <vector>
#include <tchar.h>#ifdef _UNICODE
using _tstring = std::wstring;
#else
using _tstring = std::string;
#endif// 数据单位
enum eUtilType
{eUT_Auto, // 自动eUT_b, // (2 ^ 000) * 8 BiteUT_Kb, // (2 ^ 010) * 8 BiteUT_Mb, // (2 ^ 020) * 8 BiteUT_Gb, // (2 ^ 030) * 8 BiteUT_Tb, // (2 ^ 040) * 8 BiteUT_Pb, // (2 ^ 050) * 8 BiteUT_Eb, // (2 ^ 060) * 8 BiteUT_Zb, // (2 ^ 070) * 8 BiteUT_Yb, // (2 ^ 080) * 8 BiteUT_Bb, // (2 ^ 090) * 8 BiteUT_Nb, // (2 ^ 100) * 8 BiteUT_Db, // (2 ^ 110) * 8 BiteUT_Cb, // (2 ^ 120) * 8 BiteUT_Xb, // (2 ^ 130) * 8 BiteUT_B, // Byte 2 ^ 000 ByteeUT_KB, // Kilobyte 2 ^ 010 Byte eUT_MB, // Megabyte 2 ^ 020 Byte eUT_GB, // Gigabyte 2 ^ 030 Byte eUT_TB, // Terabyte 2 ^ 040 Byte eUT_PB, // Petabyte 2 ^ 050 Byte eUT_EB, // Exabyte 2 ^ 060 Byte eUT_ZB, // Zettabyte 2 ^ 070 Byte eUT_YB, // Yottabyte 2 ^ 080 Byte eUT_BB, // Brontobyte 2 ^ 090 Byte eUT_NB, // NonaByte 2 ^ 100 Byte eUT_DB, // DoggaByte 2 ^ 110 Byte eUT_CB, // corydonbyte 2 ^ 120 Byte eUT_XB, // Xerobyte 2 ^ 130 ByteeUT_Max
};typedef struct _DATA_UTIL_INFO
{double value; // 数值eUtilType eUtil; // 单位_tstring strUtilStr; // 单位字符串_tstring strContent; // 内容(数值 + 单位字符串)
}DATA_UTIL_INFO;DATA_UTIL_INFO FormatByteSize(double nBytesSize, // 输入值eUtilType eSrcUtil = eUtilType::eUT_Auto, // 原始单位eUtilType eDestUtil = eUtilType::eUT_Auto, // 目标单位bool fHasUtils = true, // 结果字符串数值添加单位bool fSpace = true, // 结果字符串数值与单位之间添加空格int nInteger = 1, // 整数部分长度int nPrecision = 3 // 小数部分长度
);void ConsoleOutput(LPCTSTR pFormat, ...);int main()
{ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_B, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_KB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_MB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_GB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_TB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_PB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_EB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_ZB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_YB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_BB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_NB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_DB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_CB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());ConsoleOutput(_T("%s\r\n"), FormatByteSize(1, eUtilType::eUT_XB, eUtilType::eUT_B, true, true, 0, 2).strContent.c_str());return 0;
}void ConsoleOutput(LPCTSTR pFormat, ...)
{size_t nCchCount = MAX_PATH;_tstring strResult(nCchCount, 0);va_list args;va_start(args, pFormat);do{//格式化输出字符串int nSize = _vsntprintf_s(&strResult[0], nCchCount, _TRUNCATE, pFormat, args);if (-1 != nSize){HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);::WriteConsole(console, strResult.c_str(), nSize, NULL, NULL);break;}//缓冲大小超限终止if (nCchCount >= INT32_MAX){break;}//重新分配缓冲nCchCount *= 2;strResult.resize(nCchCount);} while (true);va_end(args);
}DATA_UTIL_INFO FormatByteSize(double nBytesSize,eUtilType eSrcUtil/* = eUtilType::eUT_Auto*/,eUtilType eDestUtil/* = eUtilType::eUT_Auto*/,bool fHasUtils/* = true*/,bool fSpace/* = true*/,int nInteger/* = 1*/,int nPrecision/* = 3*/
)
{TCHAR szFormatBuf[MAX_PATH] = { 0 };TCHAR szResultBuf[MAX_PATH] = { 0 };DATA_UTIL_INFO dataUtilInfo;bool fSrcBit = false;bool fDestBit = false;// 原始单位 比特 -> 字节if (eSrcUtil >= eUtilType::eUT_b && eSrcUtil < eUtilType::eUT_B){fSrcBit = true;eSrcUtil = (eUtilType)(eSrcUtil + (eUtilType)(eUtilType::eUT_B - eUtilType::eUT_b));}// 目标单位 比特 -> 字节if (eDestUtil >= eUtilType::eUT_b && eDestUtil < eUtilType::eUT_B){fDestBit = true;eDestUtil = (eUtilType)(eDestUtil + (eUtilType)(eUtilType::eUT_B - eUtilType::eUT_b));}// 原始单位转换for (int i = eUtilType::eUT_B; i < eSrcUtil; i++){nBytesSize *= 1024;}LPCTSTR strUtilByteName[] = {_T("B"),_T("KB"),_T("MB"),_T("GB"),_T("TB"),_T("PB"),_T("EB"),_T("ZB"),_T("YB"),_T("BB"),_T("NB"),_T("DB"),_T("CB"),_T("XB"),};LPCTSTR strUtilBitName[] = {_T("b"),_T("Kb"),_T("Mb"),_T("Gb"),_T("Tb"),_T("Pb"),_T("Eb"),_T("Zb"),_T("Yb"),_T("Bb"),_T("Nb"),_T("Db"),_T("Cb"),_T("Xb"),};// 自动int nUtilTypeIndex = eUtilType::eUT_B;if (eUtilType::eUT_Auto == eDestUtil){double nCurUtilSize = 1.0f;double nNextUtilSize = 1024.0f;int nUtilTypeMaxIndex = eUtilType::eUT_Max - 1;for (int i = 0; i < _countof(strUtilByteName) && nUtilTypeIndex < nUtilTypeMaxIndex; i++){if ((nBytesSize >= nCurUtilSize && nBytesSize < nNextUtilSize) || 0 == nNextUtilSize || 0 == nBytesSize){break;}nCurUtilSize *= 1024;nNextUtilSize *= 1024;nUtilTypeIndex++;}eDestUtil = (eUtilType)nUtilTypeIndex;}{::_stprintf_s(szFormatBuf, _countof(szFormatBuf), _T("%%%d.%dlf"), nInteger + nPrecision + 1, nPrecision);if (fDestBit){nBytesSize *= 8;}double fUtilSize = 1.0f;for (int i = eUtilType::eUT_B; i < eDestUtil; i++){fUtilSize *= 1024.0f;}if (fSrcBit){fUtilSize *= 8.0f;}double lfResult = nBytesSize / fUtilSize;::_stprintf_s(szResultBuf, _countof(szResultBuf), szFormatBuf, lfResult);dataUtilInfo.strContent = szResultBuf;dataUtilInfo.value = lfResult;if (fHasUtils){if (fSpace){dataUtilInfo.strContent += _T(" ");}if (fDestBit){dataUtilInfo.strContent += strUtilBitName[eDestUtil - eUtilType::eUT_B];dataUtilInfo.strUtilStr = strUtilBitName[eDestUtil - eUtilType::eUT_B];dataUtilInfo.eUtil = (eUtilType)(eDestUtil + (eUtilType::eUT_B - eUtilType::eUT_b));}else{dataUtilInfo.strContent += strUtilByteName[eDestUtil - eUtilType::eUT_B];dataUtilInfo.strUtilStr = strUtilByteName[eDestUtil - eUtilType::eUT_B];dataUtilInfo.eUtil = eDestUtil;}}}return dataUtilInfo;
}