Pad data to a WORD. : Encoding « Internationalization I18N « C# / C Sharp






Pad data to a WORD.

  
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace Vestris.ResourceLib
{
    /// <summary>
    /// Resource utilities.
    /// </summary>
    public abstract class ResourceUtil
    {

        /// <summary>
        /// Pad data to a WORD.
        /// </summary>
        /// <param name="w">Binary stream.</param>
        /// <returns>New position within the binary stream.</returns>
        internal static long PadToWORD(BinaryWriter w)
        {
            long pos = w.BaseStream.Position;

            if (pos % 2 != 0)
            {
                long count = 2 - pos % 2;
                Pad(w, (UInt16)count);
                pos += count;
            }

            return pos;
        }

        /// <summary>
        /// Pad bytes.
        /// </summary>
        /// <param name="w">Binary stream.</param>
        /// <param name="len">Number of bytes to write.</param>
        /// <returns>New position within the stream.</returns>
        internal static long Pad(BinaryWriter w, UInt16 len)
        {
            while (len-- > 0)
                w.Write((byte)0);
            return w.BaseStream.Position;
        }
        /// <summary>
        /// Pad data to a DWORD.
        /// </summary>
        /// <param name="w">Binary stream.</param>
        /// <returns>New position within the binary stream.</returns>
        internal static long PadToDWORD(BinaryWriter w)
        {
            long pos = w.BaseStream.Position;

            if (pos % 4 != 0)
            {
                long count = 4 - pos % 4;
                Pad(w, (UInt16)count);
                pos += count;
            }

            return pos;
        }
    }
}

   
    
  








Related examples in the same category

1.Encoding Class represents a character encoding.
2.Encoding.ASCII encoding for the ASCII (7-bit) character set.
3.Gets an encoding for the UTF-16 format that uses the big endian byte order.
4.Read a text file saved with Big Endian Unicode encoding
5.Get byte count and max byte count
6.Display only the encodings that have one or more different names
7.Returns an array that contains all encodings.
8.Returns a sequence of bytes that specifies the encoding used.
9.Gets an encoding for the UTF-32 format using the little endian byte order.
10.Gets an encoding for the UTF-8 format.
11.Gets the Windows operating system code page that most closely corresponds to the current encoding.
12.Petscii Decoder
13.Petscii Encoder
14.extends System.Text.Encoding to create Petscii Encoding
15.SignedLoWord from IntPtr
16.SignedHiWord from IntPtr
17.Returns the high WORD from a DWORD value.
18.Signed Lo Word
19.Signed Hi Word
20.Flip Endian
21.A Big-endian binary writer.
22.Big-endian binary reader
23.EBCDIC To ASCII
24.Is Chinese Character