Why 255 is the limit

戏子无情 提交于 2020-08-22 19:38:56

问题


I've seen lots of places say:

The maximum number of characters is 255.

where characters are ASCII. Is there a technical reason for that?

EDIT: I know ASCII is represented by 8 bits and so there're 256 different characters. The question is why do they specify the maximum NUMBER of characters (with duplicates) is 255.


回答1:


I assume the limit you're referring to is on the length of a string of ASCII characters.

The limit occurs due to an optimization technique where smaller strings are stored with the first byte holding the length of the string. Since a byte can only hold 256 different values, the maximum string length would be 255 since the first byte was reserved for storing the length.

Some older database systems and programming languages therefore had this restriction on their native string types.




回答2:


Extended ASCII is an 8-bit character set. (Original ASCII is 7-bit, but that's not relevant here.)

8 bit means that 2^8 different characters can be referenced.

2^8 equals 256, and as counting starts with 0, the maximum ASCII char code has the value 255.

Thus, the statement:

The maximum number of characters is 255.

is wrong, it should read:

The maximum number of characters is 256, the highest possible character code is 255.

To understand better how characters are mapped to the numbers from 0 to 255, see the 8-bit ASCII table.




回答3:


Is there a technical reason for that?

Yes there is. Early ASCII encoding standard is 7 bit log, which can represent 2^7 = 128 (0 .. 127) different character codes.

What you are talking about here is a variant of ASCII encoding developed later, which is 8 bit log and can hold 2^8 = 256 (0 .. 255) character codes.

See Wikipedia for more information on the same.




回答4:


the limit is 255 because 9+36+84+126 = 255. the 256th character (which is really the first character) is zero.

using the combinatoric formula Ck(n) = n/k = n!/(k!(n-k)!) to find the number of non-repeating combinations for 1,2,3,4,5,6,7,8 digits you get this:

of digits: 1 2 3 4 5 6 7 8

of combinations: 9 36 84 126 126 84 36 9

it is unnecessary to include 5-8 digits since it's a symmetric group of M. in other words, a 4 element generator is a group operation for an octet and its group action has 255 permutations.

interestingly, it only requires 3 digits to "count" to 1000 (after 789 the rest of the numbers are repetitions of previous combinations).




回答5:


Counting starts at 0 and ends at 255 which means there are 255 count starting from 1 to 255 and after including 0, it becomes 256.

A program to print all 256 characters

#include<stdio.h>

int main() {
  for (int i = 0; i <= 255; i++) {
    printf("%d = %c\n", i, i);
  }
  return 0;
}


来源:https://stackoverflow.com/questions/26197288/why-255-is-the-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!