将字符串编码为字节是添加不必要的“00” [英] Encoding string to bytes is adding unnecessary "00"s
问题描述
美好的一天,
我有一个16字节的字符串,我编码为字节,然后立即将其转换为可读的十六进制字符串。结果是它在每个Hex之后添加了00,我似乎无法理解为什么。
我的字符串:
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
为什么这样做?有没有办法在没有00的情况下编码字节?请指出正确的方向。我试图在网上搜索类似的问题(或者不是问题?),找不到相关的东西。
谢谢
我尝试了什么:
我试图搜索类似问题,找不到相关问题。
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.
推荐答案
c# - 为什么Encoding.Unicode.GetBytes()返回额外的0 - 堆栈溢出 [ ^ ]
var stringToByte = Encoding.UTF8.GetBytes(message1);
或
var stringToByte = Encoding.ASCII.GetBytes(message1);
都会给你你想要的结果。
would both give you the result you are expecting.
你我们编辑Unicode编码来获取字节。
你可能不会因为你不知道Unicode是什么而表现出谎言。我建议在这里阅读:什么是Unicode?a> [ ^ ]。
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?[^].
这篇关于将字符串编码为字节是添加不必要的“00”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!