/*
* description: "Provides methodes to encode and decode integers with fragments flags."
* date: "$Date: 2009-05-15 15:41:52 -0700 (Fri, 15 May 2009) $"
* revision: "$Revision: 78721 $"
* copyright: "Copyright (c) 1985-2007, Eiffel Software."
* license: "GPL version 2 see http://www.eiffel.com/licensing/gpl.txt)"
* licensing_options: "Commercial license is available at http://www.eiffel.com/licensing"
* copying: ""
* source: "[
* Eiffel Software
* 5949 Hollister Ave #B, Goleta, CA 93117
* Telephone 805-685-1006, Fax 805-685-6869
* Website http://www.eiffel.com
* Customer support http://support.eiffel.com
* ]"
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace Xebra
{
///
/// Provides methodes to encode and decode integers with fragments flags.
///
class XEncoder
{
///
/// Converts an array of byte to integer32
///
///
///
public static int byteArrayToInt(byte[] b)
{
return System.BitConverter.ToInt32(b, 0);
}
///
/// Converts an int32 to a byte array
///
///
///
public static byte[] intToByteArray(int i)
{
return System.BitConverter.GetBytes(i);
}
///
/// Encodes an unsigned int to include a flag
///
/// The unsigned int
/// The flag
/// The encoded unsigned int
public static uint encodeNatural(uint i, uint flag)
{
if (i > 0x7FFFFFFF)
{
return 0;
}
else
{
return (uint)((i << 1) + flag);
}
}
///
/// Extracts the unsigned int from an encoded unsigned int
///
/// The encoded unsigned int
/// The decoded unsigned int
public static uint decodeNatural(uint i)
{
return (i >> 1);
}
///
/// Extracts a flag from an unsigned int
///
/// The encoded unsigned int
/// The flag
public static bool decodeFlag(uint i)
{
return ((i & (uint)1) == 1);
}
}
}