|
-
October 2nd, 2007, 10:35 AM
#1
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.
-
October 2nd, 2007, 10:48 AM
#2
Re: DOS - Batch question regarding leading zeros
 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
-
October 2nd, 2007, 10:51 AM
#3
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.
-
October 2nd, 2007, 10:58 AM
#4
Re: DOS - Batch question regarding leading zeros
 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.
-
October 2nd, 2007, 10:59 AM
#5
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
-
October 2nd, 2007, 01:35 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|