public static string GetUniqueSequence(this ASCIIEncoding encoding, int?numberOfCharacters = null) { // For the purpose of this example app, ensure numberOfCharacters doesn't exceed GetMaximumCharacterLimit // to ensure all characters are unique (input should be validated instead) int length = numberOfCharacters == null || numberOfCharacters > encoding.GetMaximumCharacterLimit() ? encoding.GetMaximumCharacterLimit() : numberOfCharacters.Value; var byteArray = new byte[encoding.GetMaximumCharacterLimit()]; for (int i = 0; i < length; i++) { byteArray[i] = (byte)i; } return(encoding.GetString(byteArray)); }
public static string GetNonUniqueSequence(this ASCIIEncoding encoding, int?numberOfCharacters = null) { numberOfCharacters = numberOfCharacters <= 1 ? 2 : numberOfCharacters; // Force non unique sequence for purposes of this example (input should be validated instead) int length = numberOfCharacters ?? encoding.GetMaximumCharacterLimit(); int restart = length == 2 ? 0 : length / 2; // Ensures at least half the characters are the same int restartCount = 0; var byteArray = new byte[length]; for (int i = 0; i < length; i++) { byteArray[i] = (byte)restartCount; restartCount = restartCount == restart ? 0 : restartCount + 1; } return(encoding.GetString(byteArray)); }