#pragma once
#include <string>

namespace stds {
class tool {
public:
std::string ws2s(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest,0,_Dsize);

wcstombs(_Dest,_Source,_Dsize);
std::string result = _Dest;
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}

std::wstring s2ws(const std::string& s)
{
setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
std::wstring result = _Dest;
delete []_Dest;
setlocale(LC_ALL, "C");
return result;
}

std::wstring get_exeute_path() {
TCHAR* _buffer = _tgetcwd(NULL, 0);
TCHAR exeFullPath[MAX_PATH]={0};
GetModuleFileName(NULL, exeFullPath, MAX_PATH);
std::wstring sPath(exeFullPath);

std::string::size_type p_begin = sPath.find_last_of(TEXT("/\\")) + 1;
std::wstring sPath2 = sPath.replace(p_begin, sPath.length() - p_begin, TEXT(""));
return sPath2;
}
};
};

unicode string和ansi string的转换函数及获取程序运行路径的代码的更多相关文章

  1. 程序运行时间测试 - 使用系统函数 getrusage 获取程序运行时间

    https://github.com/yaowenxu/Workplace/blob/master/timer/getrusagetimer.c 关键结构体: struct rusage { stru ...

  2. delphi根据进程PID获取程序所在路径的函数(用OpenProcess取得句柄,用GetModuleFileNameEx取得程序名)

    uses psapi; {根据进程PID获取程序所在路径的函数}function GetProcessExePath(PID: Cardinal): string;varpHandle: THandl ...

  3. 【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

    下面程序演示了在嵌入式Linux和PC机Linux下使用popen函数时,程序的运行结果是有差异的. 两个程序 atest.c 和 btest.c,atest 检查是否有 btest 进程运行,如果没 ...

  4. VS2017中遇到不存在从string到const char*的转换函数的解决方法

    使用c_str()函数 c_str函数的返回值是const char*. c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有stri ...

  5. C# 两个获得程序运行路径的函数

    EXE文件的存储路径,不太受调用时环境变量的影响: Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Lo ...

  6. VC++编程中常用的字符串转换函数

    VC++编程中经常遇到不同编码编码的字符串之间需要转换的情况,以下简单提供几个不同编码字符串之间的转换函数: ANSI 字符串和Unicode字符串之间的转换 //Convert wide char ...

  7. UNICODE和ANSI字符串的转换(解释了MultiByteToWideChar,WideCharToMultiByte,GetTextCharsetInfo,GetTextCharset,IsDBCSLeadByte,IsDBCSLeadByteEx,IsTextUnicode一共7个函数)

    继上集故事<多字符集(ANSI)和UNICODE及字符串处理方式准则 >,我们现在有一些特殊需求: 有时候我们的字符串是多字符型,我们却需要使用宽字符型:有的时候却恰恰相反. Window ...

  8. Convert CString to ANSI string in UNICODE projects

    Convert CString to ANSI string in UNICODE projects Quick Answer: use an intermediate CStringA. Norma ...

  9. C#中的Byte,String,Int,Hex之间的转换函数。

    /// <summary> Convert a string of hex digits (ex: E4 CA B2) to a byte array. </summary> ...

随机推荐

  1. C++排序函数sort/qsort使用

    问题描述:        C++排序函数sort/qsort的使用 问题解决:           (1)sort函数使用   注:           sort函数,参数1为数组首地址,参数2是数组 ...

  2. 【BZOJ】【3398】【USACO 2009 Feb】Bullcow 牡牛和牝牛

    组合计数/乘法逆元 排列组合求总方案数 这个可以用一个一维的动态规划解决: f[i][0]表示第i头牛是牝牛的方案数 f[i][1]表示第i头牛是牡牛的方案数 则转移为:f[i][0]=f[i-1][ ...

  3. C#根据日期DateTime和持续时间int找到日期

    protected DateTime GetFinish(DateTime start, int duration) { return start.AddDays(duration); } prote ...

  4. 基于密度的聚类之Dbscan算法

    一.算法概述 DBSCAN(Density-Based Spatial Clustering of Applications with Noise)是一个比较有代表性的基于密度的聚类算法.与划分和层次 ...

  5. Caffe训练好的网络对图像分类

    对于训练好的Caffe 网络 输入:彩色or灰度图片 做minist 下手写识别分类,不能直接使用,需去除均值图像,同时将输入图像像素归一化到0-1直接即可. #include <caffe/c ...

  6. Curl的毫秒超时的一个”Bug”

    Curl的毫秒超时的一个”Bug” -- PHP老杨 最近我们的服务在升级php使用的libcurl, 期望新版本的libcurl支持毫秒级的超时, 从而可以更加精细的控制后端的接口超时, 从而提高整 ...

  7. crontab 指定执行用户

    linux下可以通过配置crontab来定时执行任务,执行体可以是一条系统命令或自己写的一个脚本,同时可以指派用户来执行.配置crontab有两种方法.方法1.使用crontab命令,例如添加一个新的 ...

  8. 架构探险——从零开始写Java Web框架》第二章照作

    沉下来慢慢看实现了. 越来越觉得可以和DJANGO作对比. package org.smart4j.chapter2.model; /** * Created by sahara on 2016/3/ ...

  9. 李洪强iOS学习交流群-iOS大神群

    iOS学习大神群-群号:483959373

  10. maven2 + tomcat6 + eclipse集成配置

    转载:http://wenku.baidu.com/view/d64147c676eeaeaad1f330d4.html?re=view /*maven2 + tomcat6 + eclipse集成配 ...