pyrit.prompt_converter.BinaryConverter#

class BinaryConverter(*, bits_per_char: BitsPerChar = BitsPerChar.BITS_16, word_selection_strategy: WordSelectionStrategy | None = None)[source]#

Bases: WordLevelConverter

Transforms input text into its binary representation with configurable bits per character (8, 16, or 32).

__init__(*, bits_per_char: BitsPerChar = BitsPerChar.BITS_16, word_selection_strategy: WordSelectionStrategy | None = None)[source]#

Initialize the converter with the specified bits per character and selection strategy.

Parameters:
  • bits_per_char (BinaryConverter.BitsPerChar) – Number of bits to use for each character (8, 16, or 32). Default is 16 bits.

  • word_selection_strategy (Optional[WordSelectionStrategy]) – Strategy for selecting which words to convert. If None, all words will be converted.

Raises:

TypeError – If bits_per_char is not an instance of BinaryConverter.BitsPerChar Enum.

Methods

__init__(*[, bits_per_char, ...])

Initialize the converter with the specified bits per character and selection strategy.

convert_async(*, prompt[, input_type])

Convert the given prompt into the target format supported by the converter.

convert_tokens_async(*, prompt[, ...])

Convert substrings within a prompt that are enclosed by specified start and end tokens.

convert_word_async(word)

Convert a single word into the target format supported by the converter.

get_identifier()

Get the typed identifier for this object.

input_supported(input_type)

Check if the input type is supported by the converter.

join_words(words)

Join the converted words with the binary representation of a space.

output_supported(output_type)

Check if the output type is supported by the converter.

validate_input(prompt)

Check if bits_per_char is sufficient for the characters in the prompt.

Attributes

SUPPORTED_INPUT_TYPES

Tuple of input modalities supported by this converter.

SUPPORTED_OUTPUT_TYPES

Tuple of output modalities supported by this converter.

supported_input_types

Returns a list of supported input types for the converter.

supported_output_types

Returns a list of supported output types for the converter.

class BitsPerChar(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: Enum

The number of bits per character for binary conversion.

BITS_16 = 16#

16 bits per character, suitable for Unicode characters.

BITS_32 = 32#

32 bits per character, suitable for extended Unicode characters.

BITS_8 = 8#

8 bits per character, suitable for ASCII characters.

async convert_word_async(word: str) str[source]#

Convert a single word into the target format supported by the converter.

Parameters:

word (str) – The word to be converted.

Returns:

The converted word.

Return type:

str

join_words(words: list[str]) str[source]#

Join the converted words with the binary representation of a space.

Parameters:

words (list[str]) – The list of converted words.

Returns:

The final joined string with spaces in binary format.

Return type:

str

validate_input(prompt: str) None[source]#

Check if bits_per_char is sufficient for the characters in the prompt.

Parameters:

prompt (str) – The input text prompt to validate.

Raises:

ValueError – If bits_per_char is too small to represent any character in the prompt.