我试图在bash(linux)中找到C#代码中的等效代码。
我有这个C#代码:
// C# to convert a byte array to a string.
byte [] dBytes = ...
string str;
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
str = enc.GetString(dBytes);
我在fedora linux机器上运行bash。
我在bash中获取了一个包含所有字节数组的文件作为由空格分隔的文本,如下所示:
“72 0 101 0 108 0 108 0 111 0 32 0 87 0 111 0 114 0 108 0 100 0 33 0”
任何想法?
答案 0 :(得分:0)
最终我在python中完成了它并从我的bash脚本中调用了python脚本:
file = open('xx.txt', 'r')
inputText = file.read()
split = inputText.split(" ")
noZeros= filter (lambda a: a != "0", split)
results = map(int, noZeros)
final = "".join(map(chr, results))
print final
答案 1 :(得分:0)
这些字节采用UTF-16LE编码,而不是UTF-8。你的python脚本应该是:
file = codecs.open('xx.txt', 'r', 'utf-16-le')
ustring = file.read() #this is a unicode string, not a normal python string
print ustring