Hi,
I have a very basic knowledge in C/C++,

I need help in converting HEX value to readable values.

I hope some one would give me a clear answer and make it understandable also so that i could learn from it..

thanks

Recommended Answers

All 8 Replies

try this code :)

#include<stdio.h>
#include<conio.h>
#include<math.h>
//******************************************************************************
//
//   gives a integer for a hexadecimal number
//
//******************************************************************************
int 
main(){
  //
  //   var decaration
  //
  int hex;
  while(1){
  printf("\nEnter a hexadecimal number.");
  scanf("\n%x",&hex);
  printf("\nThe integer equivalent is: %d\n", hex);
	}
  getch();
}

I'm not sure what you mean by readable values. Certainly hex is readable. I suppose you would like the number in a different base than 16.

If you are outputting to the screen then you can just format the output however you like since values are stored internally not related to the output format. Look at the format specifiers for printf.

If you'd like to convert the number to another base then you need to change the base.

I'm not sure what you mean by readable values. Certainly hex is readable. I suppose you would like the number in a different base than 16.

If you are outputting to the screen then you can just format the output however you like since values are stored internally not related to the output format. Look at the format specifiers for printf.

If you'd like to convert the number to another base then you need to change the base.

i mean like 6e:69:6a:69:6e to ascii

and also it is going to be read from a file, convert and write into a new file.

hope this makes sense..

thanks i will give a try on this...

To parse the exact format you provided you could use the following:

#include <stdio.h>

int main () {
    while (1) {
        int x = 0;
        scanf ("%x", &x);
        if (feof (stdin) || ferror (stdin))
            break;
        printf ("%d (%c)\n", x, (char) x);
        scanf(":");
    }
    return 0;
}

Providing the input from your example this yields the following:

110 (n)
105 (i)
106 (j)
105 (i)
110 (n)

To parse the exact format you provided you could use the following:

#include <stdio.h>

int main () {
    while (1) {
        int x = 0;
        scanf ("%x", &x);
        if (feof (stdin) || ferror (stdin))
            break;
        printf ("%d (%c)\n", x, (char) x);
        scanf(":");
    }
    return 0;
}

Providing the input from your example this yields the following:

110 (n)
105 (i)
106 (j)
105 (i)
110 (n)

that do work.... but can i use this to read from a file and write in a new file....

Sure. Suppose you compile the example code above to an executable named a.out and you have the contents of the file input.txt as you specified. Then you can create output.txt in the following way (in linux):

./a.out < input.txt > output.txt

Sure. Suppose you compile the example code above to an executable named a.out and you have the contents of the file input.txt as you specified. Then you can create output.txt in the following way (in linux):

./a.out < input.txt > output.txt

thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.