2

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.

bytes from the server

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.

5
  • It would help if you'd tell us the input to start with... Commented Sep 18, 2012 at 21:35
  • 3
    I don't see any bitwise shifts in your code. Commented Sep 18, 2012 at 21:35
  • 4
    63 is ASCII for ?, which means that there is no valid ASCII char for what you want. Remember only values <128 are ASCII. Commented Sep 18, 2012 at 21:37
  • 2
    It's unclear to me what you're trying to achieve with this code. One of your problems might be that php abuses string to represent both strings and byte sequences, whereas C# has different representation for those. Commented Sep 18, 2012 at 21:41
  • 1
    I don't see anything bitwise or shifting. Can you explain what you're trying to do so we can make a sensible title? Commented Sep 18, 2012 at 21:56

1 Answer 1

2

Don't treat strings as equivalent to byte arrays. Character encodings will interfere and corrupt the data (if it is not actually text). If you must transmit raw data as text, it must be encoded/decoded appropriately, such as with base64 encoding.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.