Terminal control/Display an extended character: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Perl 6}}: avoid decimal char numbers, use fullwidth pound sign)
Line 143: Line 143:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
To demonstrate we're not limited to Latin-1, we'll print the fullwidth variant.
<lang Perl 6>chr( 163 ).say ;</lang>
<lang Perl 6>say '£';
say "\x[FFE1]";
say "\c[FULLWIDTH POUND SIGN]";
0xffe1.chr.say;</lang>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==

Revision as of 17:34, 10 September 2011

Task
Terminal control/Display an extended character
You are encouraged to solve this task according to the task description, using any language you may know.

The task is to display an extended (non ASCII) character onto the terminal. For this task, we will display a £ (GBP currency sign).

Ada

<lang ada>with Ada.Text_IO; use Ada.Text_IO; with Ada.Characters.Latin_1;

procedure Pound is begin

  Put(Ada.Characters.Latin_1.Pound_Sign);

end Pound;</lang>

Ada allows Unicode characters in the source, and provides output functions on "wide characters".

<lang ada>with Ada.Wide_Text_IO; use Ada.Wide_Text_IO;

procedure Unicode is begin

  Put("札幌");

end Unicode;</lang>

AWK

You can print a literal "£".

<lang awk>BEGIN { print "£" }</lang>

You can print a "£" using the escape sequences that match the encoding of your terminal.

cp437 "\234"
iso-8859-1 "\243"
euc-jp "\241\362"
utf-8 "\302\243"
gb18030 "\201\60\204\65"

<lang awk>BEGIN { print "\302\243" } # if your terminal is utf-8</lang>

BASIC

ZX Spectrum Basic

The ZX Spectrum uses a modified ascii character set that has a uk pound sign at character number 96:

<lang basic>10 PRINT CHR$(96);</lang>

bc

You can print a literal "£".

<lang bc>"£ " quit</lang>

C

Translation of: AWK

<lang c>#include <stdio.h>

int main() { puts("£"); puts("\302\243"); /* if your terminal is utf-8 */ return 0; }</lang>

C++

<lang cpp>#include <iostream>

int main() {

   std::cout << static_cast<char>(163); // pound sign
   return 0;

}</lang>

Forth

Works with: GNU Forth version 0.7.0

The emerging ANS Forth 20xx standard includes an XCHAR wordset which allows manipulation of non-ASCII character sets such as Unicode.

<lang forth>163 xemit \ £, or s" £" type</lang>

Go

<lang go>package main

import "fmt"

func main() {

   fmt.Println("£")

}</lang>

Icon and Unicon

Write a given character number, say '163', using char to convert the integer into a string.

<lang Icon> procedure main ()

 write ("£ " || char (163)) # £

end </lang>

J

<lang J> '£' £

  '札幌'

札幌</lang>

Java

<lang Java>import java.io.PrintStream; import java.io.UnsupportedEncodingException;

public class Main {

   public static void main(String[] args) throws UnsupportedEncodingException
   {
       PrintStream writer = new PrintStream(System.out, true, "UTF-8");
       writer.println("£");
       writer.println("札幌");
   }

}</lang>

Locomotive Basic

<lang locobasic>10 PRINT CHR$(163)</lang>

Pascal

<lang Pascal>program pound; uses crt; begin

 write(chr( 163 ));

end. </lang>

Perl 6

To demonstrate we're not limited to Latin-1, we'll print the fullwidth variant. <lang Perl 6>say '£'; say "\x[FFE1]"; say "\c[FULLWIDTH POUND SIGN]"; 0xffe1.chr.say;</lang>

PicoLisp

<lang PicoLisp>(prinl (char 26413) (char 24140)) # Sapporo </lang> Output:

札幌

PureBasic

<lang PureBasic>Print(Chr(163))</lang>

£

Python

<lang Python>print u'\u00a3'</lang>

£

REXX

<lang rexx> say d2c(163) /* Assuming the pound sign is 163 on the display codepage */ </lang>

Seed7

A write to a console accepts Unicode characters.

<lang seed7>$ include "seed7_05.s7i";

 include "console.s7i";

const proc: main is func

 local
   var text: console is STD_NULL;
 begin
   console := open(CONSOLE);
   write(console, "£");
   # Terminal windows often restore the previous
   # content, when a program is terminated. Therefore
   # the program waits until Return/Enter is pressed.
   readln;
 end func;</lang>

Tcl

Provided the system encoding has a “£” symbol in it, this works: <lang tcl>puts \u00a3</lang> Tcl can output all unicode characters in the BMP, but only if the consumer of the output (terminal, etc.) is able to understand those characters in its current encoding will the output actually make sense. Strictly, this is not a limitation of Tcl but of the environment in which it is placed.