2

I have a string which is 40FA. I would like to perform an XOR operation to it and return a byte which is 0xBA in this case. However, I'm only able to get that BA in string. When I convert BA to byte, I'm getting 186.

string tmp = "40FA";
int uid_1 = Convert.ToInt32(tmp.Substring(0, 2), 16);
int uid_2 = Convert.ToInt32(tmp.Substring(2, 2), 16);

int test = uid_1 ^ uid_2 ;
string final = test.ToString("X");
byte byteresult = Byte.Parse(final , NumberStyles.HexNumber);
7
  • It seems that you should put tmp.Substring(0, 2) instead of output.Substring(0, 2); and tmp.Substring(2, 2) instead of output.Substring(2, 2) Commented Jul 1, 2016 at 7:34
  • thanks for that. i've a big chunk of code and I edited it when I posted it here. Commented Jul 1, 2016 at 7:36
  • 1
    Well, 186 (decimal) == 0xBA == 0272 (Octal) == 10111010 (Binary); if you want represent byte as hexadecimal format it out as byteresult.ToString("X2"); your code does work: 0x40 ^ 0xFA == 0xBA == 186 (decimal) Commented Jul 1, 2016 at 7:39
  • What exactly is your problem? is it the output? Commented Jul 1, 2016 at 7:41
  • I need it to be in hex byte but it is showing me decimal instead. Commented Jul 1, 2016 at 7:42

4 Answers 4

3

try:

byte[] toBytes = Encoding.ASCII.GetBytes(somestring);

and for bytes to string

string something = Encoding.ASCII.GetString(toBytes);
Sign up to request clarification or add additional context in comments.

2 Comments

@active92 you cannot print just the array to console. Console.WriteLine executes just the default ToString() method of the type and it prints you: System.Byte[]
@MongZhu yeah. you are right. i can see the output when i'm debugging
2

Try to call that function, it will convert string to bytes

private byte[] String_To_Bytes2(string strInput)
{
    int numBytes = (strInput.Length) / 2;
    byte[] bytes = new byte[numBytes];

    for (int x = 0; x < numBytes; ++x)
    {
        bytes[x] = Convert.ToByte(strInput.Substring(x * 2, 2), 16);
    }
    return bytes;
}

static void Main(string[] args)
{
    string tmp = "40FA";
    int uid_1 = Convert.ToInt32(tmp.Substring(0, 2), 16);
    int uid_2 = Convert.ToInt32(tmp.Substring(2, 2), 16);
    int test = uid_1 ^ uid_2;
    string final = test.ToString("X");
    byte[] toBytes = String_To_Bytes2(final);

    Console.WriteLine(toBytes);
    Console.ReadKey();
}

Comments

1

I think there is a little misunderstanding here. You actually already solved your problem. The computer does not care whether the number is decimal or hexadecimal or octadecimal or binary. These are just representations of a number. So only you care how it is to be displayed.

As DmitryBychenko already said: 0xBA is the same number as 186. It won't matter for your byte array if you want to store it there. It only matters for you as a user if you want to display it.

EDIT: you can test it by running this line of code:

Console.WriteLine((byteresult == Convert.ToInt32("BA", 16)).ToString());

Your code actually does what you want if I understood you correctly from your comments.

string tmp = "40FA";
int uid_1 = Convert.ToInt32(tmp.Substring(0, 2), 16);
int uid_2 = Convert.ToInt32(tmp.Substring(2, 2), 16);

int test = uid_1 ^ uid_2 ;
string final = test.ToString("X");

// here you actually have achieved what you wanted.
byte byteresult = Byte.Parse(final , NumberStyles.HexNumber);

now you can use byteresult to store it in a byte[], and this:

byte[] MyByteArray = new byte[4];
MyByteArray [0] = byteresult;

should execute without problems.

Comments

1

Your input seems to be hexadecimal value, so you need to parse two digits as one byte. Then XOR these two bytes.

The result is 186 in decimal or BA in hex. It's the same value, just another base.

string tmp = "40FA";
byte uid_1 = byte.Parse(tmp.Substring(0, 2), NumberStyles.HexNumber);
byte uid_2 = byte.Parse(tmp.Substring(2, 2), NumberStyles.HexNumber);
byte test = (byte)(uid_1 ^ uid_2); // = 186 (decimal) = BA (hexadecimal)
string result = test.ToString("X");

2 Comments

I'm getting error for this. Cannot implicitly convert type 'int' to 'byte'. dotnetfiddle.net/OEFJvc
You're right, I added the cast that's needed. See also stackoverflow.com/q/2726920/314334

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.