title: Convert to string to byte array in c# and form byte array to string in java
body: <p>I have client server setup, Client is in C# and server is in Java.
  Now I am encoding a string into a byte array and sending it to a Java server, but when Java decodes it receives a <code>String</code> with a space after each character:</p>
  
  <pre><code>Sending PLAIN  &gt;&gt;&gt;&gt;&gt; Received P L A I N
  </code></pre>
  
  <p>C# code (sender):</p>
  
  <pre class="lang-cs prettyprint-override"><code>static byte[] GetBytes(string str) {
      byte[] bytes = Encoding.UTF8.GetBytes(str);
      // System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
      return bytes;
  }
  </code></pre>
  
  <p>Java code (receiver):</p>
  
  <pre class="lang-java prettyprint-override"><code>protected SaslResponse receiveSaslMessage() throws TTransportException {
       underlyingTransport.readAll(messageHeader, 0, messageHeader.length);
       byte statusByte = messageHeader[0];
      // Bytes for C# code received here
      byte[] payload = new byte[EncodingUtils.decodeBigEndian(messageHeader, STATUS_BYTES)];
      underlyingTransport.readAll(payload, 0, payload.length);
  
      NegotiationStatus status = NegotiationStatus.byValue(statusByte);
      if (status == null) {
          sendAndThrowMessage(NegotiationStatus.ERROR, "Invalid status " + statusByte);
      } else if (status == NegotiationStatus.BAD || status == NegotiationStatus.ERROR) {
          try {
              String remoteMessage = new String(payload, "UTF-8");
              throw new TTransportException("Peer indicated failure: " + remoteMessage);
          } catch (UnsupportedEncodingException e) {
              throw new TTransportException(e);
          }
      }
  
      if (LOGGER.isDebugEnabled())
          LOGGER.debug(getRole() + ": Received message with status {} and payload length {}",
                 status, payload.length);
      return new SaslResponse(status, payload);
  }
  
  protected void handleSaslStartMessage() throws TTransportException, SaslException {
      // call upper method
      SaslResponse message = receiveSaslMessage();
  
      LOGGER.debug("Received start message with status {}", message.status);
      if (message.status != NegotiationStatus.START) {
          sendAndThrowMessage(NegotiationStatus.ERROR, "Expecting START status, received " + message.status);
      }
  
      // Bytes converted to string here - Received P L A I N
      String mechanismName = new String(message.payload);
  
      TSaslServerDefinition serverDefinition = serverDefinitionMap.get(mechanismName);
      LOGGER.debug("Received mechanism name '{}'", mechanismName);
  
      if (serverDefinition == null) {
          sendAndThrowMessage(NegotiationStatus.BAD, "Unsupported mechanism type " + mechanismName);
      }
      SaslServer saslServer = Sasl.createSaslServer(serverDefinition.mechanism,
      serverDefinition.protocol, serverDefinition.serverName, serverDefinition.props,
      serverDefinition.cbh);
      setSaslServer(saslServer);
  }
  </code></pre>