Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
3.22/5 (3 votes)
See more:
im using a code which will convert a string(char) to its hex equivalent and write it to a binary file. There is an array declared for the string of size 10. If the string is mentioned as "help" the binary equivalent is displayed as 48 45 4c 50 20 20 20 20 20 20. When there is a space it is displayed as "20"(nothing but its hex equivalent). But if the length of the string is 4 i want the remaining space to be filled with NULL character("00") , so the output will be displayed as "48 45 4c 50 00 00 00 00 00 00". I want the extra space to be filled with null character.

Code what im using:-
C#
using System;
using System.IO;
using System.Text;

namespace RandomFileAccess
{
    class Test
    {

        [STAThread]

        static void Main(string[] args)
        {
            myName me = new myName("help");
            FileStream fs = File.Create("DB.bin");
            BinaryWriter bw = new BinaryWriter(fs, Encoding.Unicode); //<---------
            bw.Write(me.Name.ToCharArray()); //<--------- Writing data as Chararray
            bw.Flush();
            fs.Close();

        }
    }

    class myName
    {
        private const int CHAR_ARRAY_LENGTH = 10;

        private char[] _name = new char[CHAR_ARRAY_LENGTH];

        public myName(string sName)
        {
            Name = sName;
        }

        public string Name
        {
            set
            {
                string name = value;
                int len = name.Length;
                if (len < CHAR_ARRAY_LENGTH)
                {
                    _name = name.PadRight(CHAR_ARRAY_LENGTH, ' ').ToCharArray();
                }
                else if (len >CHAR_ARRAY_LENGTH)
                {
                    _name = name.Substring(0, CHAR_ARRAY_LENGTH).ToCharArray();
                }
                else
                {
                    _name = name.ToCharArray();
                }
            }
            get
            {
                return new string(_name);
            }
        }
    }
}
Posted
Updated 12-Feb-14 5:17am
v2
Comments
Sergey Alexandrovich Kryukov 12-Feb-14 11:14am    
There is no such thing as "hex" file. A binary file is just binary. The term "hexadecimal", "decimal" can only be applicable to strings representing some numeric value in one or another form.
—SA

If I understand your intent, then change this line:

_name = name.PadRight(CHAR_ARRAY_LENGTH, ' ').ToCharArray();

to this:

_name = name.PadRight(CHAR_ARRAY_LENGTH, '\0').ToCharArray();
 
Share this answer
 
Comments
S Houghtelin 12-Feb-14 15:35pm    
Don't know why this was down-voted, it does exactly what the OP was looking for.

The ' ' is a space (Or ascii 20), where the '\0' is escape sequence and 0 for 00 which is the null character value ('0' is hex 32 ascii value)
Please see my comment to the question. It should be apparent that the question is based on very fundamental misunderstanding of some basics, so further discussion would make no sense. I would only note that you don't have to interpret data as characters, and even if you do, null character technically is no different from any other characters.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900