How can I programatically determine the maximum size in bytes of a character in a specific charset?

ε祈祈猫儿з 提交于 2020-01-05 04:08:41

问题


I am getting all supported charsets by using :

Object[] Charsets = Charset.availableCharsets().keySet().toArray();

I now need to iterate through each character that can be encoded in that charset. To do this I thought about using the maximum number of bytes for each encoding and going through Byte.MIN_VALUE to Byte.MAX_VALUE for each byte. That byte array I then pass it through the String constructor that accepts a byte[] array and a specific encoding.

However can't find any clues on how I can determine the maximum length in bytes of a character representation in a specific charset.

I tried using the space character (i.e. " ") to create a string in that encoding and using .getBytes("<specific charset>").length. However I believe this only works for fixed size charsets. There are charsets in which the encoding of a character can have a variable number of bytes.

This is not a commercial software so it does not need a pretty solution. I need to create a sort of visual map of each supported charset. Each character representation is written into an image. I am also not sure on how I should select a font that can properly display all characters of a charset.

Any thoughts?


回答1:


charset.newEncoder().maxBytesPerChar()


来源:https://stackoverflow.com/questions/22626975/how-can-i-programatically-determine-the-maximum-size-in-bytes-of-a-character-in

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