任何人都能说出这是什么类型的转换

时间:2011-04-13 14:51:36

标签: c# .net-2.0

enter image description here

我将输入作为左侧11位数字,输出应该在那之前,或者任何人都可以告诉我如何获得那些输出byu将这些输入作为字节数组

6 个答案:

答案 0 :(得分:3)

这远非优化,但这应该有效

int code = Convert.ToInt32("0x" + "82", 16);  //this is your first char
code = code >= 0x20 && code < 0x7f ? code : 0x2E;
byte b = Convert.ToByte(code);  
string txt = System.Text.Encoding.UTF8.GetString(new byte[] { b });

答案 1 :(得分:3)

如果你已经将输入作为字节数组,那么这应该给你字符串;

string result = Encoding.ASCII.GetString(input); // where "input" is your byte[]

答案 2 :(得分:3)

编码似乎ASCII,但由于某种原因,某些字符的高位设置。您可以清除该位并使用Encoding.ASCII从字节数组构建字符串:

string result = Encoding.ASCII.GetString(
    yourByteArray.Select(b => (byte) (b & 0x7F)).ToArray());

编辑:如果你不能使用LINQ,你可以这样做:

for (int i = 0; i < yourByteArray.Length; ++i) {
    yourByteArray[i] &= 0x7F;
}

string result = Encoding.ASCII.GetString(yourByteArray);

答案 3 :(得分:3)

Example of first line

@changed支持点     string [] line =“82 44 b4 2e 39 39 39 39 39 35”.Split('');

byte[] bytes = new byte[line.Length];

for (int i = 0; i < line.Length; i++) {
  int candidate = Convert.ToInt32(line[i], 16);
  if (!(!(candidate < 0x20 || candidate > 127)))
    candidate = 46; //.
  bytes[i] = Convert.ToByte(candidate);
}

string s =  System.Text.Encoding.ASCII.GetString(bytes);

答案 4 :(得分:0)

为了帮助您入门:

// Convert the number expressed in base-16 to an integer.
int value = Convert.ToInt32(hex, 16);

这取自MSDN

答案 5 :(得分:0)

byte[] bytes=new byte[]{0xf3,0x28,0x48,0x78,0x98};
var output=string.Concat( 
    bytes.Select(b => (char) (b >= 0x20 && b<0x7f ? b :(byte)'.') ).ToArray()
);