using System;
namespace Samples
{
class Program
{
static void Main(string[] args)
{
Version version1 = new Version(2, 0);
Version version2 = new Version(2, 0, 0);
Version version3 = new Version(2, 0, 0, 0);
Console.WriteLine(version1.Equals(version2));
Console.WriteLine(version1.Equals(version3));
Console.WriteLine(version2.Equals(version3));
}
}
}
Author: coder
Get byte count from UnicodeEncoding
using System;
using System.Text;
class UnicodeEncodingExample {
public static void Main() {
String chars = "Unicode Encoding Example";
UnicodeEncoding Unicode = new UnicodeEncoding();
int byteCount = Unicode.GetByteCount(chars);
Console.WriteLine("{0} bytes needed to encode string.", byteCount);
}
}
Use UnicodeEncoding
using System;
using System.Text;
class StringEncodingApp {
static void Main(string[] args) {
ASCIIEncoding ae = new ASCIIEncoding();
Console.WriteLine(ae.GetString(new byte[]{1,2,3}));
UnicodeEncoding bu = new UnicodeEncoding(true, false);
Console.WriteLine(bu.GetString(new byte[]{1,2,3}));
}
}
Returns the value encoded in Big/Little Endian (PPC, XDR) format.
// Copyright 2006 - 2008: Rory Plaire (codekaizen@gmail.com)
//
// This file is part of GeoAPI.
// GeoAPI is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// GeoAPI is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with GeoAPI; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
namespace GeoAPI.DataStructures
{
public static class ByteEncoder
{
#region Endian conversion helper routines
/// <summary>
/// Returns the value encoded in Big Endian (PPC, XDR) format.
/// </summary>
/// <param name="value">Value to encode.</param>
/// <returns>Big-endian encoded value.</returns>
public static Int32 GetBigEndian(Int32 value)
{
if (BitConverter.IsLittleEndian)
{
return swapByteOrder(value);
}
else
{
return value;
}
}
/// <summary>
/// Returns the value encoded in Big Endian (PPC, XDR) format.
/// </summary>
/// <param name="value">Value to encode.</param>
/// <returns>Big-endian encoded value.</returns>
[CLSCompliant(false)]
public static UInt16 GetBigEndian(UInt16 value)
{
if (BitConverter.IsLittleEndian)
{
return swapByteOrder(value);
}
else
{
return value;
}
}
/// <summary>
/// Returns the value encoded in Big Endian (PPC, XDR) format.
/// </summary>
/// <param name="value">Value to encode.</param>
/// <returns>Big-endian encoded value.</returns>
[CLSCompliant(false)]
public static UInt32 GetBigEndian(UInt32 value)
{
if (BitConverter.IsLittleEndian)
{
return swapByteOrder(value);
}
else
{
return value;
}
}
/// <summary>
/// Returns the value encoded in Big Endian (PPC, XDR) format.
/// </summary>
/// <param name="value">Value to encode.</param>
/// <returns>Big-endian encoded value.</returns>
public static Double GetBigEndian(Double value)
{
if (BitConverter.IsLittleEndian)
{
return swapByteOrder(value);
}
else
{
return value;
}
}
/// <summary>
/// Returns the value encoded in Little Endian (x86, NDR) format.
/// </summary>
/// <param name="value">Value to encode.</param>
/// <returns>Little-endian encoded value.</returns>
public static Int32 GetLittleEndian(Int32 value)
{
if (BitConverter.IsLittleEndian)
{
return value;
}
else
{
return swapByteOrder(value);
}
}
/// <summary>
/// Returns the value encoded in Little Endian (x86, NDR) format.
/// </summary>
/// <param name="value">Value to encode.</param>
/// <returns>Little-endian encoded value.</returns>
[CLSCompliant(false)]
public static UInt32 GetLittleEndian(UInt32 value)
{
if (BitConverter.IsLittleEndian)
{
return value;
}
else
{
return swapByteOrder(value);
}
}
/// <summary>
/// Returns the value encoded in Little Endian (x86, NDR) format.
/// </summary>
/// <param name="value">Value to encode.</param>
/// <returns>Little-endian encoded value.</returns>
[CLSCompliant(false)]
public static UInt16 GetLittleEndian(UInt16 value)
{
if (BitConverter.IsLittleEndian)
{
return value;
}
else
{
return swapByteOrder(value);
}
}
/// <summary>
/// Returns the value encoded in Little Endian (x86, NDR) format.
/// </summary>
/// <param name="value">Value to encode.</param>
/// <returns>Little-endian encoded value.</returns>
public static Double GetLittleEndian(Double value)
{
if (BitConverter.IsLittleEndian)
{
return value;
}
else
{
return swapByteOrder(value);
}
}
/// <summary>
/// Swaps the Byte order of an <see cref="Int32"/>.
/// </summary>
/// <param name="value"><see cref="Int32"/> to swap the bytes of.</param>
/// <returns>Byte order swapped <see cref="Int32"/>.</returns>
private static Int32 swapByteOrder(Int32 value)
{
Int32 swapped = (Int32)((0x000000FF) & (value >> 24)
| (0x0000FF00) & (value >> 8)
| (0x00FF0000) & (value << 8)
| (0xFF000000) & (value << 24));
return swapped;
}
/// <summary>
/// Swaps the byte order of a <see cref="UInt16"/>.
/// </summary>
/// <param name="value"><see cref="UInt16"/> to swap the bytes of.</param>
/// <returns>Byte order swapped <see cref="UInt16"/>.</returns>
private static UInt16 swapByteOrder(UInt16 value)
{
return (UInt16)((0x00FF & (value >> 8))
| (0xFF00 & (value << 8)));
}
/// <summary>
/// Swaps the byte order of a <see cref="UInt32"/>.
/// </summary>
/// <param name="value"><see cref="UInt32"/> to swap the bytes of.</param>
/// <returns>Byte order swapped <see cref="UInt32"/>.</returns>
private static UInt32 swapByteOrder(UInt32 value)
{
UInt32 swapped = ( (0x000000FF) & (value >> 24)
| (0x0000FF00) & (value >> 8)
| (0x00FF0000) & (value << 8)
| (0xFF000000) & (value << 24));
return swapped;
}
/// <summary>
/// Swaps the byte order of a <see cref="Int64"/>.
/// </summary>
/// <param name="value"><see cref="Int64"/> to swap the bytes of.</param>
/// <returns>Byte order swapped <see cref="Int64"/>.</returns>
private static Int64 swapByteOrder(Int64 value)
{
UInt64 uvalue = (UInt64)value;
UInt64 swapped = ( (0x00000000000000FF) & (uvalue >> 56)
| (0x000000000000FF00) & (uvalue >> 40)
| (0x0000000000FF0000) & (uvalue >> 24)
| (0x00000000FF000000) & (uvalue >> 8)
| (0x000000FF00000000) & (uvalue << 8)
| (0x0000FF0000000000) & (uvalue << 24)
| (0x00FF000000000000) & (uvalue << 40)
| (0xFF00000000000000) & (uvalue << 56));
return (Int64)swapped;
}
/// <summary>
/// Swaps the byte order of a <see cref="Double"/> (double precision IEEE 754)
/// </summary>
/// <param name="value"><see cref="Double"/> to swap.</param>
/// <returns>Byte order swapped <see cref="Double"/> value.</returns>
private static Double swapByteOrder(Double value)
{
Int64 bits = BitConverter.DoubleToInt64Bits(value);
bits = swapByteOrder(bits);
return BitConverter.Int64BitsToDouble(bits);
}
#endregion
}
}
Return the value of CharSize and display it.
using System;
using System.Text;
class UnicodeEncodingExample {
public static void Main() {
Console.WriteLine(
"Unicode character size: {0} bytes",
UnicodeEncoding.CharSize
);
}
}
Convert a string from one charset to another charset
//http://tinyerp.codeplex.com/
//GNU Library General Public License (LGPL)
//-----------------------------------------------------------------------
// <copyright file="SysUtil.cs" company="Pyramid Consulting">
// Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace Bamboo.Core.Common
{
public class SysUtil
{
/// <summary>
/// Convert a string from one charset to another charset
/// </summary>
/// <param name="strText">source string</param>
/// <param name="strSrcEncoding">original encoding name</param>
/// <param name="strDestEncoding">dest encoding name</param>
/// <returns></returns>
public static String StringEncodingConvert(String strText, String strSrcEncoding, String strDestEncoding)
{
System.Text.Encoding srcEnc = System.Text.Encoding.GetEncoding(strSrcEncoding);
System.Text.Encoding destEnc = System.Text.Encoding.GetEncoding(strDestEncoding);
byte[] bData=srcEnc.GetBytes(strText);
byte[] bResult = System.Text.Encoding.Convert(srcEnc, destEnc, bData);
return destEnc.GetString(bResult);
}
}
}
Write a string to a file using default encoding
//http://tinyerp.codeplex.com/
//GNU Library General Public License (LGPL)
//-----------------------------------------------------------------------
// <copyright file="SysUtil.cs" company="Pyramid Consulting">
// Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace Bamboo.Core.Common
{
public class SysUtil
{
/// <summary>
/// write a string to a file using default encoding
/// </summary>
/// <param name="strFilePath">path of the dest file</param>
/// <param name="strContent">content</param>
/// <returns>return 0 on succeed,otherwise throw a exception</returns>
public static int WriteFile(String strFilePath, String strContent)
{
System.Text.Encoding encDefault=System.Text.Encoding.GetEncoding(0);
System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
try
{
bw.Write(encDefault.GetBytes(strContent));
}
finally
{
bw.Close();
fs.Close();
}
return 0;
}
}
}