[討論] 操作Byte
小的最近在寫網路程式
由於Server端的要求 要我在傳過去的Json資料前加個Header
Header的格式是:
1byte(起頭) + 4byte(資料長度) + Json資料
ex.
0xBE + 0x00000008 + '{test:0}'
於是我寫了一個這樣的function來取得Header
// Get Header by message
function GetHeader($msg)
{
// startByte lenBytes(4) data
// 0 1 2 3 4 5 ...
// 0xBE . 0x00000000 . '{ ... }'
$startByte = chr(0xBE);
$lenBytes = '';
$lenBytesLength = 4; // Length of lenBytes
$tempValue = strlen($msg);
for ($i = 0; $i < $lenBytesLength; $i++)
{
$lenBytes = chr($tempValue & 0xFF) . $lenBytes;
$tempValue = floor($tempValue >> 8);
}
return $startByte . $lenBytes;
}
目前正常work
但因為我接觸PHP不久 在查資料的過程中也有發現像pack這樣相關的function
但不太會用 所以想請教有經驗的人
像這類操作byte傳輸資料的動作 有沒有更好更適合的做法?
非常感謝~
--
◢ . ______ ◣ ◣ ◢ ◣ ◢ ◢ ◥█◣▲◢█◤ ◣
◥ ◥ ● │GRAVEYARD. \‧◣ ◥ █◣◢█ ◤ ◢ ◢◤◥█◤◥◣ ◤ ◤
' \ BILE DEMON│ '◣ ◥◥▌◤◤ ◢ ◢▌ ◣▽◢▌ ◣
▄ ▄ ̄ ̄ ̄ ̄ ̄ ̄ ◤ ◢ █▌ ◣ ◥ ▎ ◥▌ ◤ ▊
◢◢ ▌ //\ ●● ◣◥◥ ◤◥ ◤◤◢ ◥◥ ︵ ◤◤ ◣◣
◥ ▃▇ ▆◣▂◢▂▅≡█▄ ◣ USHER ◢ Ⅲ◥◢▅◣◤Ⅲ ◤
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 175.181.118.207
※ 編輯: usherII 來自: 175.181.118.207 (07/13 01:30)
→
07/13 09:15, , 1F
07/13 09:15, 1F
→
07/13 09:16, , 2F
07/13 09:16, 2F
→
07/13 15:07, , 3F
07/13 15:07, 3F
→
07/13 15:19, , 4F
07/13 15:19, 4F
→
07/13 15:58, , 5F
07/13 15:58, 5F
→
07/13 15:59, , 6F
07/13 15:59, 6F
→
07/13 16:38, , 7F
07/13 16:38, 7F
推
07/17 15:34, , 8F
07/17 15:34, 8F
→
07/17 15:35, , 9F
07/17 15:35, 9F
→
07/17 15:35, , 10F
07/17 15:35, 10F
→
07/17 15:37, , 11F
07/17 15:37, 11F
→
07/17 15:38, , 12F
07/17 15:38, 12F
PHP 近期熱門文章
PTT數位生活區 即時熱門文章
4
18
6
15