Теперь запакуем строку.
В этом случае следует передавать тип данных bytes.
>>> struct.pack('=s', b'a')
b'a'
Для записи слова следует указывать количество символов.
>>> struct.pack('=5s', b'hello')
b'hello'
Кстати, запакованный вид соответствует исходному тексту. Всё верно, символ есть в таблице ASCII, то есть его код попадает в диапазон 0-127, он может быть записан одним байтом и имеет визуальное представление. А вот что будет если добавить символ вне ASCII
>>> struct.pack(f'=s', b'ё')
SyntaxError: bytes can only contain ASCII literal characters.
Ошибка возникла еще на этапе создания объекта bytes, который не может содержать такой символ. Поэтому надо кодировать эти байты из строки.
>>> enc = 'ёжик'.encode('utf-8')
>>> struct.pack(f'={len(enc)}s', enc)
b'\xd1\x91\xd0\xb6\xd0\xb8\xd0\xba'
Заметьте, длина такой строки в байтах отличается от исходной длины, так как символы вне ASCII записываются двумя байтами и более. Поэтому здесь формат создаём на лету, используя получившуюся длину как каунтер токена.
#libs#basic
Hacker AI
Provides advanced AI and integrated tools to help security teams conduct comprehensive penetration tests effortlessly. Scan, exploit, and analyze web applications, networks, and cloud environments with ease and precision, without needing expert skills.
🔗 Links:
- Website
- Source code
❤️ Support the Project
If this project makes your life easier, here are a few quick ways to show some love:
⭐ Star the repo/app
☕ Buy a coffee for the developer
🛠 Contribute code, issues, or pull-requests
🏷 Tags: #AI#Website
🔥 DeckForge Official Website is LIVE!
Our official website is now online — the first step toward building a full digital home for the entire DeckForge community.
👉https://play.deckforge.net/
Over the coming weeks, we’ll be expanding it with new sections, guides, lore, developer updates, partner announcements & exclusive content that will grow together with the game and our universe.
More updates are coming. Stay Forged!
#DeckForge#Website#OpenBeta
Join the DeckForge Community:
🌐YouTube𝕏Twitter🎮Discord
👉Chat👉Announcements👉DeckForge