Boost logo

Boost Users :

Subject: Re: [Boost-users] [Format] Formatting some string chars not encoding to hex
From: Michael Powell (mwpowellhtx_at_[hidden])
Date: 2019-01-22 23:55:30


On Tue, Jan 22, 2019 at 6:33 PM Gavin Lambert via Boost-users
<boost-users_at_[hidden]> wrote:
>
> 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.

Thanks, I arrived at this conclusion demonstrated within my code base.

> 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.

Good advice, I appreciate the feedback.

> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users


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