Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day,

I have a 16 byte string which I encode to bytes and then immediately convert it to a readable Hex string. The result is that it added 00 after every single Hex and I cannot seem to understand why.

My String:
var message1 = "Thats my Kung Fu";

Conversion:
var stringToByte = Encoding.Unicode.GetBytes(message1);
var stringByte = BitConverter.ToString(stringToByte);

And the outcome is:
54 00 68 00 61 00 74 00 73 00 20 00 6D 00 79 00 20 00 4B 00 75 00 6E 00 67 00 20 00 46 00 75 00

Expected outcome:
54 68 61 74 73 20 6D 79 20 4B 75 6E 67 20 46 75


Why is it acting like that? Is there a way to encode the bytes without 00? Please point me to the right direction. I tried to search similar issue online (or is it not an issue?), couldn't find anything related.

Thank you

What I have tried:

I tried to search similar issues, couldn't not find related issues.
Posted
Updated 19-Nov-18 16:08pm

C#
var stringToByte = Encoding.UTF8.GetBytes(message1);
or
C#
var stringToByte = Encoding.ASCII.GetBytes(message1);
would both give you the result you are expecting.
 
Share this answer
 
You used the Unicode encoding to get the bytes.

It's not behaving lie you expect probably because you have no idea what Unicode is. I suggest reading up it here: What is Unicode?[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900