/// <summary> /// Formats a number read from the input record as a string and writes it in the output record. /// </summary> /// <param name="input">the input record</param> /// <param name="output">the output record</param> public void Format(byte[] input, byte[] output) { var value = Accessor.Get(input); var digitStack = new DigitStack(value); var positive = value >= 0; var sb = new StringBuilder(); // characters are looped from last to first for (var i = Edit.Length - 1; i >= 0; i--) { var c = Edit[i]; switch (c) { case InsignificantDigit: case SignificantDigit: var d = digitStack.Pop(); if (c == SignificantDigit || d != 0) { if (d == 0) { d = '0'; } sb.Insert(0, d); } break; case Sign: sb.Insert(0, GetSign(positive, sb.Length > 0)); break; case 'C': case 'R': case 'D': case 'B': // "CR" (for credit) or "DB" (for debit) at the end is copied if // the number is negative, or replaced whitespaces otherwise sb.Insert(0, positive ? ' ' : c); break; default: // Other characters are only printed if more digits will be printed if (digitStack.HasMoreDigits || HasMoreSignificantDigits(i)) { sb.Insert(0, c); } break; } } while (sb.Length < Length) { sb.Insert(0, ' '); } Buffer.BlockCopy(Encoding.GetBytes(sb.ToString()), 0, output, OutputIndex, Length); }