参考连接:
https://blog.csdn.net/u012500825/article/details/41947013
C++ 字符、字符串转十六进制(支持中文字符串转换)
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <string>
#include <io.h>
#include <sstream>
std::string chToHex(unsigned char ch)
{
const std::string hex = "0123456789ABCDEF";
std::stringstream ss;
ss << hex[ch >> 4] << hex[ch & 0xf];
return ss.str();
}
std::string strToHex(std::string str,std::string separator="")
{
const std::string hex = "0123456789ABCDEF";
std::stringstream ss;
for (std::string::size_type i = 0; i < str.size(); ++i)
ss << hex[(unsigned char)str[i] >> 4] << hex[(unsigned char)str[i] & 0xf] << separator;
return ss.str();
}
int main()
{
using std::string;
string path = "F:\\mrm\\publicShare\\thumbnails\\*.jpeg";
intptr_t handle;
struct _finddata_t fileinfo;
handle = _findfirst(path.c_str(), &fileinfo);
if (handle == -1) {
std::cout << "Failed to find first file!\n";
return -1;
}
string oldPath;
string newPath;
do
{
printf("%s\n", fileinfo.name);
string p = path.substr(0, path.find_last_of('\\')+1);
string oldName = fileinfo.name;
string on = oldName.substr(0, oldName.find_last_of('.'));
string oldPath2 = oldPath.assign(p).append(on).append(".jpeg");
/*string oldName = fileinfo.name;
string on = oldName.substr(0, oldName.find_last_of('.'));
string newName = std::to_string(strtol(on.c_str(),NULL,16));
string nn = newName.append(".jpeg");*/
std::string str = strToHex(on);
string nn = str.append(".jpeg");
string newPath2 = newPath.assign(p).append(nn);
bool b = rename(oldPath2.c_str(), newPath2.c_str());
std::cout << oldPath2<<"--"<<newPath2<<" bool is "<<b<<"\n";
/*rename(oldName.c_str(), newName.c_str());*/
} while (!_findnext(handle, &fileinfo));
_findclose(handle);
return 0;
}