简体   繁体   English

Java“ \\ u0001something”未返回预期结果

[英]Java “\u0001something” doesn't return expected results

I'm trying to put together a string that starts with the ascii "Start of Header" character (0x01). 我正在尝试将以ascii“标头开始”字符(0x01)开头的字符串放在一起。 I did this on my home computer simple enough with: 我在家用计算机上执行以下操作非常简单:

String out = "\u0001this";
System.out.println(out);

prints a square to the terminal.. Copy and paste to hex editor and I have my 0x01; 复制一个正方形到终端。复制并粘贴到十六进制编辑器,我有0x01;

Now at work.. I do the same thing but anything below 0x20 just prints 0x00 to the terminal. 现在在工作..我做同样的事情,但是0x20以下的任何东西只会在终端上打印0x00。 As soon as I do "\ " I get a space printed to output.. Any idea whats going on. 一旦执行“ \\ u0020”,我就会打印出一个空格以进行输出。.知道发生了什么。

可能想要检查您正在使用的文件编码,默认情况下JVM运行的字符集,等等。不同的环境可能会有不同的设置。

This is almost surely a function of your terminal emulator or other program displaying what is written to standard out. 几乎可以肯定,这是终端仿真器或其他程序的功能,该程序可以显示写入标准输出的内容。 This character is by definition non-printable, so what a terminal does to 'display' it likely varies. 根据定义,此字符是不可打印的,因此终端机“显示”该字符的方式可能会有所不同。 Some print a box, some a blank, some nothing. 有些打印一个盒子,有些打印空白,有些什么也没有。

If you are relying on how a non-printable character is printed, which isn't defined, maybe that is the issue? 如果您要依靠无法打印的字符的打印方式(未定义),也许是问题所在? what problem does it cause? 它会导致什么问题?

Can you write this string directly to a file and open that file in the hex editor? 您可以将此字符串直接写入文件并在十六进制编辑器中打开该文件吗? By writing the text to the console and then relying on cut-and-paste to move the bytes to a different application, there are quite a few intermediate steps that could be messing with your data. 通过将文本写入控制台,然后依靠剪切和粘贴将字节移动到其他应用程序,有很多中间步骤可能会弄乱您的数据。

I suspect that your code is actually working but you're unintentionally obscuring your own (correct) results. 我怀疑您的代码确实有效,但是您无意间掩盖了自己(正确)的结果。

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

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