I'm trying to convert some PHP into C# but the bitwise functions are giving me different results.
PHP will return 248
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# will return 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;
}
or it will return 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;
}
An other question here worked with the bigger values but it doesn't work with the smaller values. I would like to know what the problem is.
I'm sorry. It was a bit late yesterday.

The input converted with the function below = "Ԁϸ㠁锂Ǹϸ붻ªȁ";
public string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
About the shift. I thought it maybe needed a shift because the ReadInt16() needed it.
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;
}
I should say. It is possible that I misinterpreted the use of the function in PHP.
?, which means that there is no valid ASCII char for what you want. Remember only values <128 are ASCII.