[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Dotgnu-libs-commits] CVS: dotgnu-base/DotGNU ByteParser.cs,1.1,1.2
From: |
Gopal.V <address@hidden> |
Subject: |
[Dotgnu-libs-commits] CVS: dotgnu-base/DotGNU ByteParser.cs,1.1,1.2 |
Date: |
Sat, 27 Jul 2002 10:30:18 -0400 |
Update of /cvsroot/dotgnu-libs/dotgnu-base/DotGNU
In directory subversions:/tmp/cvs-serv14870/DotGNU
Modified Files:
ByteParser.cs
Log Message:
Bitwise parser
Index: ByteParser.cs
===================================================================
RCS file: /cvsroot/dotgnu-libs/dotgnu-base/DotGNU/ByteParser.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** ByteParser.cs 27 Jul 2002 10:36:01 -0000 1.1
--- ByteParser.cs 27 Jul 2002 14:30:16 -0000 1.2
***************
*** 3,6 ****
--- 3,7 ----
*
* Copyright (C) 2002 Peter Minten
+ * Copyright (C) 2002 Gopal.V
*
* This program is free software; you can redistribute it and/or modify
***************
*** 29,235 ****
public class ByteParser
{
! ///<summary>Parses an 2 bytes array into an UInt16.</summary>
! ///<param name="input">The array to parse.</param>
/// <exception cref="ArgumentOutOfRangeException">
! /// The byte array wasn't 2 elements long.
/// </exception>
- public static UInt16 ParseUInt16(byte[] input)
- {
- if (input.Length != 2)
- throw new ArgumentOutOfRangeException("Invalid byte[]
length");
! UInt16 output = 0;
!
! for (int x = 0; x == 2; x++)
{
! if (input[x] && 0x1)
! output += 2^(0+x*8);
! if (input[x] && 0x2)
! output += 2^(1+x*8);
!
! if (input[x] && 0x3)
! output += 2^(2+x*8);
!
! if (input[x] && 0x4)
! output += 2^(3+x*8);
!
! if (input[x] && 0x5)
! output += 2^(4+x*8);
! if (input[x] && 0x6)
! output += 2^(5+x*8);
!
! if (input[x] && 0x7)
! output += 2^(6+x*8);
!
! if (input[x] && 0x8)
! output += 2^(7+x*8);
! }
return output;
}
! ///<summary>Parses an 4 bytes array into an UInt32.</summary>
! ///<param name="input">The array to parse.</param>
/// <exception cref="ArgumentOutOfRangeException">
! /// The byte array wasn't 4 elements long.
/// </exception>
! public static UInt32 ParseUInt32(byte[] input)
{
! if (input.Length != 4)
! throw new ArgumentOutOfRangeException("Invalid byte[]
length");
! UInt32 output = 0;
!
! //The following basically does the same as the thing in
ParseUInt16
! //but more elegant
! for (int x = 0; x == 4; x++)
! {
! for (int y = 0; x == 8; x++)
! {
! if (input[x] && (y+1))
! output += 2^(y+x*8);
! }
}
return output;
-
}
! ///<summary>Parses an 8 bytes array into an UInt64.</summary>
! ///<param name="input">The array to parse.</param>
/// <exception cref="ArgumentOutOfRangeException">
! /// The byte array wasn't 8 elements long.
! /// </exception>
! public static UInt64 ParseUInt64(byte[] input)
{
! if (input.Length != 8)
! throw new ArgumentOutOfRangeException("Invalid byte[]
length");
! UInt64 output = 0;
! //The following basically does the same as the thing in
ParseInt16
! //but more elegant
! for (int x = 0; x == 4; x++)
! {
! for (int y = 0; x == 8; x++)
! {
! if (input[x] && (y+1))
! output += 2^(y+x*8);
! }
}
return output;
! }
! ///<summary>Parses an 2 bytes array into an Int16.</summary>
///<param name="input">The array to parse.</param>
/// <exception cref="ArgumentOutOfRangeException">
! /// The byte array wasn't 2 elements long.
/// </exception>
- public static Int16 ParseInt16(byte[] input)
- {
- if (input.Length != 2)
- throw new ArgumentOutOfRangeException("Invalid byte[]
length");
! Int16 output = 0;
! //The following basically does the same as the thing in
ParseUInt16
! //but more elegant
! for (int x = 0; x == 2; x++)
! {
! for (int y = 0; x == 8; x++)
! {
! if (x == 1 && y == 8) //The positive/negative
bit
! {
! if (input[x] && (Int16) 0x8000)
! output = Int16.MinValue +
output;
- //else nothing needs to be done
- }
- else
- {
- if (input[x] && (y+1))
- output += 2^(y+x*8);
- }
- }
- }
return output;
! }
! ///<summary>Parses an 4 bytes array into an Int32.</summary>
///<param name="input">The array to parse.</param>
/// <exception cref="ArgumentOutOfRangeException">
! /// The byte array wasn't 4 elements long.
/// </exception>
- public static Int32 ParseInt32(byte[] input)
- {
- if (input.Length != 4)
- throw new ArgumentOutOfRangeException("Invalid byte[]
length");
! Int32 output = 0;
!
! //The following basically does the same as the thing in
ParseUInt16
! //but more elegant
! for (int x = 0; x == 4; x++)
! {
! for (int y = 0; x == 8; x++)
! {
! if (x == 3 && y == 8) //The positive/negative
bit
! {
! if (input[x] && (Int32) 0x80000000)
! output = Int32.MinValue +
output;
!
! //else nothing needs to be done
! }
! else
! {
! if (input[x] && (y+1))
! output += 2^(y+x*8);
! }
! }
}
return output;
! }
!
! ///<summary>Parses an 8 bytes array into an Int64.</summary>
///<param name="input">The array to parse.</param>
/// <exception cref="ArgumentOutOfRangeException">
! /// The byte array wasn't 8 elements long.
/// </exception>
public static Int64 ParseInt64(byte[] input)
{
! if (input.Length != 8)
! throw new ArgumentOutOfRangeException("Invalid byte[]
length");
!
! Int64 output = 0;
!
! //The following basically does the same as the thing in
ParseUInt16
! //but more elegant
! for (int x = 0; x == 8; x++)
! {
! for (int y = 0; x == 8; x++)
! {
! if (x == 7 && y == 8) //The positive/negative
bit
! {
! if (input[x] && (Int64) 0x80000000)
! output = Int64.MinValue +
output;
!
! //else nothing needs to be done
! }
! else
! {
! if (input[x] && (y+1))
! output += 2^(y+x*8);
! }
! }
! }
! return output;
}
!
//The String parsers are located elsewhere but I link to them here to
have
//all byte parser functionality accessible from one place.
--- 30,250 ----
public class ByteParser
{
! ///<summary>Parses an 2 bytes into an UInt16.</summary>
! /// <param name="input"> The byte array data in BigEndian format
</param>
! /// <param name="offset">starting offset of the data in input</param>
/// <exception cref="ArgumentOutOfRangeException">
! /// The offset+2 is greater than the length of the byte array
/// </exception>
! public static UInt16 ParseUInt16(byte[] input,int offset)
! {
! if ((offset+2) > input.Length)
{
! throw new ArgumentOutOfRangeException(
! "Not enough data: offset+2 > Length");
! }
! UInt16 output = (UInt16) ((((UInt16)input[offset]) << 0x08) |
!
((UInt16)input[offset+1]));
! return output;
! }
+ ///<summary>Parses an 2 bytes into an UInt16.</summary>
+ /// <param name="input"> The byte array data in BigEndian format
</param>
+ /// <exception cref="ArgumentOutOfRangeException">
+ /// input is less than 2 bytes long
+ /// </exception>
+
+ public static UInt16 ParseUInt16(byte[] input)
+ {
+ return ParseUInt16(input,0);
+ }
+
+ ///<summary>Parses an 2 bytes into an Int16.</summary>
+ /// <param name="input">The byte array data in BigEndian format </param>
+ /// <param name="offset">starting offset of the data in input</param>
+ /// <exception cref="ArgumentOutOfRangeException">
+ /// The offset+2 is greater than the length of the byte array
+ /// </exception>
+
+ public static Int16 ParseInt16(byte[] input,int offset)
+ {
+ if ((offset+2) > input.Length)
+ {
+ throw new ArgumentOutOfRangeException(
+ "Not enough data: offset+2 > Length");
+ }
+
+ Int16 output = (Int16) ((((Int16)input[offset]) << 0x08) |
+
((Int16)input[offset+1]));
return output;
}
! ///<summary>Parses an 2 bytes into an Int16.</summary>
! /// <param name="input"> The byte array data in BigEndian format
</param>
/// <exception cref="ArgumentOutOfRangeException">
! /// The input is less than 2 bytes long
/// </exception>
!
! public static Int16 ParseInt16(byte[] input)
{
! return ParseInt16(input,0);
! }
! ///<summary>Parses an 4 bytes into an UInt32.</summary>
! ///<param name="input">The byte array in BigEndian format
! /// to parse</param>
! /// <param name="offset">starting offset of the data in input</param>
! /// <exception cref="ArgumentOutOfRangeException">
! /// offset+4 is greater than the length of input
! /// </exception>
!
! public static UInt32 ParseUInt32(byte[] input,int offset)
! {
! if ((offset+4) > input.Length)
! {
! throw new ArgumentOutOfRangeException(
! "Not enough data: offset+4 > Length");
}
+ UInt32 output = (UInt32) (
+ (((UInt32) (input[offset])) << 0x18) |
+ (((UInt32) (input[offset+1])) << 0x10) |
+ (((UInt32) (input[offset+2])) << 0x08) |
+ ((UInt32) (input[offset+3])));
+
return output;
}
! ///<summary>Parses an 4 bytes into an UInt32.</summary>
! ///<param name="input">The byte array in BigEndian format
! /// to parse</param>
/// <exception cref="ArgumentOutOfRangeException">
! /// The input is less than 4 bytes long
! /// </exception>
! public static UInt32 ParseUInt32(byte[] input)
{
! return ParseUInt32(input,0);
! }
! ///<summary>Parses an 4 bytes array into an UInt32.</summary>
! ///<param name="input">The byte array in BigEndian format
! /// to parse</param>
! /// <param name="offset">starting offset of the data in input</param>
! /// <exception cref="ArgumentOutOfRangeException">
! /// The offset+4 is greater than the length of input
! /// </exception>
! public static Int32 ParseInt32(byte[] input,int offset)
! {
! if ((offset+4) > input.Length)
! {
! throw new ArgumentOutOfRangeException(
! "Not enough data: offset+4 > Length");
}
+ Int32 output = (Int32) (
+ (((Int32) (input[offset])) << 0x18) |
+ (((Int32) (input[offset+1])) << 0x10) |
+ (((Int32) (input[offset+2])) << 0x08) |
+ ((Int32) (input[offset+3])));
+
return output;
! }
!
! ///<summary>Parses an 4 bytes array into an UInt32.</summary>
! ///<param name="input">The byte array in BigEndian format
! /// to parse</param>
! /// <exception cref="ArgumentOutOfRangeException">
! /// The input is less than 4 bytes long
! /// </exception>
! public static Int32 ParseInt32(byte[] input)
! {
! return ParseInt32(input,0);
! }
!
! ///<summary>Parses an 8 bytes array into an UInt64.</summary>
///<param name="input">The array to parse.</param>
+ /// <param name="offset">starting offset of the data in input</param>
/// <exception cref="ArgumentOutOfRangeException">
! /// The offset+8 is greater than the length of input
/// </exception>
! public static UInt64 ParseUInt64(byte[] input,int offset)
! {
! if ((offset+8) > input.Length)
! {
! throw new ArgumentOutOfRangeException(
! "Not enough data: offset+8 > Length");
! }
! UInt64 output = (UInt64) (
! (((UInt64) (input[offset])) << 0x38) |
! (((UInt64) (input[offset+1])) << 0x30) |
! (((UInt64) (input[offset+2])) << 0x28) |
! (((UInt64) (input[offset+3])) << 0x20) |
! (((UInt64) (input[offset+4])) << 0x18) |
! (((UInt64) (input[offset+5])) << 0x10) |
! (((UInt64) (input[offset+6])) << 0x08) |
! ((UInt64) (input[offset+7])) );
return output;
! }
!
! ///<summary>Parses an 8 bytes array into an UInt64.</summary>
! ///<param name="input">The array to parse.</param>
! /// <exception cref="ArgumentOutOfRangeException">
! /// The input is less than 8 bytes long
! /// </exception>
+ public static UInt64 ParseUInt64(byte[] input)
+ {
+ return ParseUInt64(input,0);
+ }
! ///<summary>Parses an 8 bytes array into an Int64.</summary>
///<param name="input">The array to parse.</param>
+ /// <param name="offset">starting offset of the data in input</param>
/// <exception cref="ArgumentOutOfRangeException">
! /// The offset+8 is greater than the length of input
/// </exception>
! public static Int64 ParseInt64(byte[] input,int offset)
! {
! if ((offset+8) > input.Length)
! {
! throw new ArgumentOutOfRangeException(
! "Not enough data: offset+8 > Length");
}
+
+ Int64 output = (Int64) (
+ (((Int64) (input[offset])) << 0x38) |
+ (((Int64) (input[offset+1])) << 0x30) |
+ (((Int64) (input[offset+2])) << 0x28) |
+ (((Int64) (input[offset+3])) << 0x20) |
+ (((Int64) (input[offset+4])) << 0x18) |
+ (((Int64) (input[offset+5])) << 0x10) |
+ (((Int64) (input[offset+6])) << 0x08) |
+ ((Int64) (input[offset+7])) );
return output;
! }
! ///<summary>Parses an 8 bytes array into an Int64.</summary>
///<param name="input">The array to parse.</param>
/// <exception cref="ArgumentOutOfRangeException">
! /// The input is less than 8 bytes long
/// </exception>
+
public static Int64 ParseInt64(byte[] input)
{
! return ParseInt64(input,0);
}
!
! // Remove this when it is fully implemented
! #if ALL_IMPLEMENTED
!
//The String parsers are located elsewhere but I link to them here to
have
//all byte parser functionality accessible from one place.
***************
*** 248,255 ****
{
return encoding.GetBytes(input);
! }
!
}; // class ByteParser
}; // namespace DotGNU
!
\ No newline at end of file
--- 263,271 ----
{
return encoding.GetBytes(input);
! }
!
! #endif
}; // class ByteParser
}; // namespace DotGNU
!
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Dotgnu-libs-commits] CVS: dotgnu-base/DotGNU ByteParser.cs,1.1,1.2,
Gopal.V <address@hidden> <=