0

If I want to work with string to represent hexadecimal values, is there a way to make bitwise AND, bitwise OR and such between them?

I know there are types which can handle that, but I'd need to keep them as string.

7
  • 1
    logical AND/OR between string values ?? you can use logical operators on conditions/bool operands, Do you mean bitwise AND/OR ? You can't use them on strings though. Commented Jun 11, 2015 at 13:20
  • What is hex codes? Hexadecimal values? You can do logical operation with integer types (e.g. UInt16), while converting them to string to show to the user hexadecimal values. So converting string to integer, doing operation and converting back to string should do the trick, however it makes sense to keep them as integer. Commented Jun 11, 2015 at 13:21
  • I corrected my question, sorry for bad choice of words Commented Jun 11, 2015 at 13:22
  • Maybe there is another way to do what you want. Why would you need that? Commented Jun 11, 2015 at 13:23
  • I make an hex interpreter for a rom hack, and up to now I stored all the hex codes and did all the operation with them as string, so keeping them as string would be simpler since there is a lot of code. Commented Jun 11, 2015 at 13:24

2 Answers 2

1

After reading commentaries and answer I choose to use byte array (thanks to Dusan)

Solution I picked :

All I had to do was to convert the string to byte when I need to use bitwise operator, and then back to string.

This links shows how How do I get a consistent byte representation of strings in C# without manually specifying an encoding?

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

Comments

1

I don't know about such a type.

I usually use these methods :

     //check if string is in hex format.
    bool OnlyHexInString(string text)
    {
        for (var i = 0; i < text.Length; i++)
        {
            var current = text[i];
            if (!(Char.IsDigit(current) || (current >= 'a' && current <= 'f')))
            {
                return false;
            }
        }
        return true;
    }

Conversion from string hex to int is done here:

    public string HexToInt(string hexString)
    {
        hexString = hexString.Trim();
        hexString = hexString.ToLower();
        if (hexString.Contains("0x"))
            hexString = Regex.Replace(hexString, "0x", "");

        if (OnlyHexInString(hexString))
            return System.Convert.ToInt64(hexString, 16).ToString();
        else
            return "";
    }

Edit 1 I forgot to add the Int64.Parse() on the result if the result is not null or empty. Sorry for that.

Perform your actions and get the result back to hex string:

    public string IntToToHex(string integerAsAString)
    {
        if (!string.IsNullOrEmpty(integerAsAString))
        {
            Int64 integerValue = 0;

            bool parsed = Int64.TryParse(integerAsAString, out integerValue);

            if (parsed)
                return integerValue.ToString("X");

            else
                throw new Exception(string.Format("Couldn't parse {0} hex string!", integerAsAString));
        }
        else
            throw new Exception(string.Format("Couldn't parse {0} hex string!", integerAsAString));
    }

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.