0

I'm receiving data from a rabbit queue that I'm trying to convert to String. The message always starts with the SOH character and always ends with a VT character, followed by an ETX character. The data is received correctly and the byte array printed to the console is correct. The problem is that after converting to String, all the characters in the System.out.println before the VT and ETX are omitted. I first thought that the byte[] to String conversion might be done wrong by me, but I think there might be an issue with printing to the console that I'm missing.

Here's all the conversion approaches that I tried:

DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            System.out.println("received message");
            String message1 = new String(delivery.getBody(), StandardCharsets.US_ASCII);
            String message2 = new String(delivery.getBody(), StandardCharsets.UTF_8);
            String message3 = new String(delivery.getBody(), StandardCharsets.UTF_16);
            String message4 = new String(delivery.getBody(), StandardCharsets.UTF_16BE);
            String message5 = new String(delivery.getBody(), StandardCharsets.UTF_16LE);
            String message6 = new String(delivery.getBody(), StandardCharsets.ISO_8859_1);
            String message7 = new String(delivery.getBody());
            String message8 = Base64.getEncoder().encodeToString(delivery.getBody());
            String message9 = new String(Base64.getEncoder().encode(delivery.getBody()), StandardCharsets.UTF_8);
            System.out.println(Arrays.toString(delivery.getBody()) + " - " + delivery.getBody().length);
            System.out.println("1 " + message1 + " - " + message1.length() + " - ");
            System.out.println("2 " + message2 + " - " + message2.length() + " - ");
            System.out.println("3 " + message3 + " - " + message3.length() + " - ");
            System.out.println("4 " + message4 + " - " + message4.length() + " - ");
            System.out.println("5 " + message5 + " - " + message5.length() + " - ");
            System.out.println("6 " + message6 + " - " + message6.length() + " - ");
            System.out.println("7 " + message7 + " - " + message7.length() + " - ");
            System.out.println("8 " + message8 + " - " + message8.length() + " - ");
            System.out.println("9 " + message9 + " - " + message9.length() + " - ");
        };

The System.out.println for messages 1, 2, 6, 7 print only VT, ETX and what is after, but nothing of what is before. The others are not properly converted and print asian characters or other random chars.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Mr. ATC
  • 5
  • 4
  • How do you know this statement `The data is received correctly and the byte array printed to the console is correct. ` is valid. Did you debug the delivery.getBody() or? – JCompetence Dec 26 '21 at 11:22
  • It could be a `'\0'` terminating a string in C. Write all to a file, and check that. – Joop Eggen Dec 26 '21 at 15:23
  • @SMA yes, I took the byte array and decoded it using an online ASCII to text converter – Mr. ATC Dec 26 '21 at 18:46
  • @JoopEggen before the VT there is a carriage return character (ASCII character 13) could that be the issue, and does the `System.out.println` and does it reset the position to the beginning of the line, hence why nothing is seen before it in the console? – Mr. ATC Dec 26 '21 at 18:50
  • I am not working intensively with System.out, but that might be the case. – Joop Eggen Dec 26 '21 at 22:10

1 Answers1

0

Special characters

These are control characters

See also:

What is a vertical tab?

C0 and C1 control codes for message transmission

These control characters have not only special meaning (semantics) for printing but only for text transmission.

Maybe you need to put a special message-conversion component into place, when receiving data from a RabbitMQ text-message queue.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • the issue was that before the VT there is a carriage return character (ASCII character 13). I removed it from the string and it now prints correctly – Mr. ATC Dec 26 '21 at 21:37