有没有一个函数可以返回一个字符的 ASCII 值? (C++)
我需要一个返回字符的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
char是整数,不需要转换函数。
也许您正在寻找将整数显示为字符串的函数 - 使用十六进制、二进制或十进制表示形式?
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?
您不需要函数来获取 ASCII 值 - 只需通过(隐式)转换转换为整数:
要将数字转换为十六进制或十进制,您可以使用 printf 的任何成员code> family:
没有内置函数转换为二进制; 你必须自己动手。
You don't need a function to get the ASCII value -- just convert to an integer by an (implicit) cast:
To convert a number to hexadecimal or decimal, you can use any of the members of the
printf
family:There is no built-in function to convert to binary; you'll have to roll your own.
如果你想获取代码中某个字符的 ASCII 值,只需将该字符放在引号中即可
If you want to get the ASCII value of a character in your code, just put the character in quotes
您可能会将内部表示与输出混淆。 要查看字符具有什么值:
类似地,十六进制值 - 所有数字都是十六进制数字,因此这只是输出问题:
输出语句中的“hex”是一个 C++ 操纵器。 有用于十六进制和十进制(dec)的操纵器,但不幸的是没有用于二进制的操纵器。
You may be confusing internal representation with output. To see what value a character has:
Similarly fo hex valuwes - all numbers are hexadecimal numbers, so it's just a question of output:
The "hex" in the output statement is a C++ manipulator. There are manipulators for hex and decimal (dec), but unfortunately not for binary.
就十六进制而言 二进制 - 这些只是整数的表示。 您可能想要的是 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 -