[Format] Formatting some string chars not encoding to hex

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Format] Formatting some string chars not encoding to hex

Boost - Users mailing list
Hello,

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

Although I could possibly trap for this as being the case, I do not
think that I should.

Thoughts? Format authors?

Best regards,

Michael Powell
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Format] Formatting some string chars not encoding to hex

Boost - Users mailing list
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 mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Format] Formatting some string chars not encoding to hex

Boost - Users mailing list
On Tue, Jan 22, 2019 at 6:33 PM Gavin Lambert via Boost-users
<[hidden email]> 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
> [hidden email]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users