ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
i have some numbers that READ as decimal but are actually in hex, and i need to convert them to REAL decimal. i mean, i have say, hex=44, which when converted to decimal = 72 or something, but if you get what i mean the decimal value is meant to be 44. with me? it's basically so a decimal number can be stored precisely over 8 bits... currently i am using sprintf to print the hex value to a string, and then using atoi to get it to decimal... I don't like this, but can't see a better way to do it. anyone?
I am not sure I understand exactly what you are trying to do here... You realize that no matter how you represent a number (at least in languages like C) it is always stored over a percise number of bits (aka "chars" are always percisely 8 bits). If I were programming in C and wanted an integer value that was always going to take up a total of 8 bits I would just declare it as a char, or unsigned char depending.
What are you trying to use this for, maybe I can suggest a better method for doing what you are trying to do.
Originally posted by acid_kewpie currently i am using sprintf to print the hex value to a string, and then using atoi to get it to decimal... I don't like this, but can't see a better way to do it. anyone?
It's not too ugly this way, I think.
However there is a special function to do this: "strtol()". It does only "long int", but if you are sure the hex number aren't too big, it's safe to cast the return value to "int".
dec = (int) strtol(hex, NULL, 16);
printf("Decimal = %i\n", dec);
I hope this was what you meant, as your question 's not too clear IMHO.
i'm parsing information out of a dvd, and as a dvd information is stored in individual bits, to this end most decimal digits providing information about the contents of the dvd is not stored as a decimal number, but as hex. but the contents of each hex number actually READS as decimal, i.e. there are never any a-f's and the extra bits are just ignored, so as to make the data extraction much easier. i however need these numbers as real decimal, not just hex, which isn't really hex, just basically a text string.
basically i have a hex variable containing a hex value of "25" but need to convert it into a decimal value of "25", NOT a decimal value of erm.... "37"
Yes, the problem doesn't really have anything to do wih hexadecimal numbers.
But it's easily confusing in this case. Hexadecimal numbers are invented just because counting from 0 to F just nicely fill up 4 bits.