简体   繁体   English

从字节数组转换为字符串时,它会添加空格

[英]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. 我正在使用RabbitMq,我能够很好地将一个字符串添加到队列中。 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. 我已经看过使用.trim()和不同类型的编码的所有内容。 The string ALWAYS comes out with spaces, which makes it impossible to deserialize into json. 字符串ALWAYS带有空格,这使得无法反序列化为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. 看起来当邮件被编码时,它被编码为unicode。 I changed the encoding to UTF8 and the message came across perfect :) 我将编码更改为UTF8,消息传来完美:)

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

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