0

Below is the code(conversion of hexadecimal to decimal) I'm trying to work out .. I found error which appears in place I've commented in the code..

Please provide a solution to rectify ..

static void Main(string[] args)
    {

        byte[] byteData;
        int n;
        byteData = GetBytesFromHexString("001C0014500C0A5B06A4FFFFFFFFFFFFFFFFFFFFFFFFFFFF");
        n = byteData.Length;
        Console.WriteLine(n);
        string s = System.Text.Encoding.UTF8.GetString(byteData, 0, n);   //error
        Console.WriteLine(s);
        Console.ReadLine();
    }

    public static byte[] GetBytesFromHexString(string hexString)
    {
        //MessageBox.Show("getbytes ");
        if (hexString == null)
            return null;

        if (hexString.Length % 2 == 1)
            hexString = '0' + hexString; // Up to you whether to pad the first or last byte

        byte[] data = new byte[hexString.Length / 2];

        for (int i = 0; i < data.Length; i++)
        {
            data[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            Console.WriteLine(data[i]);
        }

What I'm getting as output is: "\0\0P\f\n[���������������" The converted decimal value is not been encoded.

UPDATE: Expected output is "0 28 0 20 80 12 10 91 6 164 255 255 255 255 255 255 255 255 255 255 255 255 255 255"

Max Yakimets
  • 1,195
  • 7
  • 12
user3222857
  • 15
  • 1
  • 7
  • 2
    What output do you expect from `Console.WriteLine(s);`? – Max Yakimets Jan 22 '14 at 09:48
  • check this link http://stackoverflow.com/questions/74148/how-to-convert-numbers-between-hexadecimal-and-decimal-in-c – Amjad Abdelrahman Jan 22 '14 at 10:28
  • I m getting System.Byte[] which is not getting encoded .. – user3222857 Jan 22 '14 at 10:38
  • The code works perfect: provide a string with hexadecimal numbers, convert it to an array of bytes (data[0] = 0, data[1] = 0x1c = 28 etc), convert te byte array to a string (assuming the array contains UTF8) an display it. Because the hexadecimal string contains a few characters that cannot be displayed (0x1C, 0x14 and 0xFF for instance) the output looks strange but is not incorrect. So, @MaxYakimets's question remains: what did you expect? – venerik Jan 22 '14 at 12:22
  • @venerik thank u for ur reply.. returning data from the method i m getting as System.Byte[] wen its converted to string i get that output .. pls help me – user3222857 Jan 22 '14 at 12:34
  • @user3222857 you keep ignoring the question, yet keep asking for help – Max Yakimets Jan 22 '14 at 12:59
  • @MaxYakimets : sorry .. i expect this output "0 28 0 20 80 12 10 91 6 164 255 255 255 255 255 255 255 255 255 255 255 255 255 255" – user3222857 Jan 22 '14 at 13:18
  • oh, then do `String.Join(" ", byteData);`, not UTF8-decoding – Max Yakimets Jan 22 '14 at 13:37

2 Answers2

1

Instead of

string s = System.Text.Encoding.UTF8.GetString(byteData, 0, n);

write

string s = String.Join(" ", byteData);
Max Yakimets
  • 1,195
  • 7
  • 12
  • I m getting output as System.Byte[].. in the method data is been returning System.Byte[].. i didnt still get the output .. – user3222857 Jan 23 '14 at 05:05
  • @user3222857 you accepted the answer, but the comment says you didn't get the output. Clarify your current state if you still need help. – Max Yakimets Jan 23 '14 at 08:27
  • thanks i got the output..still i have a doubt that string s = System.Text.Encoding.UTF8.GetString(byteData, 0, n); why this code doesn't work ?? – user3222857 Jan 23 '14 at 12:03
  • because "a number 0" is not the same as "a character 0": a number 0 is kept in memory as 0, while a UTF8 character "0" is kept in memory as 48 (30 in Hex, i.e 0x30) :) `byte[] zero = Encoding.UTF8.GetBytes("0");` – Max Yakimets Jan 23 '14 at 12:14
0

You can use this and try to play with the radix ( in this case 2 ). This snippet, which I once wrote, helped me all the time.

 private void ConvHexStringToBitString(ref string strErrorBitMask)
    {
        try
        {
            string strTempStr = strErrorBitMask;
            if (strTempStr != string.Empty)
            {
                strTempStr = Convert.ToString(Convert.ToInt32(strTempStr.Replace(" ", "0"), 16), 2);
            }
            strErrorBitMask = strTempStr.PadLeft(32, '0');

        }
        catch (Exception ex)
        {
            LogWithMsg(ex);
        }
    }
icbytes
  • 1,831
  • 1
  • 17
  • 27