Posts

Showing posts with the label Unicode

C++: Printing ASCII Heart And Diamonds With Platform Independent

Answer : If you want a portable way, then you should use the Unicode code points (which have defined glyphs associated to them): ♠ U+2660 Black Spade Suit ♡ U+2661 White Heart Suit ♢ U+2662 White Diamond Suit ♣ U+2663 Black Club Suit ♤ U+2664 White Spade Suit ♥ U+2665 Black Heart Suit ♦ U+2666 Black Diamond Suit ♧ U+2667 White Club Suit Remember that everything below character 32 in ASCII is a control character . They have a meaning associated with them and you don't have a guarantee of getting a glyph or a behavior there (even though most control characters to have glyphs, although they were never intended to be printable). Still, it's not a safe bet. However, using Unicode needs proper font and encoding support which may or may not be a problem on UNIX-likes. On Windows at least some of the above code points map to the ASCII control character glyphs you're outputting if the console is set to raster fonts (and therefore not supporting Unicode or anything else th...

Byte String Vs. Unicode String. Python

Answer : No python does not use its own encoding. It will use any encoding that it has access to and that you specify. A character in a str represents one unicode character. However to represent more than 256 characters, individual unicode encodings use more than one byte per character to represent many characters. bytearray objects give you access to the underlaying bytes. str objects have the encode method that takes a string representing an encoding and returns the bytearray object that represents the string in that encoding. bytearray objects have the decode method that takes a string representing an encoding and returns the str that results from interpreting the bytearray as a string encoded in the the given encoding. Here's an example. >>> a = "αά".encode('utf-8') >>> a b'\xce\xb1\xce\xac' >>> a.decode('utf-8') 'αά' We can see that UTF-8 is using four bytes, \xce, \xb1, \xce, and \xac to repr...