Adding non printable chars to a string in Java?

前端 未结 3 516
傲寒
傲寒 2020-12-19 09:59

I need to add some non printable chars to a string in java so it can be sent down a tcp pipe. the chars mean something to the protocol I am using (record separator and end o

相关标签:
3条回答
  • 2020-12-19 10:21

    If you wish, you can write these as Unicode literals (in chars or Strings):

    final String endOfText = "\u0003";
    
    • Unicode charts
    • Java Language Specification Lexical Structure chapter

    I am assuming that you don't literally want ASCII for a byte-based protocol (the chars are still 16 bits).

    0 讨论(0)
  • 2020-12-19 10:29
    public final class ProtocolConstants {
        public final char RECORD_SEPARATOR = 0x1e;
        public final char END_OF_TEXT = 0x03;
    
        private ProtocolConstants() {}
    }
    

    something like that?

    0 讨论(0)
  • 2020-12-19 10:30

    You can add any chars to a Java String. But a String is probably not what you want if you just want to transmit binary data. Consider using a byte[] or some other byte-oriented interface.

    0 讨论(0)
提交回复
热议问题