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.

Cher
  • 2,789
  • 10
  • 37
  • 64
  • 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. – Habib Jun 11 '15 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. – Sinatr Jun 11 '15 at 13:21
  • I corrected my question, sorry for bad choice of words – Cher Jun 11 '15 at 13:22
  • Maybe there is another way to do what you want. Why would you need that? – dmigo Jun 11 '15 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. – Cher Jun 11 '15 at 13:24
  • There is no such thing. But you could, for example, iterate side by side, two by two characters, convert them to bytes, and append result with result byte as hex. – Dusan Jun 11 '15 at 13:26
  • Well, byte array is much better choice for what you need than hex string. – Dusan Jun 11 '15 at 13:28
  • thanks I'll look that up – Cher Jun 11 '15 at 13:31

2 Answers2

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));
    }
Olaru Mircea
  • 2,570
  • 26
  • 49
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?

Community
  • 1
  • 1
Cher
  • 2,789
  • 10
  • 37
  • 64