开发者

Can any one tell what type of conversion is this

Can any one tell what type of conversion is this

I will have the input as the left hand side 11 digits and the output should be preceeding to that or can any one tell how can i get that ouput byu taking those in开发者_StackOverflowputs as byte array


Example of first line

@changed to support dot 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);


If you already have the input as a byte array, then this should give you the string;

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


This is far from optimized, but this should work

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 });


That encoding seems to be ASCII, but some of the characters have their high bit set for some reason. You can clear that bit and use Encoding.ASCII to build a string from your byte array:

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

EDIT: If you can't use LINQ, you can do:

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

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


To get you started:

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

This was taken from MSDN


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜