Convert hex from a text to decimal

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!
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
I'm not so sure how to do this. How can I make it select only the first three numbers then apply this function?
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);
		}
	}
}
Thanks rocketboy9000! How can I incorporate this into my code that opens the file?
replace stdin with whatever FILE * you have.
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
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
Nice it worked! Is there anyway to make it rewrite the numbers that are in hex into decimal within the file?
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.
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.
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
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
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
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
Topic archived. No new replies allowed.