Community discussions

MikroTik App
 
User avatar
Deantwo
Member
Member
Topic Author
Posts: 332
Joined: Tue Sep 30, 2014 4:07 pm

API Encoding

Mon Mar 21, 2016 4:14 pm

I have been working with the API using C# for the last few months with great success.

I am however Danish and we have a few non-ASCII characters in our alphabet (see), and I am having trouble sending these to the router via the API.
The characters are:
  • æ
  • ø
  • å
  • Æ
  • Ø
  • Å
Reading the documentation (here) I see information about the protocol, but I see no mention of what character encoding is supported. The only hint I have is that the C# class example (here) uses Encoding.ASCII.GetBytes().

My tests seem to suggest that only ASCII and UTF8 work, but neither support the special characters.
  • Using ASCII converts the characters to question marks.
  • Using UTF8 writes "æøåÆØÃ".
Last edited by Deantwo on Tue Mar 22, 2016 9:35 am, edited 3 times in total.
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26950
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia
Contact:

Re: API Encoding

Mon Mar 21, 2016 4:17 pm

RouterOS doesn't support these even in console directly or Winbox. So you will have to stick with a-z 0-9 for now
 
User avatar
Deantwo
Member
Member
Topic Author
Posts: 332
Joined: Tue Sep 30, 2014 4:07 pm

Re: API Encoding

Mon Mar 21, 2016 4:38 pm

RouterOS doesn't support these even in console directly or Winbox. So you will have to stick with a-z 0-9 for now
Ok. I would suggest mentioning in the documentation which encoding the API protocol supports.

I do see that if i manually set the the identity to "æøåÆØÅ" using WinBox it saves it as "\E6\F8\E5\C6\D8\C5".
But any attempt at printing this in the terminal will replace them with spaces (" ").
Reading the identity with the API seem to get "æøåÆØÅ" correctly though.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API Encoding

Mon Mar 21, 2016 5:32 pm

The API uses raw bytes (both when getting and when setting), not encoding/decoding of any kind.

Winbox on the other hand uses whatever your Windows encoding for non UTF-8 applications is, and the terminal escapes the non-ASCII bytes.

C# applications use Unicode.

To convert between the two, you can use System.Text.Encoding.Convert(). Convert from "Unicode" to "Default" when sending, and from "Default" to "Unicode" when receiving.
 
User avatar
Deantwo
Member
Member
Topic Author
Posts: 332
Joined: Tue Sep 30, 2014 4:07 pm

Re: API Encoding  [SOLVED]

Tue Mar 22, 2016 9:03 am

To convert between the two, you can use System.Text.Encoding.Convert(). Convert from "Unicode" to "Default" when sending, and from "Default" to "Unicode" when receiving.
That would explain it. I will give Default a try, I kinda skipped that one because, well it is called "default".
The example C# class just casts the bytes to char when it is reading, guess I could try just casting it to byte too.

EDIT:
Encoding it using Default alone seems to do the trick.
public void Send(string co)
{
    byte[] bajty = Encoding.Default.GetBytes(co.ToCharArray());
    byte[] velikost = EncodeLength(bajty.Length);
    
    _connection.Write(velikost, 0, velikost.Length);
    _connection.Write(bajty, 0, bajty.Length);
}
I'll just add that to the list of changes I would suggest in the example C# class.
Need to find out how/where to post wiki edits, since I seemingly can't make an account.
Last edited by Deantwo on Thu Jan 18, 2018 4:34 pm, edited 1 time in total.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API Encoding

Tue Mar 22, 2016 8:45 pm

The wiki client is not really a good one anyway. There are several other ones, with danikf's being one of the better ones (if not the best).

I don't know if it has that same issue, but if it does, I'd recommend you make a Pull Request to that. Ideally though, make it an option to specify the RouterOS charset (don't hardcore "Default"). Other people may want to create applications that read/write data in a foreign charset, rather than their OS' native charset.
 
User avatar
danikf
Frequent Visitor
Frequent Visitor
Posts: 72
Joined: Mon Mar 14, 2011 8:57 am

Re: API Encoding

Wed Jun 29, 2016 12:48 am

Hi,
with tik4net API it is just single-line-of-code. To set your own charset (default charset is ASCII) use this code:
using tik4net;
// ...
connection = ConnectionFactory.OpenConnection(TikConnectionType.Api, HOST, USER, PASS);
connection.Encoding = Encoding.GetEncoding("windows-1250");
See WIKI page for details. I have added it to support chinese comments :-)

Enjoy,
D