codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Can't get non-latin chars from clipboard on c# correctly


By : , Category : c#

Try to use Clipboard.GetText(); instead of (string)iData.GetData(DataFormats.Text);, which should get the text in the correct format (Text or UnicodeText).

ReLated :

The problem is that many mobile devices don't have complete font sets installed. There is nothing you can do except avoid these characters.

Convert the text to Unicode by whatever means you see fit. In Delphi 7 that typically involves using WideString.

Once you have the text encoded as UTF-16, for instance in a WideString, you need to call SetClipboardData using the CF_UNICODETEXT clipboard format. This is wrapped by Delphi as the SetAsHandle method of the global Clipboard object.

I've not tested it, but this function should set you on the way:

uses
  Windows, Clipbrd;

procedure SetClipboardText(const Text: WideString);
var
  Count: Integer;
  Handle: HGLOBAL;
  Ptr: Pointer;
begin
  Count := (Length(Text)+1)*SizeOf(WideChar);
  Handle := GlobalAlloc(GMEM_MOVEABLE, Count);
  Try
    Win32Check(Handle<>0);
    Ptr := GlobalLock(Handle);
    Win32Check(Assigned(Ptr));
    Move(PWideChar(Text)^, Ptr^, Count);
    GlobalUnlock(Handle);
    Clipboard.SetAsHandle(CF_UNICODETEXT, Handle);
  Except
    GlobalFree(Handle);
    raise;
  End;
end;

filename is not changed - the variable names the same string, and the string cannot be modified. As such, each loop starts working on the original string again when it uses filename.replace...

Instead, eliminate filename (or integrate it fully) and use str = str.replace..

str = str.replace(vregex.exec(str)[0],'');
for (var i = 0; i < from.length; i++) {
  str = str.replace(new RegExp(from[i], 'g'), to[i]);
  // ^-- next loop gets new value
}

(Also, this could be handled with a replacement function and a map instead of n-loops and there might be a Unicode library for JavaScript available..)


An approach using a map and a replacement function might look like:

// Specify map somewhere reusable; can be built from paired arrays for simplicity.
var replacements = {"à":"a", "á":"a", .. ";":"_"}
// Object.keys is ES5, shim as needed. e.g. result: [à;á..]
var alternation = "[" + Object.keys(replacements).join("") + "]"
// This regex will match all characters we are trying to match.
var regex = new Regex(alternation, "g")

str = str.replace(regex, function (m) { 
    var r = replacements[m]
    return r || m        
})

See String.replace(regex, function)

OK. I found the issue and solved. The only thing I needed to do is to add the following line within the Servlet's DoPost function:

request.setCharacterEncoding("UTF-8");

Once this was done, I now receive all strings with the correct characters (Cyrillic, Chinese, Arab, etc.).

Thanks to this guy for posting his solution!!!

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .