按位移位-在C#中得到的结果与在php中得到的结果不同
我试图将一些PHP转换为C#,但按位函数给出了不同的结果 PHP将返回248按位移位-在C#中得到的结果与在php中得到的结果不同,c#,php,bit-manipulation,C#,Php,Bit Manipulation,我试图将一些PHP转换为C#,但按位函数给出了不同的结果 PHP将返回248 protected function readInt8() { $ret = 0; if (strlen($this->_input) >= 1) { $sbstr = substr($this->_input, 0, 1); $ret = ord($sbstr); $this->_input = substr($this-
protected function readInt8()
{
$ret = 0;
if (strlen($this->_input) >= 1)
{
$sbstr = substr($this->_input, 0, 1);
$ret = ord($sbstr);
$this->_input = substr($this->_input, 1);
}
return $ret;
}
C#将返回63
private int ReadInt8()
{
int ret = 0;
if (input.Length >= 1)
{
string substr = input.Substring(0, 1);
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] buffer = ascii.GetBytes(substr);
ret = buffer[0]; // 63
this.input = this.input.Substring(1);
}
return ret;
}
否则它将返回14337
private int ReadInt8()
{
int ret = 0;
if (input.Length >= 1)
{
string substr = input.Substring(0, 1);
ret = (int)(substr[0]); // 14337
this.input = this.input.Substring(1);
}
return ret;
}
在这里,一个函数适用于较大的值,但它不适用于较小的值。我想知道问题出在哪里
对不起。昨天有点晚了
使用下面的函数转换的输入=“Ԁϸ”㠁锂Ǹϸ붻ªȁ";
关于班次。我认为它可能需要改变,因为ReadInt16()需要它
private int ReadInt16()
{
int ret = 0;
if (input.Length >= 2)
{
ret = ((int)(this.input.Substring(0, 1)[0]) & 0xffff) >> 8;
ret |= ((int)(this.input.Substring(1, 1)[0]) & 0x0000) >> 0;
this.input = input.Substring(2);
}
return ret;
}
我应该说。我可能误解了PHP中函数的使用。不要将字符串视为等同于字节数组。字符编码将干扰和损坏数据(如果数据实际上不是文本)。如果您必须以文本形式传输原始数据,则必须对其进行适当的编码/解码,例如使用base64编码。如果您能告诉我们要从哪个输入开始,这会有所帮助……我看不到您的代码中有任何位移。63是ASCII代表
?
,这意味着您想要的内容没有有效的ASCII字符。请记住,只有值我不清楚您试图用这段代码实现什么。您的问题之一可能是php滥用字符串来表示字符串和字节序列,而C#对它们有不同的表示。我看不到任何按位或移位。你能解释一下你想做什么,这样我们就可以做一个合理的标题吗?
private int ReadInt16()
{
int ret = 0;
if (input.Length >= 2)
{
ret = ((int)(this.input.Substring(0, 1)[0]) & 0xffff) >> 8;
ret |= ((int)(this.input.Substring(1, 1)[0]) & 0x0000) >> 0;
this.input = input.Substring(2);
}
return ret;
}