[Mono-list] C# Hexadecimal Escape Char Anomalies???

A Rafael D Teixeira rafael.teixeirabr@terra.com.br
Mon, 23 Feb 2004 10:36:01 -0300


Matthew Franz wrote:

        I realize this is a C# issue but can one create "binary strings"
        in C# using
        string="\xff\x00\x00\xff" used by C/Python?
        
        Why do my 0xff's get translated to 0x3f's???
        
Well, you are forgetting that C# strings are Unicode (UTF-16 encoded)
and when output to any stream it will be converted to that stream's
encoding. In the case of the console the output encoding is governed by
the LANG environment variable. See:
        
[rafael@redhat9]$ export LANG=en_US
[rafael@redhat9]$ mono bobo.exe | od -h
0000000 ffff
0000002
[rafael@redhat9]$ export LANG=en_US.UTF-8
[rafael@redhat9]$ mono bobo.exe | od -h
0000000 bfc3 bfc3
0000004

where the source is:

[rafael@redhat9]$ cat bobo.cs
using System;
 
class SimpleTest
{
        public static void Main (String[]args)
        {
                String bobo = "\xFF\xFF";
                System.Console.Write (bobo);
        }
}

Note: When outputting to a stream you are creating, you can choose what
encoding the stream will use, so you have better control of the
situation than with the console.

Hope it helps,
-- 
Rafael "Monoman" Teixeira 
Mono Hacker since 16 Jul 2001 - http://www.go-mono.org/
Mono Brasil Founding Member - http://monobrasil.redesolbrasil.org/
English Blog: http://monoblog.blogspot.com/
Brazilian Portuguese Blog: http://monoblog.weblogger.terra.com.br/