CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2003
    Location
    Timisoara, Romania
    Posts
    460

    DOS - Batch question regarding leading zeros

    Hello all,
    I don't kow if this is a good place for this question but I couldn't find any other place to put it.
    So here it goes. I'm reading a byte from a HEX file that is transformed from hexa to ascii. My input could would be 1,2 or 3 symbols as on byte is from 0 to 255. What I want is a batch script to put leading zeros to this string to always have 3 digits. So I need a transformation from 01 to 001 as example. The problem is that could be 1 or 01... so I don't know if I have to put 1 or 2 leading zeros...
    Thanks!
    You liked it? Please show your gratitude and rate it!

    There is no 'patch' for stupidity.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: DOS - Batch question regarding leading zeros

    Quote Originally Posted by vma
    Hello all,
    I don't kow if this is a good place for this question but I couldn't find any other place to put it.
    So here it goes. I'm reading a byte from a HEX file that is transformed from hexa to ascii. My input could would be 1,2 or 3 symbols as on byte is from 0 to 255. What I want is a batch script to put leading zeros to this string to always have 3 digits. So I need a transformation from 01 to 001 as example. The problem is that could be 1 or 01... so I don't know if I have to put 1 or 2 leading zeros...
    Thanks!
    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    string LeadingZeros(const string& hexstr)
    {
        switch(hexstr.length())
        {
             case 1:
                 return "00";
             case 2:
                  return "0";
         }
         return "";
    }
    
    int main()
    {
        fstream infile("q:\\hex.txt");
        if ( !infile )
             return -1;
       string hexstr;
       while (!infile.eof())
       {
            infile >> hexstr;
            hexstr = LeadingZeros(hexstr) + hexstr;
            cout << hexstr << endl;
       }
    }   
    
    Hex.txt:
    
    001
    02
    03
    4
    12
    145
    
    Output:
    001
    002
    003
    004
    012
    145
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2003
    Location
    Timisoara, Romania
    Posts
    460

    Re: DOS - Batch question regarding leading zeros

    Thank you, but as I wrote up there I need a batch script. This means really DOS
    You liked it? Please show your gratitude and rate it!

    There is no 'patch' for stupidity.

  4. #4
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: DOS - Batch question regarding leading zeros

    Quote Originally Posted by vma
    Thank you, but as I wrote up there I need a batch script. This means really DOS
    Doesn't change the fact that this is a C++ forum. Is the code not useful for you?

    Here is a forum that this thread would have been relevant in.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: DOS - Batch question regarding leading zeros

    This is a C++ forum. You can just compile the program, pass in the arguments in main(), and there you have your "script".

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Oct 2003
    Location
    Timisoara, Romania
    Posts
    460

    Re: DOS - Batch question regarding leading zeros

    I kow this is not a script forum and that's why I wrote:
    I don't kow if this is a good place for this question but I couldn't find any other place to put it.
    This is the closest I could find and that's why I hoped for an answer. If it really offends you I will delete it.
    You liked it? Please show your gratitude and rate it!

    There is no 'patch' for stupidity.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured