简体   繁体   中英

When converting from byte array to string it is adding spaces

I am using RabbitMq and I am able to add a string to the queue just fine. When I look in the queue everything looks good, but when I bring that string off the queue it is adding spaces between each character. I have looked at everything from using .trim() and different types of encoding. The string ALWAYS comes out with spaces, which makes it impossible to deserialize into json.

Since it is sitting on the queue correct, I am just adding the part that gets the data and converts it into a simple string.

 using (var connection = connectionFactory.CreateConnection())
      using (var channel = connection.CreateModel())
      {

        channel.BasicQos(0, 1, false);

        var consumer = new EventingBasicConsumer(channel);
        channel.BasicConsume(QUEUE_NAME, false, consumer);

        consumer.Received += (model, ea) =>
        {
          var body = ea.Body;
          var message = System.Text.Encoding.UTF8.GetString(body, 0, body.Length);
          Console.WriteLine($" [x] Recieved {message}");
        };

        channel.BasicConsume(QUEUE_NAME, true, "brandon", false, false, null, consumer);
        Console.ReadLine();
      }

Looks like when the message was being encoded it was encoded as unicode. I changed the encoding to UTF8 and the message came across perfect :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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