字节数组到字符串给出“???”

发布于 2024-11-16 07:24:56 字数 1469 浏览 9 评论 0原文

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

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

发布评论

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

评论(4

隱形的亼 2024-11-23 07:24:56

注意你的零。您的评论说 1000,getMessage 使用 10000,hideMessage 使用 100000

(重新发布为答案,因为显然这就是错误的全部)

Pay attention to your zeroes. Your comment says 1000, getMessage uses 10000, and hideMessage uses 100000

(reposted as answer since apparently that's all that was wrong)

來不及說愛妳 2024-11-23 07:24:56

您不能简单地从任意字节创建字符串 - 字节必须是您正在使用的编码中的字符的编码(在您的情况下,是默认编码)。如果您使用不映射到字符的字节,它们将被映射到 '?'。另一个方向也是如此:如果字符串中的字符未映射到字节,则 getBytes() 方法会将它们映射到 (byte)'?'.我认为其中之一或两者都发生在这里。

如果您使用 JPG 或类似的有损图像格式,它会在保存过程中更改图像的字节。

You can't simply create a string from arbitrary bytes - the bytes must be encodings of characters in the encoding you are using (in your case, the default encoding). If you use bytes that don't map to a character, they will be mapped to '?'. The same is true in the other direction: If you have a string with characters which do not map to bytes, the getBytes() method will map them to (byte)'?'. I think one or both of this happened here.

If you are using JPG or a similar lossy image format, it will change the bytes of your image during saving.

℉絮湮 2024-11-23 07:24:56

如果计划实际更改部分位图字节,则需要将图像导出为 png,因为它是无损的。 Jpeg 可能会稍微改变字节,这对于图像来说不是问题,但对于文本来说显然很重要。

其次,如果您要选择 100,000 作为插入消息的固定位置,则应该将其设置为常量,以使其更容易且不易出错。说到这里,您当前的固定偏移量偏离了“0”、10,000 和 100,000。

If the plan is to actually change part of your bitmap bytes, you'd need to export the image as png, as its lossless. Jpeg would probably change the bytes slightly, which isn't a problem for an image, but for text its obviously critical.

Second, if you're going to pick 100,000 as a fixed position to insert the message, you should set that up as a constant to make it easier, and less error prone. Speaking of which, your current fixed offsets are off by a '0', 10,000 and 100,000.

冬天旳寂寞 2024-11-23 07:24:56

但是您应该编辑原始文件,而是 BufferedImage 的实例,然后使用 ImageIO 将其重写回文件。

But you should edit the raw file, but an instance of BufferedImage, then rewrite it back to a file with ImageIO.

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