Bytestring to normal chars. python's struct.pack

113 Views Asked by At

I've this python code that generates bytes

import threading
import socket
import struct
import time
from datetime import datetime
import random

TICKERS = ["AAPL", "GOOG", "MSFT", "SPY"]

timestamp = datetime.utcnow()
ticker = random.choice(TICKERS)
price = 90 + random.randrange(0, 400) * 0.05
size = random.randrange(100, 10000, 100)
msg = struct.pack("!QH%dsdI" % len(ticker), timestamp_millis(timestamp), len(ticker), ticker.encode("ascii"), price, size)
msg_len = struct.pack("!H", len(msg))
print("[%s:%d] %s: %s %.2f %d" % (host, port, timestamp, ticker, price, size))
client_socket.send(msg_len + msg)

I'm listening on port that generates this data with akka io. I get data like this:

client received some data: ByteString(0, 26, 0, 0, 1, 90, -30, -44, -92, 21, 0, 4, 71, 79, 79, 71, 64, 88, -106, 102, 102, 102, 102, 102, 0, 0, 10, -116)
client received some data: ByteString(0, 26, 0, 0, 1, 90, -30, -44, -86, -75, 0, 4, 77, 83, 70, 84, 64, 88, -26, 102, 102, 102, 102, 102, 0, 0, 8, -104)
client received some data: ByteString(0, 26, 0, 0, 1, 90, -30, -44, -79, -59, 0, 4, 71, 79, 79, 71, 64, 87, 86, 102, 102, 102, 102, 102, 0, 0, 33, -4)
client received some data: ByteString(0, 25, 0, 0, 1, 90, -30, -44, -69, -126, 0, 3, 83, 80, 89, 64, 88, -29, 51, 51, 51, 51, 51, 0, 0, 0, -56)

I've several questions: 1. How can i decode this bytes to normal chars? I try:

scala> val bs = ByteString(0, 26, 0, 0, 1, 90, -30, -43, 2, -70, 0, 4, 65, 65, 80, 76, 64, 88, -48, 0, 0, 0, 0, 0, 0, 0, 26, 44)
bs: akka.util.ByteString = ByteString(0, 26, 0, 0, 1, 90, -30, -43, 2, -70, 0, 4, 65, 65, 80, 76, 64, 88, -48, 0, 0, 0, 0, 0, 0, 0, 26, 44)
scala> bs.utf8String
res3: String = ?????Z��?�??AAPL@X�????????,

2. What's !QH%dsdI? And how struct.pack works? I don't know python

1

There are 1 best solutions below

0
Satish Prakash Garg On

You can use method called decodeString like this :

scala> val bs = ByteString(0, 26, 0, 0, 1, 90, -30, -43, 2, -70, 0, 4, 65, 65, 80, 76, 64, 88, -48, 0, 0, 0, 0, 0, 0, 0, 26, 44)
bs: akka.util.ByteString = ByteString(0, 26, 0, 0, 1, 90, -30, -43, 2, -70, 0, 4, 65, 65, 80, 76, 64, 88, -48, 0, 0, 0, 0, 0, 0, 0, 26, 44)

scala> bs.decodeString("US-ASCII")

!QH%dsdI is a format string that is that are built up from Format Characters, which specify the type of data being packed/unpacked.

You can refer to this link for reference on struct.pack

Note that the arguments must match the values required by the format exactly.