我需要將整數轉換爲8bit,但是pack只能轉換爲16bit的二進制數據。 我在谷歌搜索,不能得到anthing。 有人可以幫忙嗎?如何通過使用PHP將整數轉換爲8位二進制數據?
3
A
回答
3
您可以使用c或C來做到這一點。像:
pack('C', 234);
c是有符號的,C是無符號的。
+0
c是char,是否可以這樣使用? –
+0
是的!!!它工作!謝謝! –
5
echo $test = pack('C', 1);
包根據格式給出參數轉換成二進制字符串。
這個函數的思想來自Perl,所有格式化代碼的工作原理與Perl中的一樣。但是,有一些缺少的格式代碼,例如Perl的「u」格式代碼。
注意符號和無符號值之間的區別隻影響函數解包(),其中爲功能包()給出了符號和無符號格式代碼
代碼都是相同的結果
a NUL-padded string
A SPACE-padded string
h Hex string, low nibble first
H Hex string, high nibble first
c signed char
C unsigned char
s signed short (always 16 bit, machine byte order)
S unsigned short (always 16 bit, machine byte order)
n unsigned short (always 16 bit, big endian byte order)
v unsigned short (always 16 bit, little endian byte order)
i signed integer (machine dependent size and byte order)
I unsigned integer (machine dependent size and byte order)
l signed long (always 32 bit, machine byte order)
L unsigned long (always 32 bit, machine byte order)
N unsigned long (always 32 bit, big endian byte order)
V unsigned long (always 32 bit, little endian byte order)
f float (machine dependent size and representation)
d double (machine dependent size and representation)
x NUL byte
X Back up one byte
@ NUL-fill to absolute position
3
爲了得到正確的格式,並添加前導零這樣的00000001 使用這樣的:
<?php
// Add leading zeros
$bin = sprintf("%08d", decbin(26)); // "00011010"
?>
相關問題
- 1. 使用整數將8位二進制轉換爲BCD
- 2. 如何將8位整數轉換爲xquery中的二進制字符串?
- 3. 使用二進制將整數轉換爲過量表示法
- 4. 將二進制轉換爲整數
- 5. 如何將二進制轉換爲十進制與長整數?
- 6. 將二進制整數向量轉換爲向量位數
- 7. 如何使用C和GMP_Bignum將二進制原始數據轉換爲整數
- 8. 將二進制數據轉換爲圖像使用php
- 9. 使用數學模塊(Python)將整數轉換爲二進制
- 10. 如何將二進制數字轉換爲erlang中的整數?
- 11. 使用8位編碼將二進制轉換爲bytearray
- 12. 將整數轉換爲二進制/十進制的MIPS函數?
- 13. 將數據集轉換爲二進制
- 14. 將二進制數據轉換爲有符號整數
- 15. 將十進制數字轉換爲6位二進制數字
- 16. 轉換十進制整數爲4位二進制
- 17. 將整數轉換爲32位二進制 - 輸出反向
- 18. 將64位二進制轉換爲長整數
- 19. 如何將二進制數轉換爲整數而不使其爲正數
- 20. python - 將二進制數據轉換爲utf-8
- 21. 將十進制數位數組轉換爲二進制數位數組
- 22. 使用C將整數轉換爲二進制表示?
- 23. 如何將二進制數字轉換爲八進制數字?
- 24. 如何使用返回方法將整數轉換爲二進制數字?
- 25. 十進制到二進制轉換器(整數1-8)
- 26. MaxMind - 將二進制數據庫轉換爲PHP中的PHP
- 27. 如何將二進制文件轉換爲長整數?
- 28. 如何將二進制轉換爲mysql中的整數?
- 29. SystemVerilog:如何將二進制向量轉換爲整數
- 30. 如何將整數轉換爲VHDL中的二進制表示?
你能舉一個你想做什麼的例子嗎? –