473,785 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I get the 8bit-string representation of any unicode string

Hello, everyone.

I have a problem when I'm processing unicode strings. Is it possible
to get the 8bit-string representation of any unicode string?

Suppose I get a unicode string:
a = u'\xc8\xce\xcf\ xcd\xc6\xeb';
then, by
a.encode('latin-1');
I can get the 8bit-string representation of it, that is, the physical
storage format of this string.

But for another kind of unicode string, say:
b = u'\u4efb\u8d24\ u9f50';
I have to:
b.encode('utf-8')
to get the 8bit-string format of it.

Since these unicode strings are given by an external library function,
I don't know which kind a unicode string belongs to before I get it at
runtime. So, I wonder if there is a unified way to get the 8bit-string
representation, say, byte-by-byte, of any unicode string?

Thank you very much.

Feb 12 '06 #1
5 2500
wa****@gmail.co m wrote:
Hello, everyone.

I have a problem when I'm processing unicode strings. Is it possible
to get the 8bit-string representation of any unicode string?
Yes, if you can be more precise about what you mean by '8bit-string
representation' . Likely candidates are
b.encode('utf-8')
b.encode('utf_1 6_be')
b.encode('utf_1 6_le')

Kent

Suppose I get a unicode string:
a = u'\xc8\xce\xcf\ xcd\xc6\xeb';
then, by
a.encode('latin-1');
I can get the 8bit-string representation of it, that is, the physical
storage format of this string.

But for another kind of unicode string, say:
b = u'\u4efb\u8d24\ u9f50';
I have to:
b.encode('utf-8')
to get the 8bit-string format of it.

Since these unicode strings are given by an external library function,
I don't know which kind a unicode string belongs to before I get it at
runtime. So, I wonder if there is a unified way to get the 8bit-string
representation, say, byte-by-byte, of any unicode string?

Thank you very much.

Feb 12 '06 #2
wa****@gmail.co m wrote:
I have a problem when I'm processing unicode strings. Is it possible
to get the 8bit-string representation of any unicode string?

Suppose I get a unicode string:
a = u'\xc8\xce\xcf\ xcd\xc6\xeb';
then, by
a.encode('latin-1');
I can get the 8bit-string representation of it, that is, the physical
storage format of this string.

But for another kind of unicode string, say:
b = u'\u4efb\u8d24\ u9f50';
I have to:
b.encode('utf-8')
to get the 8bit-string format of it.
latin-1 and utf-8 are two different 8-bit representations (encodings) of
Unicode.
Since these unicode strings are given by an external library function,
I don't know which kind a unicode string belongs to before I get it at
runtime. So, I wonder if there is a unified way to get the 8bit-string
representation, say, byte-by-byte, of any unicode string?


since the Unicode character set contains 1.1 million code points, and a
single byte can contain 256 different values, it should be fairly obvious
that there's no "8 bit byte by byte" representation of a Unicode string.
you need to decide what 8-bit encoding to use, and stick to that.

</F>

Feb 12 '06 #3
Thank you all for your replies :-)

I may misunderstood it. I will think about it carefully.

By the way, does python has a interface, just like iconv in libc for
C/C++? Or, how can I convert a string from a encoding into another
one?
Thank you so much.

Feb 12 '06 #4
wa****@gmail.co m wrote
I may misunderstood it. I will think about it carefully.

By the way, does python has a interface, just like iconv in libc for
C/C++? Or, how can I convert a string from a encoding into another
one?


if b is an 8-bit string containing an encoded unicode string,

u = b.decode(encodi ng)

or

u = unicode(b, encoding)

gives you a unicode string. to encode the unicode string back to another
byte string, use the encode method.

b = u.encode(encodi ng)

</F>

Feb 12 '06 #5
Hi,

I see. Thank you for your help!
Regards,
hongzheng

Fredrik Lundh wrote:
wa****@gmail.co m wrote
I may misunderstood it. I will think about it carefully.

By the way, does python has a interface, just like iconv in libc for
C/C++? Or, how can I convert a string from a encoding into another
one?


if b is an 8-bit string containing an encoded unicode string,

u = b.decode(encodi ng)

or

u = unicode(b, encoding)

gives you a unicode string. to encode the unicode string back to another
byte string, use the encode method.

b = u.encode(encodi ng)

</F>


Feb 12 '06 #6

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

Similar topics

0
1581
by: marko | last post by:
How to convert 8bit numbers to 7bit numbers? Thanks, Marko
2
3106
by: Web Developer | last post by:
Hi, Have a book that says a char in c++ is 8bit wide. But i remember reading that C++ uses ANSI character set which uses 7bit per character. Who is right? WD
8
3152
by: Nick | last post by:
Hi ! I want to load an old Pascal-Dos-File where records stand in. When i view the file in a HEX-Editor it's clear how to acces these Strings and chars in that file. Since these are old 8BIT chars (C# uses 16BIT) i read the file bytewise and convert the bytes to chars using ENCODER.getChars(). From the chars i make a big String which should be the file as i see in the HEX-Editor. But there are many errors
9
4447
by: Raj | last post by:
Hello Members, I wrote a program to convert a greyscale bitmap image in to monochrome bitmap image, a simple thresholding. Input:Greyscale image; Expected Output:Monochrome image Pseudocode: row:0->height column:0->width
0
2227
by: Andreas Prilop | last post by:
http://www.iana.org/assignments/character-sets and RFC 1428 have an encoding (charset) "unknown-8bit". There is also the widely recognized "x-user-defined", which means the same thing, afaik. Both designate an 8-bit encoding where *nothing* is known about its characters. Especially, unknown-8bit is *not* necessarily a superset of US-ASCII. Wouldn't it be useful (e.g. for certain HTML documents) to have a charset parameter that is "half...
1
1412
by: James Dean | last post by:
I know that 16bit colour is split up into 3 "5bit" sections but i can't seems to find how exactly 8bit is split up?.... *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
0
1329
by: Filippo Bettinaglio | last post by:
Using NET2: Can I load a 24bit .BMP image and convert it in a 8Bit BMP, and save the new one on disk? Many Thanks, Filippo
1
2717
by: Filippo Bettinaglio | last post by:
Using C# 2005 How can I load a 24bit colors .BMP image and convert it in a 8bit colors BMP, and save the new one on disk? Many Thanks, Filippo
0
1092
by: Andrew Bullock | last post by:
Hi, Is this possible? If so can someone please give me an example of how to do it? Thanks Andrew
1
4694
by: Neeraj | last post by:
Hi guys, I am currently working on Dicom image of format jpeg. I am searching about that on net but not getting much more. If any body knows C# source code which open at least on type of jpeg Dicom image.plz guide me. regards Neeraj
0
9647
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10100
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7509
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4061
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
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.