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"

8
  • 2
    What output do you expect from Console.WriteLine(s);? Commented Jan 22, 2014 at 9:48
  • check this link stackoverflow.com/questions/74148/… Commented Jan 22, 2014 at 10:28
  • I m getting System.Byte[] which is not getting encoded .. Commented Jan 22, 2014 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? Commented Jan 22, 2014 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 Commented Jan 22, 2014 at 12:34

2 Answers 2

1

Instead of

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

write

string s = String.Join(" ", byteData);
Sign up to request clarification or add additional context in comments.

4 Comments

I m getting output as System.Byte[].. in the method data is been returning System.Byte[].. i didnt still get the output ..
@user3222857 you accepted the answer, but the comment says you didn't get the output. Clarify your current state if you still need help.
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 ??
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");
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);
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.