Exemplo n.º 1
0
        public string ReadPaddedStringShiftJIS(int paddedRegionLength, byte?padding)
        {
            byte[] data        = ReadBytes(paddedRegionLength);
            int    strEndIndex = -1;

            for (int i = 0; i < data.Length; i++)
            {
                if (data[i] == 0)
                {
                    strEndIndex = i;
                    break;
                }
            }

            // String has null-terminator in it
            if (strEndIndex > 0)
            {
                return(ShiftJISEncoding.GetString(data, 0, strEndIndex));
            }
            // String begins with null-terminator
            else if (strEndIndex == 0)
            {
                // If string ends on very first byte, there's no real point to
                // running it through the encoding and all that.
                return(string.Empty);
            }
            // String has no null-terminator
            else
            {
                return(ShiftJISEncoding.GetString(data));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads a Shift-JIS string.
        /// </summary>
        /// <returns>A Shift-JIS string.</returns>
        public string ReadStringShiftJIS(int specificLength = -1, bool stopOnTerminator = true)
        {
            if (!stopOnTerminator)
            {
                return(ShiftJISEncoding.GetString(ReadBytes(specificLength).ToArray()));
            }

            List <byte> shiftJisData = new List <byte>();

            byte nextByte = 0;

            while (specificLength < 0 || shiftJisData.Count < specificLength)
            {
                nextByte = ReadByte();

                if (stopOnTerminator && nextByte == 0)
                {
                    break;
                }

                shiftJisData.Add(nextByte);
            }

            if (shiftJisData.Count == 0)
            {
                return("");
            }

            return(ShiftJISEncoding.GetString(shiftJisData.ToArray()));
        }
Exemplo n.º 3
0
        public void WritePaddedStringShiftJIS(string str, int paddedRegionLength, byte?padding, bool forceTerminateAtMaxLength = false)
        {
            byte[] jis      = ShiftJISEncoding.GetBytes(str);
            int    origSize = jis.Length;

            Array.Resize(ref jis, paddedRegionLength);

            if (paddedRegionLength > origSize)
            {
                if (padding.HasValue)
                {
                    // Start at [origSize + 1] because [origSize] is the null-terminator
                    for (int i = origSize + 1; i < paddedRegionLength; i++)
                    {
                        jis[i] = padding.Value;
                    }
                }
            }
            else if (paddedRegionLength < origSize && forceTerminateAtMaxLength)
            {
                jis[jis.Length - 1] = 0;
            }

            Write(jis);
        }
Exemplo n.º 4
0
        public void WriteMtdName(string name, byte delim)
        {
            byte[] shift_jis = ShiftJISEncoding.GetBytes(name);

            Write(shift_jis.Length);

            Write(shift_jis);

            WriteDelimiter(delim);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Writes a Shift-JIS string.
        /// </summary>
        /// <param name="str">The string to write.</param>
        /// /// <param name="terminate">Whether to append a string terminator character of value 0 to the end of the written string.</param>
        public void WriteStringShiftJIS(string str, bool terminate)
        {
            byte[] b = ShiftJISEncoding.GetBytes(str);
            if (terminate)
            {
                Array.Resize(ref b, b.Length + 1);
            }

            Write(b);
        }