473,586 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to emit UTF-8 from console mode?

The following perl program works when I run it from urxvt-X console on
cygwin-x windows

LC_CTYPE=en_US. UTF-8 urxvt-X.exe&
perl -wle "binmode STDOUT, q[:utf8]; print chr() for 0x410 .. 0x430;"

This little one liner prints the Russian alphabet in Cryllic. With some
slight modification it will also print a lot of other alphabets too --
including Hebrew, chinese and japanese.

It does not work with cmd.exe because apparently cmd.exe cannot deal with
UTF-8.

Can someone help me translate it into python? I would not expect it to work
from cmd.exe with python, but I am hopeful it will work with urxvt-X!

Thanks,
Siegfried
Oct 1 '08 #1
8 7023
LC_CTYPE=en_US. UTF-8 urxvt-X.exe&
perl -wle "binmode STDOUT, q[:utf8]; print chr() for 0x410 .. 0x430;"
Can someone help me translate it into python?
LC_CTYPE=en_US. UTF-8 urxvt-X.exe&
python -c 'for i in range(0x410, 0x431):print unichr(i),'
I would not expect it to work
from cmd.exe with python
It should work in cmd.exe, as long as the terminal's encoding supports
these characters in the first place. Use chcp.exe to find out what the
terminal's encoding is. The Python program is not completely equivalent,
as it leaves the output encoding to Python, rather than assuming a fixed
UTF-8 output encoding.

Regards,
Martin

Oct 1 '08 #2

""Martin v. Löwis"" <ma****@v.loewi s.dewrote in message
news:48******** **************@ news.freenet.de ...
>LC_CTYPE=en_US .UTF-8 urxvt-X.exe&
perl -wle "binmode STDOUT, q[:utf8]; print chr() for 0x410 .. 0x430;"
>Can someone help me translate it into python?

LC_CTYPE=en_US. UTF-8 urxvt-X.exe&
python -c 'for i in range(0x410, 0x431):print unichr(i),'
>I would not expect it to work
from cmd.exe with python

It should work in cmd.exe, as long as the terminal's encoding supports
these characters in the first place. Use chcp.exe to find out what the
terminal's encoding is. The Python program is not completely equivalent,
as it leaves the output encoding to Python, rather than assuming a fixed
UTF-8 output encoding.

Regards,
Martin
Make sure you are using the Lucida Console font for the cmd.exe window and
type the commands:

chcp 1251
python -c "print ''.join(unichr( i) for i in range(0x410,0x4 31))"

Output:

АБВГДЕЖ ИЙКЛМНО П*СТУФХ ЧШЩЪЫЬ* ЮЯа

UTF-8 encoding (chcp 65001) doesn't work (Python doesn't recognize it:
"LookupErro r: unknown encoding: cp65001") and I couldn't get any Chinese
code pages to work either. There is some trick I don't know, because
Chinese versions of Windows can display Chinese. I have the East Asian
languages installed and Chinese IME enabled, but it doesn't help for console
apps.

--Mark

Oct 1 '08 #3
>Make sure you are using the Lucida Console font for the cmd.exe window and
type the commands:

chcp 1251
python -c "print ''.join(unichr( i) for i in range(0x410,0x4 31))"

Output:

?????????????? ??????????????? ????
Wowa! I was not aware of that chcp command! Thanks! How could I do that
"chcp 1251" programatically ?

The code was a little confusing because those two apostrophes look like a
double quote!

But what are we doing here? Can you convince me that we are emitting UTF-8?
I need UTF-8 because I need to experiment with some OS function calls that
give me UTF-16 and I need to emit UTF-16 or UTF-8.

I think part of the problem is that Lucida Console is not as capable as
"Arial Unicode MS" or the fonts used by urxvt-X.

Thanks,
Siegfried
Oct 1 '08 #4
On Wed, 01 Oct 2008 08:17:15 -0700, Siegfried Heintze wrote:

(snip)
The code was a little confusing because those two apostrophes look like
a double quote!
Tips: use mono-spaced font. There is no ambiguity.

(snip)
I think part of the problem is that Lucida Console is not as capable as
"Arial Unicode MS" or the fonts used by urxvt-X.

Thanks,
Siegfried
Why don't you write it to a file? Then open that file from Notepad

Oct 1 '08 #5
But what are we doing here? Can you convince me that we are emitting UTF-8?

Most definitely not. We are emitting cp1251.
I need UTF-8 because I need to experiment with some OS function calls that
give me UTF-16 and I need to emit UTF-16 or UTF-8.
Try setting the code page to 65001, and emit the UTF-8 explicitly.

Regards,
Martin
Oct 1 '08 #6
I need UTF-8 because I need to experiment with some OS function calls that
give me UTF-16 and I need to emit UTF-16 or UTF-8.
<ma****@v.loewi s.dewrote:
>Try setting the code page to 65001, and emit the UTF-8 explicitly.
Hmm... apparently that's not allowed on Windows XP:

C:\chcp 65001
Active code page: 65001

C:\python -c "for i in range(0x410, 0x430): print unichr(i).encod e('utf-8')"
Traceback (most recent call last):
File "<string>", line 1, in <module>
IOError: [Errno 13] Permission denied

This works though:

C:\python -c "for i in range(0x410, 0x430): print unichr(i).encod e('utf-8')" x

C:\type x
[a bunch of Cyrillic letters]

Hmm... "more x" doesn't work, while "copy x con" works but gives an error.
Looks like Windows XP support UTF-8 console output is a bit half-assed.

Ross Ridge

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rr****@csclub.u waterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
Oct 1 '08 #7

"Ross Ridge" <rr****@csclub. uwaterloo.cawro te in message
news:gc******** **@rumours.uwat erloo.ca...
>I need UTF-8 because I need to experiment with some OS function calls
that
give me UTF-16 and I need to emit UTF-16 or UTF-8.

<ma****@v.loewi s.dewrote:
>>Try setting the code page to 65001, and emit the UTF-8 explicitly.

Hmm... apparently that's not allowed on Windows XP:

C:\chcp 65001
Active code page: 65001

C:\python -c "for i in range(0x410, 0x430): print
unichr(i).encod e('utf-8')"
Traceback (most recent call last):
File "<string>", line 1, in <module>
IOError: [Errno 13] Permission denied

This works though:

C:\python -c "for i in range(0x410, 0x430): print
unichr(i).encod e('utf-8')" x

C:\type x
[a bunch of Cyrillic letters]

Hmm... "more x" doesn't work, while "copy x con" works but gives an error.
Looks like Windows XP support UTF-8 console output is a bit half-assed.
It is odd, though, that when the code page (and font) are correct,
redirecting to a file and typing it work, but printing the result to the
console does not

Oct 2 '08 #8

"Siegfried Heintze" <si*******@hein tze.comwrote in message
news:vL******** *************** *******@comcast .com...
>
>>Make sure you are using the Lucida Console font for the cmd.exe window and
type the commands:

chcp 1251
python -c "print ''.join(unichr( i) for i in range(0x410,0x4 31))"

Output:

????????????? ??????????????? ?????
Wowa! I was not aware of that chcp command! Thanks! How could I do that
"chcp 1251" programatically ?

The code was a little confusing because those two apostrophes look like a
double quote!

But what are we doing here? Can you convince me that we are emitting
UTF-8? I need UTF-8 because I need to experiment with some OS function
calls that give me UTF-16 and I need to emit UTF-16 or UTF-8.

I think part of the problem is that Lucida Console is not as capable as
"Arial Unicode MS" or the fonts used by urxvt-X.
In this case, it is not emitting UTF-8. It is emitting the windows-1251
encoding. As another poster mentioned, the Windows console gets an error
when attempting to write UTF8 when the code page is 65001 (UTF8). But you
can write output to a file explicitly in UTF-8 or UTF-16 and view the file
with Notepad. I've used this method for processing Chinese.
>>import os,codecs
data = u''.join(unichr (i) for i in range(0x410,0x4 31))
codecs.open(' out.txt','wt',' utf-8').write(data)
os.startfile( 'out.txt')
P.S.

One way to set the code page programmaticall y is to use ctypes, but this
will only work in a Windows console:
>>import ctypes
k=ctypes.WinD LL('kernel32')
x.SetConsoleO utputCP(1251)
1
>>print u''.join(unichr (i) for i in
range(0x410,0 x430)).encode(' windows-1251')
АБВГДЕЖ ИЙКЛМНО П*СТУФХ ЧШЩЪЫЬ* ЮЯ

--Mark

Oct 2 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2176
by: Nigel Sampson | last post by:
ey all, I'm in the process of creating a method of tracking whenever an objects properties are changed. Since its not possible to alter existing types I decided to used a proxy generated using Reflection.Emit which inherits from the object needed Person p = (Person)ProxyBuilder.Construct(typeof(Person));
2
5179
by: DEK | last post by:
I'm creating a dynamic assembly which has one type with one constructor, the type inherits a base class Season, which is in the current assembly. I am trying to emit a constructor which simply calls the base constructor with the 4 args passed in, but I get this error Message at the second last line of the method CreateSeason (Marked with...
7
9948
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the method. The method could look like this: public void myDynamicMethod(Type type){ //.....do something with the Type passed in....... }
3
2510
by: John Arthur | last post by:
Hi, I am reading about Reflection.Emit and I can't really imagine a real situation where I can use this. Can someone give me an example that can't be done without Reflection or an example of something that If done with Reflection.Emit will be a lot faster or something sensible. I will really appreciate your help. Thanks : John Arthur
8
2533
by: Eyeawanda Pondicherry | last post by:
I have put some code together that creates an enum dynamically from some database values. The enum can be read perfectly by an application that references the dynamically generated dll. If I /emit/ a new version of the assembly, and start the application again, the new values will appear for the enum. However, suppose I want the...
4
3052
by: Gianluca | last post by:
This is the simplified code I'm trying to generate: ilg.DeclareLocal(typeof(int)); // define local integer Label exit = ilg.DefineLabel(); // define "exit" label ilg.Emit(OpCodes.Ldc_I4_0); // put 0 on the stack ilg.Emit(OpCodes.Stloc_0); // set the local integer to 0 ilg.Emit(OpCodes.Ldloc_0); // load the local...
2
3626
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
1
1430
by: John | last post by:
I am trying to figure out how to translate the following CIL call into an Emit statement. In CIL it is: IL_004c: callvirt instance class System.Data.DataRow System.Data.DataRowCollection::get_Item(int32) The C# equivilant is as follows: MyDataRowCollection;
2
4475
by: joasi | last post by:
Hi, I am working in C# with an Object / Relational Database mapping framework, where I want to generate method calls based on string representation of the call signature. I have used reflection and Method-/Field-/PropertyInfo invokation to do this, but now I want to make the calls faster by using Emit and DynamicMethods I am now trying to...
0
1652
by: =?Utf-8?B?QXJ0dXJvIE1hcnRpbmV6?= | last post by:
I've been looking everywhere on how to call AddRange Method of a List<myClassfield in Reflection.Emit My code goes like this MethodInfo method = typeof(List<>).MakeGenericType(typeof(myClass)).GetMethod("AddRange", BindingFlags.Instance | BindingFlags.Public, null, new Type { typeof(IEnumerable<>).MakeGenericType(typeof(myClass)) },...
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.