Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org converting fake hex to decimal in c
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 10-06-2002, 06:58 PM #1 acid_kewpie Moderator   Registered: Jun 2001 Location: UK Distribution: Gentoo, RHEL, Fedora, Centos Posts: 43,417 Rep: converting fake hex to decimal in c 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?
 10-08-2002, 09:13 AM #2 jtshaw Senior Member   Registered: Nov 2000 Location: Seattle, WA USA Distribution: Ubuntu @ Home, RHEL @ Work Posts: 3,892 Blog Entries: 1 Rep: 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.
10-08-2002, 11:26 AM   #3
Hko
Senior Member

Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep:
Re: converting fake hex to decimal in c

Quote:
 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".
Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
char hex[]="44";
int dec;

dec =  (int) strtol(hex, NULL, 16);
printf("Decimal = %i\n", dec);
return 0;
}```
I hope this was what you meant, as your question 's not too clear IMHO.

Last edited by Hko; 10-08-2002 at 11:30 AM.

 10-08-2002, 11:30 AM #4 acid_kewpie Moderator   Registered: Jun 2001 Location: UK Distribution: Gentoo, RHEL, Fedora, Centos Posts: 43,417 Original Poster Rep: 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" any better?
10-08-2002, 11:40 AM   #5
llama_meme
Member

Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep:
Quote:
 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"
What do you mean by "a hex variable"? It's all in binary really (I guess you know this, but it's still not clear what you mean)

Alex

Last edited by llama_meme; 10-08-2002 at 11:43 AM.

 10-08-2002, 12:07 PM #6 acid_kewpie Moderator   Registered: Jun 2001 Location: UK Distribution: Gentoo, RHEL, Fedora, Centos Posts: 43,417 Original Poster Rep: hmm, i think it's just easiest to forget this ehh? it's a bugger to explain without finding it out for yourself, and not really that important i guess. never mind. thanks for trying though.
 10-08-2002, 12:43 PM #7 Hko Senior Member   Registered: Aug 2002 Location: Groningen, The Netherlands Distribution: ubuntu Posts: 2,530 Rep: OK, I think I get it now. If I understand it well, it's like this: You read a byte. Of this byte, the leftmost 4 bits contain a decimal value of 0 to 9. And the rightmost 4 bits of your byte as well contain a decimal value of 0 to 9. So a value of 25 is coded this way appears as an int with value 37. Because the '2' is in the leftmost 4 bits, and the '5' is in the rightmost 4 bits. If I got it right this time, the following code will dot the trick using bitwise operators. Code: ```#include int main() { unsigned char byte = 37; unsigned char dec; dec = 10 * (byte >> 4) + (byte & 0x0F); printf("Decimal = %d\n",dec); return 0; }``` Now I'm wondering how you managed to do the same using sprintf() and atoi(). Last edited by Hko; 10-09-2002 at 06:52 AM.
 10-08-2002, 01:13 PM #8 acid_kewpie Moderator   Registered: Jun 2001 Location: UK Distribution: Gentoo, RHEL, Fedora, Centos Posts: 43,417 Original Poster Rep: Code: ```sprintf(hex, "%02x", dvd_time->second); tmp = second + atoi(hex);``` thanks. i just got hex stuck in my head mainly as the program i've been using for "inspiration" uses %x print statements... that looks much nicer... Last edited by acid_kewpie; 10-08-2002 at 01:16 PM.
 10-08-2002, 01:33 PM #9 Hko Senior Member   Registered: Aug 2002 Location: Groningen, The Netherlands Distribution: ubuntu Posts: 2,530 Rep: 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.
 08-20-2003, 09:56 AM #10 mike0w LQ Newbie   Registered: Aug 2003 Location: Shrewsbury UK Distribution: RedHat, Familiar, Debian, SuSE Posts: 6 Rep: I think the term you were looking for was "BCD" - ie. "binary coded decimal" This is used as you describe, where you have (multiples of) 4 bits each containing a number 0...9, the whole bit string to be interpreted as a string of decimal digits in a decimal number. Many general purpose processors have a few BCD instructions for limited arithmetic directly in BCD.
 08-20-2003, 02:29 PM #11 Hko Senior Member   Registered: Aug 2002 Location: Groningen, The Netherlands Distribution: ubuntu Posts: 2,530 Rep: Right! Sounds familiar. Must have heard it somewhere before. Thanks. (your first post, and you stumbled across this one year old thread?!? :-) )

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post jht2k Linux - General 2 07-01-2005 09:12 AM ipreuss Linux - General 2 03-25-2005 12:13 PM maldini1010 Programming 6 02-01-2005 04:03 PM Linh Programming 4 05-20-2004 03:21 PM Sm0k3 Programming 2 10-11-2003 04:29 AM

LinuxQuestions.org

All times are GMT -5. The time now is 05:50 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -