LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 10-06-2002, 06:58 PM   #1
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
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?
 
Old 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: Reputation: 66
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.
 
Old 10-08-2002, 11:26 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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.
 
Old 10-08-2002, 11:30 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Original Poster
Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
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?
 
Old 10-08-2002, 11:40 AM   #5
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
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.
 
Old 10-08-2002, 12:07 PM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Original Poster
Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
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.
 
Old 10-08-2002, 12:43 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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 <stdio.h>

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.
 
Old 10-08-2002, 01:13 PM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Original Poster
Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
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.
 
Old 10-08-2002, 01:33 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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.
 
Old 08-20-2003, 09:56 AM   #10
mike0w
LQ Newbie
 
Registered: Aug 2003
Location: Shrewsbury UK
Distribution: RedHat, Familiar, Debian, SuSE
Posts: 6

Rep: Reputation: 0
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.
 
Old 08-20-2003, 02:29 PM   #11
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Right! Sounds familiar. Must have heard it somewhere before.
Thanks.

(your first post, and you stumbled across this one year old thread?!? :-) )
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
gnome-calculator - converting between numeric bases (eg hex to binary) jht2k Linux - General 2 07-01-2005 09:12 AM
Decimal Symbol - does it exist??? ipreuss Linux - General 2 03-25-2005 12:13 PM
Decimal to binary maldini1010 Programming 6 02-01-2005 04:03 PM
Have problem converting a decimal number to octal Linh Programming 4 05-20-2004 03:21 PM
C, converting hex to text Sm0k3 Programming 2 10-11-2003 04:29 AM


All times are GMT -5. The time now is 01:51 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration