【问题标题】:How? from an Int can I get the Upper Char value?如何?从 Int 我可以得到 Upper Char 值吗?
【发布时间】:2018-03-06 15:18:58
【问题描述】:

我有一个有点困惑的 java 问题。 一直在搜索和测试一些东西,但没有任何成功。

我有这个 FilterWriter 类,它必须将我的字符串、char 和 int 更改为它的上限值。

 public class UpperCaseFilterWriter extends FilterWriter {

 public UpperCaseFilterWriter(Writer wrappedWriter) {
    super(wrappedWriter);
 }

 @Override
 public void write(String str, int off, int len) throws IOException {
   while(len-- > 0){
     write(str.charAt(off++));
   }
 }

 @Override
 public void write(char[] cbuf, int off, int len) throws IOException {
   while(len-- > 0){
     write(cbuf[off++]);
   }
 }

 @Override
 public void write(int c) throws IOException {
   //TODO Change int c to upper int c
   out.write(c);
 }
}

现在我的write(String) method 和我的write(char[]) method 正在打电话给我的write(int c) method

但现在我需要

  1. 接受我的 int c
  2. 将其转换为字符

  3. 将字符(如果是字母)更改为其上限值

  4. 再次将其转换为 Int 以将其传递给我的方法 write(int c)

它采用 UTF-8 编码,所以我有“é”或“ä”类型的字母。

我已经尝试过以不同的方式执行 int -> char -> upper char -> int 但没有成功。

如果有人能帮我解决这个问题或给我信息在哪里看,那就太好了。也许我需要用另一种方式来做,或者也许有一种方法可以用一些字节编码/解码来做......我不知道。

谢谢!!

【问题讨论】:

  • out.write(Character.toUpperCase((char) c)); ?
  • 不,不是 UTF-8 编码。 charString 和在这种情况下,int 用于 UTF-16 代码单元。这两种编码都适用于 Unicode 字符集。所以,你有 Unicode 字符。也许您的意思是最终的输出编码是 UTF-8。这与论点没有任何关系。
  • 对于大写,您会应用哪个语言环境?

标签: java type-conversion decorator


【解决方案1】:

Character.toUpperCase是你需要的:它可以直接转换码位:

 @Override
 public void write(int c) throws IOException {
   //TODO Change int c to upper int c
   out.write(Character.toUpperCase(c));
 }

它可以正确处理非 ascii 字符,例如成功地将 é 转换为 É

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 2013-11-04
    • 2017-06-25
    • 2013-01-10
    • 2021-06-29
    相关资源
    最近更新 更多