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!
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
Re: DOS - Batch question regarding leading zeros
Thank you, but as I wrote up there I need a batch script. This means really DOS :)
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.
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
Re: DOS - Batch question regarding leading zeros
I kow this is not a script forum and that's why I wrote:
Quote:
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.