Example #1
0
        unsafe public static void WriteASCIIString(FileWriter writer, string value, int length, bool isLastNull)
        {
            byte[] bytes = new byte[length];
            fixed(byte *pBytes = bytes)
            {
                if (value != null)
                {
                    char[] chars      = value.ToCharArray();
                    int    copyLength = chars.Length;
                    if (copyLength > 0)
                    {
                        if (copyLength > length)
                        {
                            copyLength = length;
                        }

                        fixed(char *pChars = chars)
                        {
                            for (int i = 0; i != copyLength; ++i)
                            {
                                switch (pChars[i])
                                {
                                case ' ': pBytes[i] = 0x20; break;

                                case '!': pBytes[i] = 0x21; break;

                                case '\"': pBytes[i] = 0x22; break;

                                case '#': pBytes[i] = 0x23; break;

                                case '$': pBytes[i] = 0x24; break;

                                case '%': pBytes[i] = 0x25; break;

                                case '&': pBytes[i] = 0x26; break;

                                case '\'': pBytes[i] = 0x27; break;

                                case '(': pBytes[i] = 0x28; break;

                                case ')': pBytes[i] = 0x29; break;

                                case '*': pBytes[i] = 0x2A; break;

                                case '+': pBytes[i] = 0x2B; break;

                                case ',': pBytes[i] = 0x2C; break;

                                case '-': pBytes[i] = 0x2D; break;

                                case '.': pBytes[i] = 0x2E; break;

                                case '/': pBytes[i] = 0x2F; break;

                                case '0': pBytes[i] = 0x30; break;

                                case '1': pBytes[i] = 0x31; break;

                                case '2': pBytes[i] = 0x32; break;

                                case '3': pBytes[i] = 0x33; break;

                                case '4': pBytes[i] = 0x34; break;

                                case '5': pBytes[i] = 0x35; break;

                                case '6': pBytes[i] = 0x36; break;

                                case '7': pBytes[i] = 0x37; break;

                                case '8': pBytes[i] = 0x38; break;

                                case '9': pBytes[i] = 0x39; break;

                                case ':': pBytes[i] = 0x3A; break;

                                case ';': pBytes[i] = 0x3B; break;

                                case '<': pBytes[i] = 0x3C; break;

                                case '=': pBytes[i] = 0x3D; break;

                                case '>': pBytes[i] = 0x3E; break;

                                case '?': pBytes[i] = 0x3F; break;

                                case '@': pBytes[i] = 0x40; break;

                                case 'A': pBytes[i] = 0x41; break;

                                case 'B': pBytes[i] = 0x42; break;

                                case 'C': pBytes[i] = 0x43; break;

                                case 'D': pBytes[i] = 0x44; break;

                                case 'E': pBytes[i] = 0x45; break;

                                case 'F': pBytes[i] = 0x46; break;

                                case 'G': pBytes[i] = 0x47; break;

                                case 'H': pBytes[i] = 0x48; break;

                                case 'I': pBytes[i] = 0x49; break;

                                case 'J': pBytes[i] = 0x4A; break;

                                case 'K': pBytes[i] = 0x4B; break;

                                case 'L': pBytes[i] = 0x4C; break;

                                case 'M': pBytes[i] = 0x4D; break;

                                case 'N': pBytes[i] = 0x4E; break;

                                case 'O': pBytes[i] = 0x4F; break;

                                case 'P': pBytes[i] = 0x50; break;

                                case 'Q': pBytes[i] = 0x51; break;

                                case 'R': pBytes[i] = 0x52; break;

                                case 'S': pBytes[i] = 0x53; break;

                                case 'T': pBytes[i] = 0x54; break;

                                case 'U': pBytes[i] = 0x55; break;

                                case 'V': pBytes[i] = 0x56; break;

                                case 'W': pBytes[i] = 0x57; break;

                                case 'X': pBytes[i] = 0x58; break;

                                case 'Y': pBytes[i] = 0x59; break;

                                case 'Z': pBytes[i] = 0x5A; break;

                                case '[': pBytes[i] = 0x5B; break;

                                case '\\': pBytes[i] = 0x5C; break;

                                case ']': pBytes[i] = 0x5D; break;

                                case '^': pBytes[i] = 0x5E; break;

                                case '_': pBytes[i] = 0x5F; break;

                                case '`': pBytes[i] = 0x60; break;

                                case 'a': pBytes[i] = 0x61; break;

                                case 'b': pBytes[i] = 0x62; break;

                                case 'c': pBytes[i] = 0x63; break;

                                case 'd': pBytes[i] = 0x64; break;

                                case 'e': pBytes[i] = 0x65; break;

                                case 'f': pBytes[i] = 0x66; break;

                                case 'g': pBytes[i] = 0x67; break;

                                case 'h': pBytes[i] = 0x68; break;

                                case 'i': pBytes[i] = 0x69; break;

                                case 'j': pBytes[i] = 0x6A; break;

                                case 'k': pBytes[i] = 0x6B; break;

                                case 'l': pBytes[i] = 0x6C; break;

                                case 'm': pBytes[i] = 0x6D; break;

                                case 'n': pBytes[i] = 0x6E; break;

                                case 'o': pBytes[i] = 0x6F; break;

                                case 'p': pBytes[i] = 0x70; break;

                                case 'q': pBytes[i] = 0x71; break;

                                case 'r': pBytes[i] = 0x72; break;

                                case 's': pBytes[i] = 0x73; break;

                                case 't': pBytes[i] = 0x74; break;

                                case 'u': pBytes[i] = 0x75; break;

                                case 'v': pBytes[i] = 0x76; break;

                                case 'w': pBytes[i] = 0x77; break;

                                case 'x': pBytes[i] = 0x78; break;

                                case 'y': pBytes[i] = 0x79; break;

                                case 'z': pBytes[i] = 0x7A; break;

                                case '{': pBytes[i] = 0x7B; break;

                                case '|': pBytes[i] = 0x7C; break;

                                case '}': pBytes[i] = 0x7D; break;

                                case '~': pBytes[i] = 0x7E; break;

                                case 'Ђ': pBytes[i] = 0x80; break;

                                case 'Ѓ': pBytes[i] = 0x81; break;

                                case '‚': pBytes[i] = 0x82; break;

                                case 'ѓ': pBytes[i] = 0x83; break;

                                case '„': pBytes[i] = 0x84; break;

                                case '…': pBytes[i] = 0x85; break;

                                case '†': pBytes[i] = 0x86; break;

                                case '‡': pBytes[i] = 0x87; break;

                                case '€': pBytes[i] = 0x88; break;

                                case '‰': pBytes[i] = 0x89; break;

                                case 'Љ': pBytes[i] = 0x8A; break;

                                case '‹': pBytes[i] = 0x8B; break;

                                case 'Њ': pBytes[i] = 0x8C; break;

                                case 'Ќ': pBytes[i] = 0x8D; break;

                                case 'Ћ': pBytes[i] = 0x8E; break;

                                case 'Џ': pBytes[i] = 0x8F; break;

                                case 'ђ': pBytes[i] = 0x90; break;

                                case '‘': pBytes[i] = 0x91; break;

                                case '’': pBytes[i] = 0x92; break;

                                case '“': pBytes[i] = 0x93; break;

                                case '”': pBytes[i] = 0x94; break;

                                case '•': pBytes[i] = 0x95; break;

                                case '–': pBytes[i] = 0x96; break;

                                case '—': pBytes[i] = 0x97; break;

                                case '™': pBytes[i] = 0x99; break;

                                case 'љ': pBytes[i] = 0x9A; break;

                                case '›': pBytes[i] = 0x9B; break;

                                case 'њ': pBytes[i] = 0x9C; break;

                                case 'ќ': pBytes[i] = 0x9D; break;

                                case 'ћ': pBytes[i] = 0x9E; break;

                                case 'џ': pBytes[i] = 0x9F; break;

                                case 'Ў': pBytes[i] = 0xA1; break;

                                case 'ў': pBytes[i] = 0xA2; break;

                                case 'Ј': pBytes[i] = 0xA3; break;

                                case '¤': pBytes[i] = 0xA4; break;

                                case 'Ґ': pBytes[i] = 0xA5; break;

                                case '¦': pBytes[i] = 0xA6; break;

                                case '§': pBytes[i] = 0xA7; break;

                                case 'Ё': pBytes[i] = 0xA8; break;

                                case '©': pBytes[i] = 0xA9; break;

                                case 'Є': pBytes[i] = 0xAA; break;

                                case '«': pBytes[i] = 0xAB; break;

                                case '¬': pBytes[i] = 0xAC; break;

                                case '®': pBytes[i] = 0xAE; break;

                                case 'Ї': pBytes[i] = 0xAF; break;

                                case '°': pBytes[i] = 0xB0; break;

                                case '±': pBytes[i] = 0xB1; break;

                                case 'І': pBytes[i] = 0xB2; break;

                                case 'і': pBytes[i] = 0xB3; break;

                                case 'ґ': pBytes[i] = 0xB4; break;

                                case 'µ': pBytes[i] = 0xB5; break;

                                case '¶': pBytes[i] = 0xB6; break;

                                case '·': pBytes[i] = 0xB7; break;

                                case 'ё': pBytes[i] = 0xB8; break;

                                case '№': pBytes[i] = 0xB9; break;

                                case 'є': pBytes[i] = 0xBA; break;

                                case '»': pBytes[i] = 0xBB; break;

                                case 'ј': pBytes[i] = 0xBC; break;

                                case 'Ѕ': pBytes[i] = 0xBD; break;

                                case 'ѕ': pBytes[i] = 0xBE; break;

                                case 'ї': pBytes[i] = 0xBF; break;

                                case 'А': pBytes[i] = 0xC0; break;

                                case 'Б': pBytes[i] = 0xC1; break;

                                case 'В': pBytes[i] = 0xC2; break;

                                case 'Г': pBytes[i] = 0xC3; break;

                                case 'Д': pBytes[i] = 0xC4; break;

                                case 'Е': pBytes[i] = 0xC5; break;

                                case 'Ж': pBytes[i] = 0xC6; break;

                                case 'З': pBytes[i] = 0xC7; break;

                                case 'И': pBytes[i] = 0xC8; break;

                                case 'Й': pBytes[i] = 0xC9; break;

                                case 'К': pBytes[i] = 0xCA; break;

                                case 'Л': pBytes[i] = 0xCB; break;

                                case 'М': pBytes[i] = 0xCC; break;

                                case 'Н': pBytes[i] = 0xCD; break;

                                case 'О': pBytes[i] = 0xCE; break;

                                case 'П': pBytes[i] = 0xCF; break;

                                case 'Р': pBytes[i] = 0xD0; break;

                                case 'С': pBytes[i] = 0xD1; break;

                                case 'Т': pBytes[i] = 0xD2; break;

                                case 'У': pBytes[i] = 0xD3; break;

                                case 'Ф': pBytes[i] = 0xD4; break;

                                case 'Х': pBytes[i] = 0xD5; break;

                                case 'Ц': pBytes[i] = 0xD6; break;

                                case 'Ч': pBytes[i] = 0xD7; break;

                                case 'Ш': pBytes[i] = 0xD8; break;

                                case 'Щ': pBytes[i] = 0xD9; break;

                                case 'Ъ': pBytes[i] = 0xDA; break;

                                case 'Ы': pBytes[i] = 0xDB; break;

                                case 'Ь': pBytes[i] = 0xDC; break;

                                case 'Э': pBytes[i] = 0xDD; break;

                                case 'Ю': pBytes[i] = 0xDE; break;

                                case 'Я': pBytes[i] = 0xDF; break;

                                case 'а': pBytes[i] = 0xE0; break;

                                case 'б': pBytes[i] = 0xE1; break;

                                case 'в': pBytes[i] = 0xE2; break;

                                case 'г': pBytes[i] = 0xE3; break;

                                case 'д': pBytes[i] = 0xE4; break;

                                case 'е': pBytes[i] = 0xE5; break;

                                case 'ж': pBytes[i] = 0xE6; break;

                                case 'з': pBytes[i] = 0xE7; break;

                                case 'и': pBytes[i] = 0xE8; break;

                                case 'й': pBytes[i] = 0xE9; break;

                                case 'к': pBytes[i] = 0xEA; break;

                                case 'л': pBytes[i] = 0xEB; break;

                                case 'м': pBytes[i] = 0xEC; break;

                                case 'н': pBytes[i] = 0xED; break;

                                case 'о': pBytes[i] = 0xEE; break;

                                case 'п': pBytes[i] = 0xEF; break;

                                case 'р': pBytes[i] = 0xF0; break;

                                case 'с': pBytes[i] = 0xF1; break;

                                case 'т': pBytes[i] = 0xF2; break;

                                case 'у': pBytes[i] = 0xF3; break;

                                case 'ф': pBytes[i] = 0xF4; break;

                                case 'х': pBytes[i] = 0xF5; break;

                                case 'ц': pBytes[i] = 0xF6; break;

                                case 'ч': pBytes[i] = 0xF7; break;

                                case 'ш': pBytes[i] = 0xF8; break;

                                case 'щ': pBytes[i] = 0xF9; break;

                                case 'ъ': pBytes[i] = 0xFA; break;

                                case 'ы': pBytes[i] = 0xFB; break;

                                case 'ь': pBytes[i] = 0xFC; break;

                                case 'э': pBytes[i] = 0xFD; break;

                                case 'ю': pBytes[i] = 0xFE; break;

                                case 'я': pBytes[i] = 0xFF; break;

                                default: pBytes[i] = 0x20; break;
                                }
                            }
                        }

                        if (copyLength < length)
                        {
                            for (int i = copyLength; i != length; ++i)
                            {
                                pBytes[i] = 0;
                            }
                        }
                    }
                }
                if (isLastNull)
                {
                    pBytes[length - 1] = 0;
                }
            }

            writer.Write(bytes, 0, length);
        }