0

I need to show readable arabic string correctly in WinForm, in Notepad++ i set encoding to ANSI, paste the string "ÌÜÜæØÜÜÇÈá ÊíÌÜÜí 2", after that i go to encoding -> Char coding -> Arab -> Windows 1256, and i gets the correct value جــوطــابل تيجــي 2

How can i do that by C#?, i try with that but dosn't work:

        Encoding en_source = Encoding.GetEncoding("windows-1250");
        Encoding en_dest = Encoding.Unicode; 
        byte[] srcBytes = en_source.GetBytes("ÌÜÜæØÜÜÇÈá ÊíÌÜÜí 2");
        return  en_dest.GetString(srcBytes);
Mohamed SAIM
  • 69
  • 1
  • 7
  • 1
    You paste "جــوطــابل تيجــي" wherever you want to have it. C# understands UTF-8 natively. No need to do fancy conversions. – nvoigt Mar 28 '18 at 15:40
  • @nvoigt, i have only "ÌÜÜæØÜÜÇÈá ÊíÌÜÜí 2" restored from old app – Mohamed SAIM Mar 28 '18 at 15:44
  • And what's the result of the code? Wrong output, exception, compilation error... – H. Pauwelyn Mar 28 '18 at 15:49
  • H. Pauwelyn , Wrong Output, unreadable string! – Mohamed SAIM Mar 28 '18 at 15:51
  • I found the solution;Encoding en_source = Encoding.Default; Encoding en_dest = Encoding.GetEncoding("windows-1256"); byte[] srcBytes = en_source.GetBytes("ÌÜÜæØÜÜÇÈá ÊíÌÜÜí 2"); return en_dest.GetString(srcBytes); – Mohamed SAIM Mar 28 '18 at 15:54
  • var bytes = Encoding.Default.GetBytes(nonsense); var arabic = Encoding.GetEncoding(1256).GetString(bytes); Not every nonsense string will convert properly, you need to tackle this at the root. – Hans Passant Mar 28 '18 at 15:54
  • Hans Passant, thank you, thank for all. – Mohamed SAIM Mar 28 '18 at 15:58
  • As an addition to Hans' answer: Encoding.Default means the current system default encoding. The actual encoding thus can and will vary from computer to computer. So it's only useable if you have text created from an application running on the current computer. – ckuri Mar 28 '18 at 16:03
  • The must important thing is, that i have restore the data correctly!!! – Mohamed SAIM Mar 28 '18 at 16:09

1 Answers1

4

You want to convert from codepage 1252 to 1256.

var oldStr = "ÌÜÜæØÜÜÇÈá ÊíÌÜÜí 2";

var enSource = Encoding.GetEncoding(1252); // Windows 1252
var enDest = Encoding.GetEncoding(1256); // Windows 1256

var srcBytes = enSource.GetBytes(oldStr);

var newStr = enDest.GetString(srcBytes);

Note that this works in this case, but you may have other cases that does not convert correctly using enSource as codepage 1252. Assuming this nonsense string is generated the same way in your old application, this should work fine for you.

St. Pat
  • 749
  • 4
  • 12
  • The old application that generate this data, was maded by Delphi7 and paradox db, that make the source encoding is specific, but i can't identify it, the only way is test! – Mohamed SAIM Mar 28 '18 at 16:08
  • 1
    @MohamedSAIM it looks like paradox db relied on code pages to display international characters, while Delphi liked to use the system's default code page to write strings. Unfortunately, this means that if systems with different default code pages were used, or if a system was updated and the default code page changed, then you will most likely run into a string where my answer doesn't work, and you will have to figure out the code page used. Hopefully this won't be a problem for you though. – St. Pat Mar 28 '18 at 16:20