2011-06-23 66 views

回答

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" 
?> 

搶答http://php.net/manual/en/function.decbin.php

+0

老,但謝謝,這正是我所需要的:-) – Jek

+0

謝謝,這幫了我一些其他的東西,sprintf是我現在最好的事情,因爲自從切片面包。 – DarrenK

相關問題