1

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);
active92
  • 644
  • 12
  • 24
  • 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)` – Dmitry Bychenko Jul 01 '16 at 07:34
  • thanks for that. i've a big chunk of code and I edited it when I posted it here. – active92 Jul 01 '16 at 07: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)` – Dmitry Bychenko Jul 01 '16 at 07:39
  • What exactly is your problem? is it the output? – Mong Zhu Jul 01 '16 at 07:41
  • I need it to be in hex byte but it is showing me decimal instead. – active92 Jul 01 '16 at 07:42
  • ok so you actually need it in a `string` format to display it somewhere ?=! ok that is something you should add to your question. Because otherwise you solved your problem already. Because you have your result in the line: `string final = test.ToString("X");` – Mong Zhu Jul 01 '16 at 07:45
  • actually i need this output to be inserted to a byte array. – active92 Jul 01 '16 at 07:47

4 Answers4

2

try:

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

and for bytes to string

string something = Encoding.ASCII.GetString(toBytes);
vipin
  • 2,374
  • 1
  • 19
  • 36
  • @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[]` – Mong Zhu Jul 01 '16 at 07:43
  • @MongZhu yeah. you are right. i can see the output when i'm debugging – active92 Jul 01 '16 at 07:52
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();
}
Parveen Kumar
  • 344
  • 2
  • 11
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");
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
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.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76