每个字符的字节

8wtpewkr  于 2021-06-15  发布在  Mysql
关注(0)|答案(3)|浏览(318)

你好抱歉,如果这篇文章是愚蠢的,但我需要知道,如果我在java中得到这样的字符串。

final String string = "myNastyString";
for(int i=0;i<string.length();i++){
    System.out.println((int)string.charAt(i));
}

我想知道char的int值或者char本身在mysql中使用了多少字节。
请善待我,非常感谢。是的,我做了一些研究。
像这样的。

51 3 would use X bytes in a mysqlTable{X}
32   would use X bytes in a mysqlTable{X}
67 C would use X bytes in a mysqlTable{X}
100 d would use X bytes in a mysqlTable{X}
115 s would use X bytes in a mysqlTable{X}
32   would use X bytes in a mysqlTable{X}
70 F would use X bytes in a mysqlTable{X}
114 r would use X bytes in a mysqlTable{X}
233 é would use X bytes in a mysqlTable{X}
65533 � would use X bytes in a mysqlTable{X}
68 D would use X bytes in a mysqlTable{X}
233 é would use X bytes in a mysqlTable{X}
65533 � would use X bytes in a mysqlTable{X}
82 R would use X bytes in a mysqlTable{X}
105 i would use X bytes in a mysqlTable{X}
99 c would use X bytes in a mysqlTable{X}
32   would use X bytes in a mysqlTable{X}
67 C would use X bytes in a mysqlTable{X}
104 h would use X bytes in a mysqlTable{X}
111 o would use X bytes in a mysqlTable{X}
112 p would use X bytes in a mysqlTable{X}
105 i would use X bytes in a mysqlTable{X}
110 n would use X bytes in a mysqlTable{X}
32   would use X bytes in a mysqlTable{X}
40 ( would use X bytes in a mysqlTable{X}
77 M would use X bytes in a mysqlTable{X}
97 a would use X bytes in a mysqlTable{X}
115 s would use X bytes in a mysqlTable{X}
116 t would use X bytes in a mysqlTable{X}
101 e would use X bytes in a mysqlTable{X}
114 r would use X bytes in a mysqlTable{X}
112 p would use X bytes in a mysqlTable{X}
105 i would use X bytes in a mysqlTable{X}
101 e would use X bytes in a mysqlTable{X}
99 c would use X bytes in a mysqlTable{X}
101 e would use X bytes in a mysqlTable{X}
115 s would use X bytes in a mysqlTable{X}
41 ) would use X bytes in a mysqlTable{X}

我的意思是每个值aka char在mysql中会使用多少字节,因为我使用的是拉丁文-瑞典语-ci排序规则,我需要进行验证,以防任何字符都不适合我的表
我想知道mystring中的char何时会消耗mysql表中超过1个字节的数据

pcww981p

pcww981p1#

从mysql引用:
https://dev.mysql.com/doc/refman/8.0/en/char.html
如果声明为“char”,则不管字符串有多少个字符,都使用相同的数字。而是使用“varchar”,这取决于指定的长度。
例如字符串:
char(13)中的“mynastystring”使用13字节
char(20)中的“mynastystring”使用20字节
varchar(13)中的“mynastystring”使用13字节
varchar(20)中的“mynastystring”使用13字节

2uluyalo

2uluyalo2#

db中每个字符的字节数并不取决于字符串在java或任何其他写入db的客户机中的存储方式。它取决于为数据库、表或特定列定义的字符集。一旦db接收到字符串,它就被转换成db/table/column定义的字符集。所以我来回答你的问题:字符集latin1总是每个字符有1个字节。顺便说一句,latin1更为人所知的是iso-8859-1,它绝对是一个非常标准的字符集,而且绝对受java支持。请参见此处有关字符集的信息。
我还建议切换到一个unicode字符集,它支持所有语言中的所有字符。常见的是utf-8(可能为每个字符分配不同的字节数(如果我没记错的话是1-3)或utf-16(总是每个字符分配2个字节)。
在java方面,为了分析字符串并诊断一些与字符集相关的问题,我建议使用具有实用程序类stringunicodeencoderdecoder的开源库mgntutils(由我编写)。该类提供了将任何字符串转换为unicode序列的静态方法,反之亦然。非常简单实用。要转换字符串,只需执行以下操作:

String codes = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(myString);

例如,字符串“hello world”将转换为
“\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064”
它适用于任何语言。下面是一个链接,它解释了关于库的所有细节:mgntutils。查找副标题“字符串unicode转换器”。本文提供了maven central的链接,您可以在这里获得工件,github可以在这里获得项目本身。该库附带了编写良好的javadoc和源代码。

raogr8fs

raogr8fs3#

我的意思是每个值aka char在mysql中会使用多少字节,因为我使用的是拉丁文-瑞典语-ci排序规则,我需要进行验证,以防任何字符都不适合我的表
mysql“latin1”是windows-1252的一个修改版本,这意味着它包含了windows-1252中的所有字符,并且还为windows-1252中未定义的几个字符定义了Map:
对于cp1252中的“未定义”条目,mysql将0x81转换为unicode 0x0081,将0x8d转换为0x008d,将0x8f转换为0x008f,将0x90转换为0x0090,将0x9d转换为0x009d。
我不希望java直接支持“mysql拉丁1”,因为它不是一个标准字符集。所以对于每个字符,你可以检查它是否
范围为u+0000-u+007f(ascii)
在u+00a0-u+00ff范围内(其中cp1252和iso拉丁语-1重合)
其中一个字符windows-1252Map到范围0x80-0x9f(参见wikipedia页面)
u+0081、u+008d、u+008f、u+0090或u+009d

相关问题