如何输出 unsigned/signed char 或 类型为带有 << 的整数在 C++

发布于 2024-09-29 06:43:09 字数 812 浏览 5 评论 0 原文

背景:

我有模板流运算符(例如 operator << (ostream &, std::vector ))(输出可能是某些 8 位的容器元素整数类型(例如,unsigned charint_least8_t 等)

问题:

默认情况下,这些类型输出为 char (ASCII)。 我只使用 char (或 wchar_t 或其他)作为 ASCII 变量,从不使用无符号/有符号类型。 即使调用者不知道类型,如何使这些其他 8 位类型始终输出为 signed int / unsigned int (数字)?

第一次尝试:

我尝试过(使用 GCC)例如定义 operator << (ostream &, unsigned char) 其中包含强制转换(即 stream << static_cast (value)。这适用于 unsigned char code> 值,但随后 uint8_t 仍然以 char 形式输出。

相同的基础类型(即 unsigned/signed char 不能用于)。重载,因此我无法定义例如 operator << (ostream &, int_fast8_t) 的重载。

Background:

I have template stream operators (e.g. operator << (ostream &, std::vector <T>)) (that output container elements that may possibly be of some 8-bit integer type, (e.g. unsigned char, int_least8_t, et cetera).

Problem:

Default is that these types are output as char (ASCII).
I only used char (or wchar_t or whatever) for ASCII variables, never unsigned/signed types.
How do I get these other 8-bit types to always be output as signed int / unsigned int (numbers) instead, even when the caller doesn't know the type?

First tries:

I have tried (with GCC) for example defining operator << (ostream &, unsigned char) with a cast in it (i.e. stream << static_cast <int> (value). That works for unsigned char values, but then uint8_t still gets output as a char.

The same underlying type (i.e. unsigned/signed char can not be used in overloads, so I can't define an overload of for example operator << (ostream &, int_fast8_t).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十二 2024-10-06 06:43:09

您将变量中保存的实际数据与您选择用于打印它的任何表示形式混淆。

这样想:charsintsdoubleslongs,无论如何,它们都只是块供您存储数字的内存。字符是 0 到 255(或 -128 到 127)之间的数字——您可以选择将其表示为 ASCII 字符、数字或天空中的星星OpenGL的帮助。

如果您想查看字符“a”后面的数字,只需指示您的程序将该内存块(对您而言包含“a”)视为数字。使用强制转换。这里:

http://www.cplusplus.com/doc/tutorial/typecasting/

看看是否有帮助!

You're confusing the actual data held in a variable, with whatever representation you choose for printing it.

Think of it this way: chars, ints, doubles, longs, whatevers, they're all just chunks of memory for you to store numbers in. A char is a number between 0 and 255 (or -128 and 127) -- you can choose to represent it as an ASCII character, or as a number, or as stars in the sky with the aid of OpenGL.

If you want to see the number behind the character 'a', just instruct your program to treat that chunk of memory (that for you contains an 'a') as a number. Use casts. Here:

http://www.cplusplus.com/doc/tutorial/typecasting/

See if that helps!

晨敛清荷 2024-10-06 06:43:09

我想到的一种方法是使用类型特征来定义每种类型的输出类型。您必须手动为每种类型声明这一点。这些特征可以定义为一个模板结构,专门用于具有与数据类型本身不同的输出类型的每个数据类型:

template< T >
struct output_trait {
    typedef const T & output_type;
}

在您的运算符中,您可以编写:

std::cout << static_cast< output_trait< T >::output_type >( variable ) << std::endl;

默认情况下,这不会进行强制转换,但对于以下类型: output_trait 是专门的,它将进行强制转换:

template<>
struct output_trait< unsigned char > {
    typedef unsigned int output_type;
}

One way that comes to mind is using type traits to define the output type for each type. You would have to declare that for every type by hand. The traits could be defined as a template struct that is specialized for every data-type that has a different output-type than the data-type itself:

template< T >
struct output_trait {
    typedef const T & output_type;
}

In your operator you write:

std::cout << static_cast< output_trait< T >::output_type >( variable ) << std::endl;

This will do no cast by default, but for types for which output_trait is specialized it will do a cast:

template<>
struct output_trait< unsigned char > {
    typedef unsigned int output_type;
}
夜灵血窟げ 2024-10-06 06:43:09

你可以简单地投射它:

#include<iostream>

int main()
{
 uint8_t blah = 65;
 std::cout << static_cast<int>(blah) << "\n";
 return 0;
}

65

You can simply cast it:

#include<iostream>

int main()
{
 uint8_t blah = 65;
 std::cout << static_cast<int>(blah) << "\n";
 return 0;
}

65

贪了杯 2024-10-06 06:43:09

如果我理解正确的话..像这样输出它:

std::cout << ( unsigned int )char << '\n';

或者更多c ++风格 - 使用static_cast,例如:

int main()
{
    char a = 'a';
    char b = 97;
    std::cout << static_cast< unsigned int >( a ) << '\n';
    std::cout << static_cast< unsigned int >( b ) << '\n';
    return 0;
}

两个 std::cout 将打印相同的:第一个 - < 的 ASCII 代码code>'a':97,第二个 - 只是值 97,存储在 b 中。 ab 完全相同。

If I have understood you right.. output it like this:

std::cout << ( unsigned int )char << '\n';

Or more c++ style - use static_cast, for example:

int main()
{
    char a = 'a';
    char b = 97;
    std::cout << static_cast< unsigned int >( a ) << '\n';
    std::cout << static_cast< unsigned int >( b ) << '\n';
    return 0;
}

both std::cout will print the same: the first one - the ASCII code of 'a': 97, the second one - just the value 97, stored in b. Both, a and b, are absolutely the same.

预谋 2024-10-06 06:43:09

您可以在输出它们之前投射它们:

std::cout << (unsigned int) container[index];

You can cast them before you output them:

std::cout << (unsigned int) container[index];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文