[C ++] converts the byte sequence to a hexadecimal string

xiaoxiao2021-03-05  24

[C ] converts the byte sequence to a hexadecimal string

Rayleahaan@hotmail.com

The first thing I thought is to use the formatting function of the output stream in C , and String Stream connects a string, which is what we want. Use the COPY algorithm to copy these bytes into the stream, which seems to be the most intuitive:

Std :: string bytes_to_hexstr (unsigned char * last) {std :: ostringstream OS; oss << std :: hex << std :: setw (2) << std :: setfill ('0') ; std :: copy (first, last, std :: ostream_iterator ); return oss.str ();

However, the result thus obtained is wrong, because the SETW this manipulator is only valid for the next output, so only conversion to the first byte is pair. Rewrive a custom Ostream_iterator too expensive, or write a custom type output is also cumbersome, or use a handwritten loop:

Std :: string bytes_to_hexstr (unsigned char * last) {std :: ostringstream OS; oss << std :: hex << std :: setfill ('0'); while (first

It should be noted that the unsigned char will be converted to INT output because hex is only valid for the output of the integer value.

Last Modified: Sunday, April 17th, 2005

HTML Conversion by

TEX2PAGE 2005-02-27

转载请注明原文地址:https://www.9cbs.com/read-35102.html

New Post(0)