Boost logo

Boost Users :

Subject: Re: [Boost-users] [Format] Formatting some string chars not encoding to hex
From: Gavin Lambert (boost_at_[hidden])
Date: 2019-01-22 23:33:27


On 23/01/2019 11:35, Michael Powell wrote:
> I've got some characters that are not formatting to hex properly. They
> are not being interpreted correctly by the strongly typed Format.
>
> For instance, consider the format string:
>
> const char x = '$'; // also, '_', for instance
> const auto s = (boost::format("\\x%1$02x") % x).str();
>
> I would expect this to encode to: "\\x24", however, instead this
> encodes to "\\x0$".
>
> I would expect '_' to encode: "\\x5f", however, this encodes to "\\x0_".
>
> In a few words, the encoding is "seeing" the char as a literal char
> instead of as an "integer".

Boost.Format largely ignores the type character in printf-style format
strings in favour of using the actual C++ type of the parameter instead.
  It's only there to indicate the end of an individual format parameter.
  Indeed, the "newer" format styles don't use a type character at all.

If you want to format these characters as integers, then you need to
cast it to an integer type first.

Bear in mind that a naive conversion may still not have the results
you're expecting depending on the character encoding being stored,
especially if this is different from the one assumed by the eventual
consumer of this output.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net