C++存储数据单位转换输出字符串

devtools/2024/9/24 5:55:00/

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;
}


http://www.ppmy.cn/devtools/116360.html

相关文章

vue3知识汇总

vue3.x 0. changelog https://juejin.cn/post/7030992475271495711#heading-0 1. vite//要构建一个 Vite Vue 项目&#xff0c;运行&#xff0c;使用 NPM:npm init vitejs/app 项目名//使用 Yarn:yarn create vitejs/app 项目名//你会觉得非常快速的创建了项目&#xff0c;然…

vue-router路由(重定向,嵌套,动态路由匹配,命名,高亮,守卫)

一、前端路由的概念与原理 路由router就是对应关系。分为前端路由和后端路由。 1后端路由 后端路由指的是&#xff1a;请求方式、请求地址与function处理函数之间的对应关系。在node.js中&#xff0c;express理由的基本用法如下&#xff1a; const express require(expres…

联影医疗嵌入式面试题及参考答案(3万字长文)

假如你要做机器人控制,你会遵循怎样的开发流程? 首先,需求分析阶段。明确机器人的功能需求,例如是用于工业生产中的物料搬运、还是家庭服务中的清洁打扫等。了解工作环境的特点,包括空间大小、障碍物分布、温度湿度等因素。同时,确定机器人的性能指标,如运动速度、精度、…

mysql学习教程,从入门到精通,SQL LEFT JOIN 语句(23)

1、SQL LEFT JOIN 语句 在SQL中&#xff0c;LEFT JOIN&#xff08;也称为左连接&#xff09;是一种将左表&#xff08;LEFT JOIN左侧的表&#xff09;的所有记录与右表&#xff08;LEFT JOIN右侧的表&#xff09;中匹配的记录结合起来的查询方式。如果左表中的记录在右表中没有…

移动技术开发:注册案例

1 实验名称 注册案例 2 实验目的 掌握多个Activity的实现方法&#xff0c;Activity之间数据的交互实现&#xff0c;Intent对象的使用 3 实验源代码 布局文件代码&#xff1a; &#xff08;1&#xff09;activity_choose_city.xml <?xml version"1.0" encodi…

重修设计模式-结构型-享元模式

重修设计模式-结构型-享元模式 复用不可变对象&#xff0c;节省内存 享元模式&#xff08;Flyweight Pattern&#xff09;核心思想是通过共享对象方式&#xff0c;达到节省内存和提高性能的目的。享元对象需是不可变对象&#xff0c;因为它会被多处代码共享使用&#xff0c;要避…

从入门到精通:PHP 100个关键技术关键词

PHP 是一种广泛用于Web开发的服务器端脚本语言&#xff0c;以其简单易学和强大的功能而闻名。通过掌握本指南中的100个关键技术关键词&#xff0c;你将逐步了解PHP的核心概念、基本语法、数据库操作、会话管理、安全性和框架等方面的知识。每个关键词都配有详细的注释&#xff…

《Nginx核心技术》第18章:基于主从模式搭建Nginx+Keepalived双机热备环境

作者&#xff1a;冰河 星球&#xff1a;http://m6z.cn/6aeFbs 博客&#xff1a;https://binghe.gitcode.host 文章汇总&#xff1a;https://binghe.gitcode.host/md/all/all.html 星球项目地址&#xff1a;https://binghe.gitcode.host/md/zsxq/introduce.html 沉淀&#xff0c…