在 java 中将 int 转换为 char - 我必须使用 ASCII 吗?

发布于 2024-10-03 19:37:07 字数 377 浏览 3 评论 0原文

问:在 Java 中将 int 转换为 char 时,默认结果似乎是与该 int 值对应的 ASCII 字符。我的问题是,是否有某种方法可以指定在转换时使用不同的字符集?

(背景信息:我正在开发一个项目,在该项目中我读取一串二进制字符,将其转换为块,然后将块转换为十进制整数值,然后将其转换为字符。然后我需要能够通过反转该过程将生成的压缩字符“扩展”回二进制。 我已经能够做到这一点,但目前我只能将最多 6 个“位”压缩为单个字符,因为当我允许更大的数量时,该范围内的某些值似乎无法处理很好用 ASCII;它们变成方框或问号,当它们被转换回 int 时,它们的原始值没有被保留。如果我可以使用其他字符集,我想我可以避免这个问题并一次将二进制文件压缩 8 位,这是我的目标。)

我希望这是清楚的,并提前致谢!

Q: When casting an int to a char in Java, it seems that the default result is the ASCII character corresponding to that int value. My question is, is there some way to specify a different character set to be used when casting?

(Background info: I'm working on a project in which I read in a string of binary characters, convert it into chunks, and convert the chunks into their values in decimal, ints, which I then cast as chars. I then need to be able to "expand" the resulting compressed characters back to binary by reversing the process.
I have been able to do this, but currently I have only been able to compress up to 6 "bits" into a single character, because when I allow for larger amounts, there are some values in the range which do not seem to be handled well by ASCII; they become boxes or question marks and when they are cast back into an int, their original value has not been preserved. If I could use another character set, I imagine I could avoid this problem and compress the binary by 8 bits at a time, which is my goal.)

I hope this was clear, and thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浪菊怪哟 2024-10-10 19:37:08

我正在开展一个项目,其中我
读取一串二进制字符,
将其转换为块,然后转换
将块放入其值中
小数、整数,然后我将其转换为
字符。然后我需要能够
“扩展”由此产生的压缩
通过反转将字符恢复为二进制
过程。

你没有提到你为什么要这么做,而且(说实话)很难理解你想要描述的内容(一方面,我不明白为什么会出现这样的结果)字符将以任何方式“压缩”,

如果您只想将二进制数据表示为文本,则有很多 标准方法来实现这一点,但听起来你可能在追求别的东西?

I'm working on a project in which I
read in a string of binary characters,
convert it into chunks, and convert
the chunks into their values in
decimal, ints, which I then cast as
chars. I then need to be able to
"expand" the resulting compressed
characters back to binary by reversing
the process.

You don't mention why you are doing that, and (to be honest) it's a little hard to follow what you're trying to describe (for one thing, I don't see why the resulting characters would be "compressed" in any way.

If you just want to represent binary data as text, there are plenty of standard ways of accomplishing that. But it sounds like you may be after something else?

寂寞花火° 2024-10-10 19:37:07

您的问题与 ASCII 或字符集无关。

在 Java 中,char 只是一个 16 位整数。将 int(32 位整数)转换为 char 时,您唯一要做的就是保留 int 的 16 个最低有效位,并丢弃高 16 位。这称为缩小转换

参考文献:

Your problem has nothing to do with ASCII or character sets.

In Java, a char is just a 16-bit integer. When casting ints (which are 32-bit integers) to chars, the only thing you are doing is keeping the 16 least significant bits of the int, and discarding the upper 16 bits. This is called a narrowing conversion.

References:

三岁铭 2024-10-10 19:37:07

字符和整数之间的转换使用 Unicode 值,ASCII 是其中的子集。如果您正在处理二进制数据,则应避免使用字符和字符串,而应使用整数数组 - 请注意,Java 没有无符号 8 位整数。

The conversion between characters and integers uses the Unicode values, of which ASCII is a subset. If you are handling binary data you should avoid characters and strings and instead use an integer array - note that Java doesn't have unsigned 8-bit integers.

风透绣罗衣 2024-10-10 19:37:07

你所寻找的不是演员阵容,而是转换。

有一个 String 构造函数,它接受一个字节数组和一个字符集编码。这应该对你有帮助。

What you search for in not a cast, it's a conversion.

There is a String constructor that takes an array of byte and a charset encoding. This should help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文