0

I have a document with ascii characters like this:

~recÛßå  ^ÿìü   P       ` pÀ 0Ðÿp àÀ
```^^ÚÞâ  ^ÿòüü P      ÿ ÿà 0ÿ 0ÿÐ €
```^^ÚÞã     hÿòüü T

etc.

now I want this characters convert into the hexadecimal character like this:

037E038D03720365036301DB01DF01E50012005EFFEC0FFC0000005000000000000000600090027001C0003001D0FF7000E003C00D036003600360035E035E01DA01DE01E20012005EFFF20FFC0FFC0050000000000000FFA0FFE002200130FFA00130FFD0012003800D036003600360035E035E01DA01DE01E300090068FFF20FFC0FFC0054 etc.

My Code:

var byteArray = Encoding.UTF8.GetBytes(inputAscii);
var hexchars = "";
var i = 0;
while (i != byteArray.Length)
{
    hexchars += (byteArray[i]).ToString("X2");
}

I also tried var byteArray = Encoding.Default.GetBytes(inputAscii); and Encoding Ascii. Can you help me?

Edi G.
  • 2,432
  • 7
  • 24
  • 33
Selina
  • 59
  • 3
  • Try and look at: http://msdn.microsoft.com/en-us/library/2bbab5dh%28v=vs.110%29.aspx – Quintium Sep 03 '14 at 14:09
  • 3
    Side note: these are not "ASCII characters". – Alexei Levenkov Sep 03 '14 at 14:10
  • 1
    You'll probably want to read the file as binary instead of text. – Matthew Sep 03 '14 at 14:11
  • This post is duplicate as converting byte->hex is covered many times. Note that your code missing `i++` (or usage of `for`/`foreach`), but it is not clear if it is copy/paste error or actual issue since you did not post what problem you have with the code. – Alexei Levenkov Sep 03 '14 at 14:14
  • My Problem is that for example for the character ' ' my code encode it with 3F instead of 8D – Selina Sep 08 '14 at 11:09

1 Answers1

0

Try this:

var e = Encoding.GetEncoding("iso-8859-1");
            var bytes = Encoding.GetEncoding(1252).GetBytes("~recÛßå ^ÿìü P  pÀ 0Ðÿp àÀ^^ÚÞâ ^ÿòüü P ÿ ÿà 0ÿ 0ÿÐ € `^^ÚÞã hÿòüü T usw.");
            var s = e.GetString(bytes);

            var hexchars = "";
            for (int i = 0; i < bytes.Length; i++ )
            {
                hexchars += (bytes[i]).ToString("X2");
            }
  • The result is : 036003600360035E013F01 usw but I need the result 037E038D03720365036301DB01DF01E50 usw.. – Selina Sep 05 '14 at 07:28