1

Hi I have a multilingual line: "Hindi - हिंदी , Chinese - 痴呢色 ,Russian - руссиан"

I need to perform an URLEncode on it after cnverting it into UTF8 in both PHP and C#.

<?php
$sms_text = 'Hindi - हिंदी , Chinese - 痴呢色 ,Russian - руссиан';
$encoded_text = utf8_encode($sms_text);
$message = urlencode($encoded_text);

echo $message;

Output: (Correct)

Hindi++-+%C3%A0%C2%A4%C2%B9%C3%A0%C2%A4%C2%BF%C3%A0%C2%A4%C2%82%C3%A0%C2%A4%C2%A6%C3%A0%C2%A5%C2%80+%2C+Chinese+-++%C3%A7%C2%97%C2%B4%C3%A5%C2%91%C2%A2%C3%A8%C2%89%C2%B2+%C3%AF%C2%BC%C2%8CRussian+-+%C3%91%C2%80%C3%91%C2%83%C3%91%C2%81%C3%91%C2%81%C3%90%C2%B8%C3%90%C2%B0%C3%90%C2%BD

But when I use c#:

string msg = "Hindi - हिंदी , Chinese - 痴呢色 ,Russian - руссиан";
var a = Encoding.UTF8.GetBytes(msg);
msg = HttpUtility.UrlEncode(a);
Console.WriteLine(msg);

It gives me following output (Incorrect)

Hindi+-+%e0%a4%b9%e0%a4%bf%e0%a4%82%e0%a4%a6%e0%a5%80+%2c+Chinese+-+%e7%97%b4%e5%91%a2%e8%89%b2+%ef%bc%8cRussian+-+%d1%80%d1%83%d1%81%d1%81%d0%b8%d0%b0%d0%bd

Can anyone please help me to fix my C# code.

3
  • try HttpUtility.UrlEncodeUnicode?
    – sietse85
    Oct 9, 2018 at 15:36
  • Please see my dotnet fiddle, the output is quite different than what I have in PHP : dotnetfiddle.net/Widget/xJxjgj Oct 9, 2018 at 15:40
  • use mb_convert_encoding for converting charsets in PHP
    – sietse85
    Oct 9, 2018 at 16:09

2 Answers 2

0

So, there are a couple of issues here -

The php you have isnt giving you the correct result - $encoded_text = utf8_encode($sms_text); that part is causing some mangling and shouldnt be needed.

Beyond that in the C# code you are calling msg = HttpUtility.UrlEncode(a); this is causing you to url encode the Type of a, and not the string. Again all you should need to do is

UTF8Encoding utf8 = new UTF8Encoding(); string utf8Encoded = HttpUtility.UrlEncode(msg,utf8);

something like that, if you ditch the mangling encode from your PHP and only do a straight utf8 url encode on the string .. they end up coming out the same in both C# and PHP

1
  • 1
    utf8_encode is only meant for ISO-8859-1 string and should not be needed anyways
    – sietse85
    Oct 9, 2018 at 16:08
0

You have error in php code. Just write this:

$sms_text = 'Hindi - हिंदी , Chinese - 痴呢色 ,Russian - руссиан';
$message = rawurlencode($sms_text);

and this in C# project

string msg = "Hindi - हिंदी , Chinese - 痴呢色 ,Russian - руссиан";    
HttpUtility.UrlEncode(msg, Encoding.UTF8);
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.