Теперь запакуем строку.
В этом случае следует передавать тип данных 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
#Sentiment notes that the activity of #BTC discussion on social media has risen to a 4-month high, there is a serious panic among the crowd. Such depressive indicators in the moods usually preceded the rebounds.
⚡️ Sentiment notes a decrease in the number of wallets with a non-zero balance 🪙#BTC, which may mean that some participants will enter the cache before the US elections. The experts of #Sentiment consider this development of the situation to be #bullish for #BTC after the elections are held...
#Sentiment notes a sharp rise in bullish sentiment in #Solana
there are rumors that #Apple (#AAPL) is starting to use the Solana — Sentiment blockchain
🤣Ethereum Fear & Greed Index has dropped to 13, signaling Extreme Fear across the market.
市場情緒跌入 極度恐懼區間,代表短期資金風險偏好明顯下降,投資人情緒趨於保守。
#Crypto#以太坊#币圈#区块链
⚡️ 歷史上極度恐懼區域,往往出現在市場接近階段性底部時,但短線波動仍可能持續。
#Ethereum#Markets#Sentiment
———
👇⭐️👇
🤣留言分享觀點
🥲👇