开发者

Printing the hex value of an array to a string using C++

Using standard C++ I/O (such as 开发者_开发知识库std::cout), is it possible to "print" the value of an array (however long) into a string?

For example, say I have the following array:

unsigned long C = {0x497fecf2, 0xfa989ea3, 0xd594974e};

I'd like to be able to print those values into a string, and then remove the "0x" from them. From another SO question, I've found how to print hex values with cout.

Is what I'm asking possible the way I've described?

Would it be better to just go back to an old comp sci assignment for base conversions, and convert the decimal value into hex, using a lookup table to add the appropriate next hexit to the string?


Create a std::ostringstream, and print them to it just like you could to cout. Retrieve the string with the contents with the stringstream's str() member.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜