This project is read-only.

How to Encode the result in UTF-8

Dec 28, 2010 at 7:23 AM

Currently this Library runs well, but it will display messy codes if the result isn't ascii code, Could any one make a tip on how to encode the result in UTF-8? Appreciate for your efforts.

Dec 28, 2010 at 8:10 AM

I've add a new extension method to fix the Encode issue. and here is the method:

public static string GetSshString(this IEnumerable<byte> data, Encoding encoding)
            return encoding.GetString(data.ToArray());

This method is in file: Extension.cs, and the old method is:

public static string GetSshString(this IEnumerable<byte> data)
            return new string((from b in data select (char)b).ToArray());

Dec 28, 2010 at 3:58 PM



Can you give me an example of where I need to use different encoding? I just want to test it, and.or to add it to my test cases.


But, I just checked in new SshClient class which has new CreateCommand method where you can specify encoding.


Internally I had all the infrastructure in place, it just wasnt exposed outside. Now it is.




Jan 7, 2011 at 2:11 PM

Hi, Oleg,

Sorry to reply so late, Sometimes there are some files in the server, and some of them may be with Unicode file name, but in the class: Renci.SshClient.Messages.Sftp.NameMessage, it only read file name by Asccii:

var fileName = this.ReadString();
var fullName = this.ReadString();

and in ReadString, the code is:
var result = this._data.Skip(this._readerIndex).Take((int)length).GetSshString();
 this._readerIndex += (int)length;

The file name will be messed if the filename encoded in UTF-8.

This issue won't occurs when we loadFile because we just get the Stream from server and can encode it by need.

Hope it can make the Library more better!



Jan 7, 2011 at 2:29 PM

ok, thanks,

unfortunalty I dont have a file system in UTF-8 that I can test against but will think about some kind of solution, where you could specify it as a parameter.