将 _TCHAR* 转换为 char* [英] Converting _TCHAR* to char*

查看:24
本文介绍了将 _TCHAR* 转换为 char*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 一个简单的 OpenCV 示例 在 Windows 上使用 C++ 工作,而我的 C++ 不仅仅是生疏.

I'm trying to get a simple OpenCV sample working in C++ on Windows and my C++ is more than rusty.

示例 相当简单:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
        cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
        return -1;
    }
    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file
    if(! image.data )                      // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }
    namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
    imshow( "Display window", image );                // Show our image inside it.
    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

当我在 Visual Studio 2012 中创建一个新的简单 C++ 控制台应用程序(使用 ATL)时,我得到了一个不同的 main 模板:

When I make a new simple C++ console application (with ATL) in Visual Studio 2012 I get a different template for main:

int _tmain( int argc, _TCHAR* argv[] )

所以在我将文件名发送到 OpenCV 的 imread 函数之前,我需要将 _TCHAR* arg[1] 转换为 字符*.使用简单的文件名opencv-logo.jpg",从内存窗口的内存中我可以看到 _TCHAR 每个占用两个字节

So before I send the filename to OpenCV's imread function I need to convert the _TCHAR* arg[1] to a char*. Using a simple filename, 'opencv-logo.jpg', from the memory in the memory window I can see that the _TCHAR are taking two bytes each

o.p.e.n.c.v.-.l.o.g.o...j.p.g...
6f 00 70 00 65 00 6e 00 63 00 76 00 2d 00 6c 00 6f 00 67 00 6f 00 2e 00 6a 00 70 00 67 00 00 00

按照另一个答案中的转换建议,我正在尝试使用ATL 7.0 字符串转换类和宏,插入以下代码:

Following the conversion recommendation in another answer I am trying to use ATL 7.0 String Conversion Classes and Macros by inserting the following code:

char* filename = CT2A(argv[1]);

但是产生的内存是一团糟,肯定不是'opencv-logo.jpg'作为ascii字符串:

But the resulting memory is a mess, certainly not 'opencv-logo.jpg' as an ascii string:

fe fe fe fe fe fe fe fe fe fe ...
þþþþþþþþþþ ...

我应该使用哪种转换技术、函数或宏?

Which conversion technique, function, or macro should I be using?

(注意可能是一个相关的问题,但我看不到如何申请答案在这里.)

(N.B. This may be a related question but I cannot see how to apply the answer here.)

推荐答案

最快的解决方案是将签名更改为标准签名.替换:

The quickest solution is to just change the signature to the standard one. Replace:

int _tmain( int argc, _TCHAR* argv[] )

int main( int argc, char *argv[] )

这确实意味着在 Windows 上,命令行参数被转换为系统的语言环境编码,并且由于 Windows 不支持 UTF-8,因此并非所有内容都能正确转换.但是,除非您确实需要国际化,否则可能不值得花时间做更多事情.

This does mean on Windows that the command line arguments get converted to the system's locale encoding and since Windows doesn't support UTF-8 here not everything converts correctly. However unless you actually need internationalization then it may not be worth your time to do anything more.

这篇关于将 _TCHAR* 转换为 char*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆