Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Decimal to Hexadecimal

Posted on 2006-11-17
6
1,852 Views
Last Modified: 2008-01-09
Hello guys.

I've mounted a system that uses a latch and a shift register to send values to 4 7-segment displays.

The displays show whatever is sent to them, but on a hexadecimal base. I need to send a __int16 number, but it's conversion has to show only decimal digits. For instance, if I sent a sequency from 7 to 12 in decimal, it would bring:

0x00000007 ( = 7 decimal)
0x00000008 ( = 8 decimal)
0x00000009 ( = 9 decimal)
0x0000000A ( = 10 decimal)
0x0000000B ( = 11 decimal)
0x0000000C ( = 12 decimal)

and so on.

I need to send only:

0x00000007 ( = 7 decimal)
0x00000008 ( = 8 decimal)
0x00000009 ( = 9 decimal)
0x00000010 ( = 16 decimal)
0x00000011 ( = 17 decimal)
0x00000012 ( = 18 decimal)

and so on.

Does anyone have any ideas?

Thank you.
0
Comment
Question by:luizblink182
6 Comments
 
LVL 1

Expert Comment

by:Mathematix
ID: 17970076
How would your required system represent 11 to 15 inclusive? If it can't do this then there is no solution.
0
 
LVL 15

Expert Comment

by:efn
ID: 17970534
I'm not sure I understand the question, so I am going to restate it and ask if my interpretation is correct.

Given an integer m, calculate an integer n such that the representation of m in decimal is the same numeral sequence as the representation of n in hexadecimal.

The application is that you have a number m you want to display and an output device that shows the hexadecimal representation of the number, but you want to be able to read the value from the device in decimal.  To do that, you would apply the algorithm and send n to the device.

Is this the problem?
0
 

Author Comment

by:luizblink182
ID: 17971556
I don't need to be showing any results, I basically only need an algorithm that will output 7, 8, 9, 16, 17, 18 and so on (witch would be 7, 8, 9, 10, 11, 12 and so on).
here's an example that would work out only up to 153 in decimal (99 in hex)

int auxiliar = 0;
unsigned _int16 sendvalue = 0;

for (int decvalue = 0; decvalue < 154; decvalue++)
{
     if(aux == 9)
     {
           sendvalue = sendvalue + 6; // this skips the hexadecimal char digits (A, B, C, D, E and F)
           auxiliar = 0;
     }
 
     Outport32(0x378,sendvalue); // this will send the value to the system

     sendvalue++;

     auxiliar++;
}

Everytime the auxiliar reaches 10, it adds 6 to sendvalue to skip the char digits in hexadecimal. Unfortunatelly the code above only works up to 99 in hex, because 99 in hex is equal to 153 in decimal, and 100 in hex (witch should be the next number to be displayed) is equal to 256 in hex.

I hope now it's understandable, and I'm sorry for my bad english.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Accepted Solution

by:
alkisg earned 500 total points
ID: 17971585
You may use the built-in functions for this. Suppose your number is 14.
First, you convert it into a string, "14".
Then you add "0x" in front, so it becomes "0x14".
And then you use the sscanf function to convert it into the respective decimal integer, which is 20.

#include <stdio.h>

int dec2hex(int i)
{
        char buffer[20];
        int result;

        sprintf(buffer, "0x%d", i);
        sscanf(buffer, "%X", &result);

        return result;
}

int main(void)
{
        printf("%X\n", dec2hex(14));

        return 0;
}
0
 
LVL 4

Expert Comment

by:Raymun
ID: 17971586
To do it manually, multiply each digit by its hex weight and add.

examples:

0x12 = (1 x 16^1) + (2 x 16^0) = 18
0x4236 = (4 x 16^3) + (2 x 16^2) + (3 x 16^1) + (6 x 16^0) = 16950

or just simply assign to type int:

_int16 x16 = 0x1234;
int x10 = x16;

x10 will hold 4660;

-ray
0
 
LVL 15

Expert Comment

by:efn
ID: 17972910
It now looks like the problem is to generate a sequence of numbers, skipping all that have a non-decimal digit in their hexadecimal representation.  The solution could be represented like this:

for (unsigned _int16 n = startvalue; n < endvalue; ++n)
{
  if (!hasHexDigit(n))
  {
    Outport32(0x378, n);
  }
}

If this is the correct interpretation of the problem, the remaining problem is to write the hasHexDigit function.  A hex digit is represented by four bits, so it will be convenient to do this with bit manipulation.  Example pseudocode:

bool hasHexDigit(unsigned _int16 n)
{
  for each 4-bit nibble in n
    if nibble > 9
      return true
  return false
}

This design is somewhat inefficient, since once it hits 0xA, it is not smart enough to skip to 0x10.  You could remedy that by having hasHexDigit identify which nibble had the hex digit.  Then the main loop could add 6 to that nibble to skip past the next set of numbers it would skip anyway.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question