Re: Writing uncommon ASCII characters programmatically

"Gabriel Scherer [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <CAPFanBGEPA55w5UfMv0k2FJ7XgAnFW_OZQhZxrE58O4J5sv_Fg@mail.gmail.com>
The numerical value of the character 'c' is *defined* by the encoding.

The wikipedia article for latin-1
  http://en.wikipedia.org/wiki/ISO_8859-1
gives you the encodings in hexadecimal (you can use \xdd to specify the
hexa number as an OCaml character literal).
The character 'è' in this table is \xE8.

$ cat > test.ml <<EOF
print_endline "\xE8";;
EOF
$ ocaml test.ml | iconv -f latin1
è

According to
  http://en.wikipedia.org/wiki/UTF-8
the euro symbol "€" is encoded in UTF-8 as the byte sequence (in hexa) E2
82 AC:

$ cat > test.ml <<EOF
print_endline "\xE2\x82\xAC";;
EOF
$ ocaml test.ml | iconv -f utf-8
€



On Tue, Jan 27, 2015 at 11:12 AM, Doyle Jonathan [email protected]
[ocaml_beginners] <[email protected]> wrote:

>
>
> OK, so let me rephrase my question.
> I have a certain character (let us call it c).
> According to what you said, the corresponding \ddd will be displayed as c
> by an ASCII reader.
> What do I type in OCaml to get it displayed as c by an ISO-latin1 reader?
> What do I type in OCaml to get it displayed as c by an UTF-8 reader ?
>
> My point is that I do not want to change the encoding in the non-OCaml
> parts of my projects, which might cause trouble. I want to deal with the
> encoding issue in OCaml directly.
>
>
>   ------------------------------
>  *De :* "Gabriel Scherer [email protected] [ocaml_beginners]" <
> [email protected]>
> *À :* "[email protected]" <[email protected]>
> *Envoyé le :* Mardi 27 janvier 2015 10h52
> *Objet :* Re: "ocaml_beginners"::[] Writing uncommon ASCII characters
> programmatically
>
>
>  \ddd will output precisely the byte you ask for. How it will be
> interpreted by the reader depends on the encoding it has been configured
> for. "It is misread in the .sql file" doesn't mean much, it all depends on
> which software reads that file and how it is configured -- whether it
> expects UTF-8 input, or ISO-latin1, or just plain ASCII.
>
>
>
> On Tue, Jan 27, 2015 at 10:24 AM, Doyle Jonathan [email protected]
> [ocaml_beginners] <[email protected]> wrote:
>
>
>
> In my project where I use Ocaml to manipulate MySQL files, I need to wtite
> some text into files programmatically, and I am experiencing some trouble
> when the text has some accentuated Latin characters :
>
> 1) When I type accentuated character directly in the Ocaml source
> interpreter, it is misread in the receiving .sql file (this is example s1
> in the code below)
>
> 2) According to the Ocaml manual, I could use \ddd where ddd is the ASCII
> decimal code for the character. When I try that, it is misread  n the
> receiving .sql file again, but in a different way ( this is example s2 in
> the code below).
>
> Below I copied some functions from my personal library, then explained
> miscelleneaous attempts to write correctly an accentuated Latin character.
>
> Code from my personal library :
>
> type complete_filename=CFN of string;;
> type vague_filename=string;;
> let make_filename_complete (s:vague_filename)=
>   let home=Sys.getenv("HOME") in
>   if s="" then CFN(home) else
>   let c=String.get s 0 in
>   if c='/' then CFN(s) else
>   if c='~' then CFN(home^(String.sub s 1 (String.length(s)-1))) else
>   CFN((Sys.getcwd ())^"/"^s);;
> let open_out_locally x=try open_out(x) with
> _->failwith("File "^x^" cannot be opened out");;
> let erase_file_and_fill_it_with_contents_of_buffer (vx:vague_filename) b=
>    let x=(fun (CFN(u))->u)(make_filename_complete(vx)) in
>   let john=open_out_locally(x) in
>   (Buffer.output_buffer(john)(b);close_out john);;
> let erase_file_and_fill_it_with_string s (x:vague_filename)=
>    let n=String.length(s) in
>    let b=Buffer.create(n) in
>    let _=Buffer.add_string b s in
>    erase_file_and_fill_it_with_contents_of_buffer x b;;
>
>
> And here are my failed attempts :
>
> let s1="This is the French e grave character : è"  ;;
> erase_file_and_fill_it_with_string s1 "example.sql";;
>
> It produces the following output in example.sql :
> This is the French e grave character : è
>
> let s2="This is the French e grave character :\133"  ;;
> erase_file_and_fill_it_with_string s2 "example.sql";;
>
> It produces the following output in example.sql :
> This is the French e grave character :Ö
>
>
>
>
>
>
>
>
>
>
> 
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.