将std :: string转换为(byte *,DWORD) [英] convert std::string to (byte*, DWORD)

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

问题描述

我有std :: string类型的图像的内容。如何用这种类型制作一个

CxImage对象。

CxImage的参数是:


CxImage (字节*数据,DWORD大小)


Thx提前

解决方案

Khuong Dinh Pham写道:< blockquote class =post_quotes>我有std :: string类型图像的内容。如何用这种类型制作一个
CxImage对象。

CxImage的参数是:

CxImage(byte * data,DWORD size)




std :: string stuff;


CxImage(convert_to_byte(stuff.data()),stuff.size())


convert_to_byte可以简单如下:


const_cast< byte *>(stuff.data())


祝你好运


Khuong Dinh Pham写道:

我有std :: string类型图像的内容。如何用这种类型制作一个
CxImage对象。

CxImage的参数是:

CxImage(byte * data,DWORD size)

Thx提前



std :: string.c_str()返回一个''const char *''(一个指针

到一个字符串无法修改的。


std :: string.length()返回内容的数据长度



因为CxImage()采用''byte *''而不是

''const byte *'',我希望它会修改

它的第一个arg(''data'')所指向的内存。


如果CxImage()没有修改它所指向的内存,那么首先是
arg(''data''),你的图像是在一个名为

''myStr'的std :: string中,那么你可能会尝试这样的事情:


CxImage(myStr.c_str(),myStr.length());


你可能需要添加一些演员来转换第一个arg

到''byte *' '和第二个arg到''DWORD''。


如果CxImage()修改了它的第一个指向的内存,那么这个数据就会被修改为'b $ b arg'(''数据''),然后你可以将字符串复制到char buf:


try {

char * buf = new char [myStr.length()] ;

memcpy(buf,myStr.c_str(),myStr.length());

CxImage(buf,myStr.length());

delete [] buf;

}

catch(std :: bad_alloc){

std :: cerr<< 内存分配失败\ n;

}


拉里


拉里一世史密斯写道:

Khuong Dinh Pham写道:

我有std :: string类型图像的内容。如何用这种类型制作一个
CxImage对象。

CxImage的参数是:

CxImage(byte * data,DWORD size)

Thx提前



std :: string.c_str()返回一个''const char *''(一个指针
到一个不能是的字符串修改)。

std :: string.length()返回内容的数据长度。

因为CxImage()采用''byte *' '而不是
''const byte *'',我希望它能修改第一个arg指向的内存(''data'')。

如果CxImage()不修改其第一个arg(''data'')所指向的内存,并且你的图像是一个名为
''myStr'的std :: string,那么你可能会尝试这样的事情:

CxImage(myStr.c_str(),myStr.length());

您可能需要添加一些转换来转换第一个arg
到''byte *''和第二个arg到''DWORD''。

如果是CxImage( )DOES修改其第一个arg(''''''指向的内存,然后你可以将字符串复制到char buf:

尝试{
char * buf = new char [myStr.length()];
memcpy(buf,myStr.c_str(),myStr.length());
CxImage(buf,myStr.length());
delete [] buf;
}
catch(std :: bad_alloc){
std :: cerr<< 内存分配失败\ nn;
}
拉里




我更喜欢:


std :: vector< char> buf(myStr.begin(),myStr.end());

CxImage(& buf [0],buf.size());


不需要尝试/捕获阻止。


I have the contents of an image of type std::string. How can I make a
CxImage object with this type.

The parameters to CxImage is:

CxImage(byte* data, DWORD size)

Thx in advance

解决方案

Khuong Dinh Pham wrote:

I have the contents of an image of type std::string. How can I make a
CxImage object with this type.

The parameters to CxImage is:

CxImage(byte* data, DWORD size)



std::string stuff;

CxImage( convert_to_byte( stuff.data() ), stuff.size() )

convert_to_byte could be as simple as:

const_cast<byte *>( stuff.data() )

good luck


Khuong Dinh Pham wrote:

I have the contents of an image of type std::string. How can I make a
CxImage object with this type.

The parameters to CxImage is:

CxImage(byte* data, DWORD size)

Thx in advance


std::string.c_str() returns a ''const char *'' (a pointer
to a string that can not be modified).

std::string.length() returns the data length
of the content.

Since CxImage() takes a ''byte *'' rather than a
''const byte *'', I would expect that it modifies
the memory pointed to by its first arg (''data'').

If CxImage() does NOT modify the memory pointed to by its
first arg (''data''), and your image is in a std::string named
''myStr'', then you might try something like this:

CxImage(myStr.c_str(), myStr.length());

You may have to add some casts to convert the first arg
to ''byte *'' and the 2nd arg to ''DWORD''.

If CxImage() DOES modify the memory pointed to by its first
arg (''data''), then you could copy the string to a char buf:

try {
char * buf = new char[myStr.length()];
memcpy(buf, myStr.c_str(), myStr.length());
CxImage(buf, myStr.length());
delete[] buf;
}
catch (std::bad_alloc) {
std::cerr << "memory allocation failed\n";
}

Larry


Larry I Smith wrote:

Khuong Dinh Pham wrote:

I have the contents of an image of type std::string. How can I make a
CxImage object with this type.

The parameters to CxImage is:

CxImage(byte* data, DWORD size)

Thx in advance



std::string.c_str() returns a ''const char *'' (a pointer
to a string that can not be modified).

std::string.length() returns the data length
of the content.

Since CxImage() takes a ''byte *'' rather than a
''const byte *'', I would expect that it modifies
the memory pointed to by its first arg (''data'').

If CxImage() does NOT modify the memory pointed to by its
first arg (''data''), and your image is in a std::string named
''myStr'', then you might try something like this:

CxImage(myStr.c_str(), myStr.length());

You may have to add some casts to convert the first arg
to ''byte *'' and the 2nd arg to ''DWORD''.

If CxImage() DOES modify the memory pointed to by its first
arg (''data''), then you could copy the string to a char buf:

try {
char * buf = new char[myStr.length()];
memcpy(buf, myStr.c_str(), myStr.length());
CxImage(buf, myStr.length());
delete[] buf;
}
catch (std::bad_alloc) {
std::cerr << "memory allocation failed\n";
}

Larry



I''d prefer:

std::vector<char> buf(myStr.begin(), myStr.end());
CxImage(&buf[0],buf.size());

No try/catch block necessary.


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

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