了解一个字符到ASCII的转换函数

Understanding a Char to ASCII conversion function

本文关键字:ASCII 转换 函数 字符 一个 了解      更新时间:2023-10-16

如何在c++中使用这个函数?它应该返回索引为ip的字符串p中字符的ASCII值。

#include <cstring>
int ascVal(size_t ip, const char* p)
{
    // Return the ASCII value of the char
    if(!p || ip > strlen(p))
        return -1;
    else
        return p[ip];
}

我想学习如何使用这个函数。谢谢你。

你似乎误解了"字符"answers"字符缓冲区"。

字符缓冲区是一个字符数组。"ascval"函数中的"size_t"只是缓冲区中的字符数,从0开始索引。

为了你的理解,我调整了你的代码。它工作(未优化)
#include <iostream>
#include <cstring>

int ascVal(size_t ip, const char* p)
{
    // Return the ASCII value of the char
    if(!p || ip > strlen(p))
        return -1;
    else
        return p[ip];
}

int main()
{
    std::cout << ascVal(0,"a") << std::endl;
    char *letter = new char;
    *letter = 'a';
    size_t asciiSize = 0;
    int letterValue = letter[asciiSize];
    std::cout << letterValue << std::endl;

}

首先浏览一下这个函数,因为为了欣赏它,您应该了解它所做的所有工作。

int ascVal(size_t ip, const char* p)
{
    // Return the ASCII value of the char
    if(!p // if p is NULL there is not string ergo nothing vcan be done with it
       || // boolean OR means if either condition is met
       ip > strlen(p)) //the index of the requested character is NOT inside the string  
        return -1; // we can do nothing with input this bad. Return an error
    else
        return p[ip]; // get the ipth character in the string and return the character as 
                      // an integer. This makes the character look like a number. It's 
                      // really been a number the whole time but characters are treated 
                      // and displayed to the user differently than an int
}

现在OP在做什么:

char *letter = new char; // dynamically allocating a block of memory exactly one 
                         // character long
*letter = 'a'; // assigning the letter a to that one character
size_t asciiSize = 256;
int letterValue = letter[asciiSize]; // get the asciiSizeth (256th) character in the block 
                                     // of memory. Unfortunately there is no 256th
                                     // character in that block of memory. This is bad.
cout << letterValue << endl; // print value of 256th character in bloc of one character.

与上面的函数不同,OP没有验证letter指向任何东西。这是一个有争议的问题,因为OP只是在上面几行设置了它,并且非常清楚letter是一个有效的指针。OP也不保证asciiSize指定的数组位置在数组内。

我认为这就是OP的概念问题所在。OP似乎认为,因为ASCII中有256个值,所以他们可以只要求值256。

问题1:原点0

几乎所有的编程都是从0开始计数的。如果你有256个可能的值,这些值从0到255。256太大了,会发生一些奇怪的事情。通常情况下,256会溢出并变为0,但我不会在所有情况下都指望这一点。

问题2:ASCII是7位

ASCII值只有2的7次方,即128。因为我们通常将ASCII值放在一个8位的数字中,所以人们找到了使用未使用的128值的方法,但这通常是由显示值的本地化或代码页设置定义的。不要指望不先检查就能得到你想要的。

问题3:数组索引

letter[asciiSize]是对letter之后的asciiSize数组元素的请求。教科书是查找数组和索引细节的更好的地方,但这里重要的是,您不应该请求您没有的内存,也不应该请求超出使用范围的数据。这把我们带到了

半问题4:null终止

*letter = 'a'只是一个指向恰好是'a'的单个字符的指针。这不是字符串。要成为一个字符串,您需要有某种大小信息或终止符,以便您知道何时停止读取字符串。在C语言中,终止符是字符值0。它从不在对话中使用,所以它是表示文本字符串结束的完美标记。不要尝试将其用于二进制信息。二进制有非常不同的规则。通常二进制信息的确切大小是提前知道的,或者用一个不可能出现在二进制流中的序列来标记结束,即结束符。在二进制中,0是很常见的,所以你通常必须选择其他的值。

这里我们知道letter正好指向一个字符,因为OP正好在那里放了一个字符。指针没有线索。这完全取决于程序员,如果程序员分配了一个字符,然后要求第100个字符,那是他们的问题。

letter转换成字符串

char *letter = new char[2];
*letter = 'a'; // same as letter[0] = 'a'
letter[1] = ''; // same as *(letter + 1) = ''

如果需要,现在可以使用strlen来确定字符串的长度。

把这些放在一起,如果你想要一个给定值的字符,比如42,你所要做的就是

char val = 42;

显示val将得到'*',即赋值给42的符号。为什么是'*' ?没人知道,道格拉斯·亚当斯为了找到答案而死。道格拉斯!我们向你致敬!

这里不需要数组索引。您所需要的只是一个ASCII表的副本。

如果你真的想要字符串的第256个字符,你必须首先使字符串足够大,然后填充到第256个字符或更大。不要因为程序没有崩溃就认为没有第256个字符。首先像strlen的原始函数一样进行测试。测试并祈祷提供字符串的人正确地终止了它。