流程上的帮助很小...... [英] Little help on stream...

查看:62
本文介绍了流程上的帮助很小......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我试图在c ++代码中摆脱一个sprintf,但是我尝试了几个方法来获得
并且不能弄清楚如何改变:


uint16_t group,uint16_t element;

sprintf(buffer,"%04x |%04x",group,element);


到目前为止我有类似的东西:


std :: ostringstream buf;

buf.flags(std :: ios_base :: right | std :: ios_base :: hex);

buf.width(4);

buf<< group;

buf<< " |" ;;

buf<< std :: hex<<元素;

std :: string key = buf.str();


但似乎没有用......


感谢您的帮助/指针

Mathieu

Hello,

I am trying to get rid a of sprintf in a c++ code, but I tried in
several ways and couldn''t figure out how to change:

uint16_t group, uint16_t element;
sprintf(buffer, "%04x|%04x", group , element);

so far I have something like:

std::ostringstream buf;
buf.flags ( std::ios_base::right |std::ios_base::hex );
buf.width( 4 );
buf << group;
buf << "|";
buf << std::hex << element;
std::string key = buf.str();

But doesn''t seems to work...

Thanks for any help/pointer
Mathieu

推荐答案

" Mathieu Malaterre" <毫安******* @ Mfree.fr>写道...
"Mathieu Malaterre" <ma*******@Mfree.fr> wrote...
我试图摆脱c ++代码中的sprintf,但我尝试了几种方式,并且无法弄清楚如何改变:

uint16_t group,uint16_t element;
sprintf(buffer,"%04x |%04x",group,element);

到目前为止,我有类似的东西:

std :: ostringstream buf;
buf.flags(std :: ios_base :: right | std :: ios_base :: hex);
buf.width(4);
buf<< group;
buf<< " |" ;;
buf<< std :: hex<<元素;
std :: string key = buf.str();

但似乎无法工作......
I am trying to get rid a of sprintf in a c++ code, but I tried in several
ways and couldn''t figure out how to change:

uint16_t group, uint16_t element;
sprintf(buffer, "%04x|%04x", group , element);

so far I have something like:

std::ostringstream buf;
buf.flags ( std::ios_base::right |std::ios_base::hex );
buf.width( 4 );
buf << group;
buf << "|";
buf << std::hex << element;
std::string key = buf.str();

But doesn''t seems to work...




#include< sstream>

#include< string>

#include< iomanip>


int main()

{

int group = 10,element = 20;

std :: ostringstream buf;

buf<< std :: right<< std :: setw(4)<< std :: setfill(''0'')<< std :: hex<<

group

<< " |"

<< std :: right<< std :: setw(4)<< std :: setfill(''0'')<< std :: hex<<

元素;

std :: string key = buf.str();


返回0;

}


而且,就在我们之间,你不必摆脱''sprintf''。整个

C标准库是C ++标准库的一部分。如果使用C功能更容易使用C / B
$ b,请务必这样做。


V



#include <sstream>
#include <string>
#include <iomanip>

int main ()
{
int group = 10, element = 20;
std::ostringstream buf;
buf << std::right << std::setw(4) << std::setfill(''0'') << std::hex <<
group
<< "|"
<< std::right << std::setw(4) << std::setfill(''0'') << std::hex <<
element;
std::string key = buf.str();

return 0;
}

And, just between us, you don''t have to get rid of ''sprintf''. The entire
C Standard Library is part of C++ Standard Library. If something''s easier
to do with a C function, by all means, do it.

V

< br>



" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

news:TYo9d.142704

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:TYo9d.142704


wV.42458@attbi_s54 ...
wV.42458@attbi_s54...
" Mathieu Malaterre" <毫安******* @ Mfree.fr>写道...
"Mathieu Malaterre" <ma*******@Mfree.fr> wrote...
我试图摆脱c ++代码中的sprintf,但我尝试了几种方式,并且无法弄清楚如何改变:

uint16_t group,uint16_t element;
sprintf(buffer,"%04x |%04x",group,element);

到目前为止,我有类似的东西:

std :: ostringstream buf;
buf.flags(std :: ios_base :: right | std :: ios_base :: hex);
buf.width(4);
buf<< group;
buf<< " |" ;;
buf<< std :: hex<<元素;
std :: string key = buf.str();

但似乎没有用......
#include< sstream>
#include< string>
#include< iomanip>

int main()
{
int group = 10,element = 20;
std :: ostringstream buf;
buf<< std :: right<< std :: setw(4)<< std :: setfill(''0'')<< std :: hex<<

<< " |"
<< std :: right<< std :: setw(4)<< std :: setfill(''0'')<< std :: hex<<
元素;
std :: string key = buf.str();

返回0;
}
I am trying to get rid a of sprintf in a c++ code, but I tried in several
ways and couldn''t figure out how to change:

uint16_t group, uint16_t element;
sprintf(buffer, "%04x|%04x", group , element);

so far I have something like:

std::ostringstream buf;
buf.flags ( std::ios_base::right |std::ios_base::hex );
buf.width( 4 );
buf << group;
buf << "|";
buf << std::hex << element;
std::string key = buf.str();

But doesn''t seems to work...
#include <sstream>
#include <string>
#include <iomanip>

int main ()
{
int group = 10, element = 20;
std::ostringstream buf;
buf << std::right << std::setw(4) << std::setfill(''0'') << std::hex <<
group
<< "|"
<< std::right << std::setw(4) << std::setfill(''0'') << std::hex <<
element;
std::string key = buf.str();

return 0;
}




必须重复Setw,但setfill和hex不重复,右边是默认值。


buf<< std :: setfill(''0'')<< std :: hex<< std :: setw(4)<<组<< " |"

std :: setw(4)<<元素;

而且,就在我们之间,你不必摆脱''sprintf''。整个
C标准库是C ++标准库的一部分。如果用C函数更容易做什么,无论如何都要做。



Setw must be repeated but setfill and hex do not, and right is the default.

buf << std::setfill(''0'') << std::hex << std::setw(4) << group << "|"
std::setw(4) << element;

And, just between us, you don''t have to get rid of ''sprintf''. The entire
C Standard Library is part of C++ Standard Library. If something''s easier
to do with a C function, by all means, do it.




绝对。


john



Absolutely.

john


这篇关于流程上的帮助很小......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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