Reference Links:
https://blog.csdn.net/u012500825/article/details/41947013
C++ char, string to hex (supports Chinese string conversion)
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 68 69 70 71
|
#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");
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"; } while (!_findnext(handle, &fileinfo)); _findclose(handle);
return 0; }
|