简体   繁体   English

我在用Java创建16位字符时遇到问题

[英]I am having trouble creating a 16bit char in java

How can I create a variable character that can hold a four byte value? 如何创建可以容纳四个字节值的可变字符?

I am trying to write an program to encrypt messages in java, for fun. 我正在尝试编写一个程序来加密Java中的消息,以达到娱乐目的。 I figured out how to use RSA, and managed to write a program that will encrypt a message and save it to a .txt file. 我弄清楚了如何使用RSA,并设法编写了一个对消息进行加密并将其保存到.txt文件的程序。 For example if "Quiet" is entered the outcome will be "041891090280". 例如,如果输入“安静”,则结果将为“ 041891090280”。 I wrote my code so that the number would always have length that is a multiple of six. 我编写了代码,以使数字的长度始终是六的倍数。 So I thought that I could convert the numbers into a hash code. 所以我认为我可以将数字转换为哈希码。 The first three letters are "041" so I could convert that into ")". 前三个字母为“ 041”,因此我可以将其转换为“)”。

However I am having trouble created a char with a number greater than 255. I have looked around online and found a few examples, but I can't figure out how to implement them. 但是,我在创建一个大于255的char时遇到了麻烦。我在网上环顾四周,发现了一些示例,但是我不知道如何实现它们。 I created a new method just to test them. 我创建了一种新方法来测试它们。

int a = 256;
char b = (char) a;
char c = 0xD836;
char[] cc = Character.toChars(0x1D50A);
System.out.println(b);
System.out.println(c); 
System.out.println(cc);

The program outputs 程序输出

?

?

?

I am only getting two bytes. 我只有两个字节。 I read that Java uses Unicode which should go up to 65535 which is four bytes. 我读到Java使用Unicode,它最多可以增加到65535,即四个字节。 I am using eclipse if that makes a difference. 如果这有所作为,我正在使用eclipse。

I apologize for the noob question. 对于菜鸟问题​​,我深表歉意。 And thanks in advance. 并预先感谢。

edit I am sorry, I think I gave too much information and ended up being confusion. 编辑对不起,我想我提供了太多信息,最终造成混乱。 What I want to do is store a string of numbers as a string of unicode characters. 我想做的是将一串数字存储为一串unicode字符。 the only way I know how to do that is to break up the number string small enough to fit it into a character. 我知道怎么做的唯一方法是将数字字符串分解得足够小以使其适合字符。 then add the characters one by one to a new string. 然后将字符一个接一个地添加到新字符串中。 But I don't know how to add a variable unicode character to a string. 但是我不知道如何在字符串中添加可变unicode字符。

All chars are 16-bit already. 所有字符都是16位的。 0 to 65535 only need 16-bit and 2^16 = 65536. 0至65535仅需要16位,而2 ^ 16 = 65536。

Note: not all characters are valid and in particular, 0xD800 to 0xDFFF are used for encoding code points (characters beyond 65536) 注意:并非所有字符都是有效的,尤其是0xD800至0xDFFF用于编码代码点(65536以后的字符)

If you want to be able to store all possible 16-bit values I suggest you use short instead. 如果您希望能够存储所有可能的16位值,建议您使用short代替。 You can store the same values but it may be less confusing to use. 您可以存储相同的值,但使用起来可能会更容易混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM