Convert hex from a text to decimal

Feb 17, 2011 at 1:06am
Hello,
I have a text file that contains lines that look like this:
FDB $01F0

I want to take the first three numbers after the "$" and convert them to decimal. I would like to output them into a new file or just display them, it doesn't matter. I found this to start:

#include "stdafx.h"
#include<iostream>
#include<fstream>

using namespace std;

int main() {

ifstream myReadFile;
myReadFile.open("myfile");
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {


myReadFile >> output;
cout<<output;


}
}
myReadFile.close();
return 0;
}

However I'm not sure where to go from here to pick out the hex and convert it then display it. Thanks in advance!
Feb 17, 2011 at 2:24pm
Firstly, write a function that takes a hex digit '0', '1' , '2' ... 'E', 'F' and converts it to integer numbers 0, 1, 2 ... 14, 15. Then apply this function to every digit.

How conversion works:
F -> 15*1 = 15
C7 -> 12*16+7*1 = 199
12B -> 1*256+2*16+11 = 299

decimal number is the sum of every digit multiplied by 16pusition of that number
Feb 17, 2011 at 11:19pm
I'm not so sure how to do this. How can I make it select only the first three numbers then apply this function?
Feb 17, 2011 at 11:37pm
Here:
1
2
3
4
5
6
7
8
9
10
#include "stdio.h"
int main(){
	int i;
	char s[100];
	while(fgets(s,100,stdin)){
		if(sscanf(s,"%*[^$]$%3x",&i)>0){
			printf("%d\n",i);
		}
	}
}
Feb 18, 2011 at 12:19am
Thanks rocketboy9000! How can I incorporate this into my code that opens the file?
Feb 18, 2011 at 12:22am
replace stdin with whatever FILE * you have.
Feb 18, 2011 at 1:24am
when I replace it with Table (My file name) I get an undeclared identifier error. When I replace it with "Table" I get this:
'fgets' : cannot convert parameter 3 from 'const char [6]' to 'FILE *' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Feb 18, 2011 at 1:32am
You need to use fopen to open the file:
1
2
3
4
5
6
7
8
9
10
11
12
#include "stdio.h"
int main(){
	int i;
	char s[100];
	FILE * f=fopen("Table.txt","r");//replace table.txt with the name of your file.
	while(fgets(s,100,f)){
		if(sscanf(s,"%*[^$]$%3x",&i)>0){
			printf("%d\n",i);
		}
	}
	fclose(f);
}
Last edited on Feb 18, 2011 at 1:32am
Feb 18, 2011 at 1:37am
Nice it worked! Is there anyway to make it rewrite the numbers that are in hex into decimal within the file?
Feb 18, 2011 at 4:39am
You mean replace them? Try opening another file, writing the new data in, deleting the old one, and renaming the new one to the name of the old one. The easiest way to do the rename and delete is to use system() to use whatever commands your computer has for that.
Feb 18, 2011 at 11:45am
Feb 18, 2011 at 7:11pm
I dunno if those work on files in another folder... stdio.h makes no reference to the concept of directories, although I admit it would be weird if it didn't work.
Feb 18, 2011 at 7:38pm
I would just use a stringstream to handle the base conversion:
1
2
3
4
5
6
#include <sstream>
//...
istringstream ss( "1a" );
int n;
ss >> hex >> n;
cout << n << endl;





26
Feb 18, 2011 at 7:45pm
Uhhuh? Congrats you turned line 8 and and half of line 7 of my solution into 3 lines and an extra object!
And what about getting the 3 digits from after the '$'?
I don't think there's a simple equivalent to "%*[^$]$%3x", especially the first part.
Remember that my solution only does the printing if the entire format matches!
Bah, stupid sstream bullsh!t elitism.
Last edited on Feb 18, 2011 at 7:47pm
Feb 18, 2011 at 8:30pm
Ok... Of course this doesn't validate the format but it has a few nice benefits.
1
2
3
4
5
6
7
8
9
10
11
#include <fstream>
#include <sstream>
//...
ifstream fin( "data.txt" );
string line;
while( getline( fin, line ) {
    istringstream ss( line.substr( 5, 3 ) );
    int n;
    ss >> hex >> n;
    cout << n << endl;
}
Last edited on Feb 18, 2011 at 8:38pm
Feb 18, 2011 at 8:40pm
And if the number of characters before the dollar sign varied?
And if some lines didn't have a dollar sign?
Your solution will no doubt involve ignore(), an if statement to check for EOL versus finding the dollar sign, and other manual format matching that tries desperately to match the functionality of the superior scanf interface.
What are these nice benefits?
Last edited on Feb 18, 2011 at 8:53pm
Topic archived. No new replies allowed.