有没有一个函数可以返回一个字符的 ASCII 值? (C++)

发布于 2024-07-19 14:32:39 字数 85 浏览 5 评论 0原文

我需要一个返回字符的 ASCII 值的函数,包括空格、制表符、换行符等...

类似地,在十六进制、十进制和二进制数之间进行转换的函数是什么?

I need a function that returns the ASCII value of a character, including spaces, tabs, newlines, etc...

On a similar note, what is the function that converts between hexadecimal, decimal, and binary numbers?

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

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

发布评论

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

评论(5

别闹i 2024-07-26 14:32:39
char c;
int ascii = (int) c;
s2.data[j]=(char)count;

char整数,不需要转换函数。

也许您正在寻找将整数显示为字符串的函数 - 使用十六进制、二进制或十进制表示形式?

char c;
int ascii = (int) c;
s2.data[j]=(char)count;

A char is an integer, no need for conversion functions.

Maybe you are looking for functions that display integers as a string - using hex, binary or decimal representations?

戏剧牡丹亭 2024-07-26 14:32:39

您不需要函数来获取 ASCII 值 - 只需通过(隐式)转换转换为整数:

int x = 'A';  // x = 65
int y = '\t'; // x = 9

要将数字转换为十六进制或十进制,您可以使用 printf 的任何成员code> family:

char buffer[32];  // make sure this is big enough!
sprintf(buffer, "%d", 12345);  // decimal: buffer is assigned "12345"
sprintf(buffer, "%x", 12345);  // hex: buffer is assigned "3039"

没有内置函数转换为二进制; 你必须自己动手。

You don't need a function to get the ASCII value -- just convert to an integer by an (implicit) cast:

int x = 'A';  // x = 65
int y = '\t'; // x = 9

To convert a number to hexadecimal or decimal, you can use any of the members of the printf family:

char buffer[32];  // make sure this is big enough!
sprintf(buffer, "%d", 12345);  // decimal: buffer is assigned "12345"
sprintf(buffer, "%x", 12345);  // hex: buffer is assigned "3039"

There is no built-in function to convert to binary; you'll have to roll your own.

烟若柳尘 2024-07-26 14:32:39

如果你想获取代码中某个字符的 ASCII 值,只需将该字符放在引号中即可

char c = 'a';

If you want to get the ASCII value of a character in your code, just put the character in quotes

char c = 'a';
你曾走过我的故事 2024-07-26 14:32:39

您可能会将内部表示与输出混淆。 要查看字符具有什么值:

char c = 'A';
cout << c << " has code " << int(c) << endl;

类似地,十六进制值 - 所有数字都是十六进制数字,因此这只是输出问题:

int n = 42;
cout << n << " in hex is " << hex << n << endl;

输出语句中的“hex”是一个 C++ 操纵器。 有用于十六进制和十进制(dec)的操纵器,但不幸的是没有用于二进制的操纵器。

You may be confusing internal representation with output. To see what value a character has:

char c = 'A';
cout << c << " has code " << int(c) << endl;

Similarly fo hex valuwes - all numbers are hexadecimal numbers, so it's just a question of output:

int n = 42;
cout << n << " in hex is " << hex << n << endl;

The "hex" in the output statement is a C++ manipulator. There are manipulators for hex and decimal (dec), but unfortunately not for binary.

轻拂→两袖风尘 2024-07-26 14:32:39

就十六进制而言 二进制 - 这些只是整数的表示。 您可能想要的是 printf("%d",n) 和 printf("%x",n) 之类的东西 - 第一个打印十进制,第二个打印同一数字的十六进制版本。 明确你想做什么 -

As far as hex & binary - those are just representations of integers. What you probably want is something like printf("%d",n), and printf("%x",n) - the first prints the decimal, the second the hex version of the same number. Clarify what you are trying to do -

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