C++ 获取文件夹下所有文件并修改为16进制名

参考连接:
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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值