开发者

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

开发者 https://www.devze.com 2022-12-21 07:25 出处:网络
Using standard C++ I/O (such as 开发者_开发知识库std::cout), is it possible to \"print\" the value of an array (however long) into a string?

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

精彩评论

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