Computing.Net > Forums > Programming > Convert Hex to Dec and vice versa

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Convert Hex to Dec and vice versa

Reply to Message Icon

Name: Selma Li
Date: October 24, 2003 at 10:34:31 Pacific
OS: Window 2000
CPU/Ram: 256
Comment:

Is there a function is C that converts a decimal number to a hex number and vice versa? I'm trying to access some data in memory with a known address.

I determined the physical address in decimal. Actually, do I even need the conversion for accessing address(is there a parameter that define the unit that the number is in?)

If you happen to know how to access data from address, that would be great too.

Suppose I have a value 268 at the address of 0x00000059. How do I get that value from know the address of 0x00000059?

Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: Infinite Recursion
Date: October 24, 2003 at 15:13:22 Pacific
Reply:

Seems like these conversion functions are in demand... Here are some functions in C++, they can easily be converted to C... basically just changing the output syntax, etc. Using these functions, you should be able to generate the reverse to get the other conversion function...


// Display a number in hex </br>

void display_hex(int x) </br>

{ </br>

  int i; </br>

  int nonzero = 0; </br>

</br>

  cout << "Hex:     "; </br>

  for(i = 7; i >= 0; i--) { </br>

    nonzero += (x >> (i * 4)) & 0xF; </br>

    if(nonzero) </br>

      print_hex_digit((x >> (i * 4)) & 0xF); </br>

  } </br>

</br>

  if(x == 0) </br>

    print_hex_digit(0); </br>

</br>

  cout << "\n"; </br>

} </br>

</br>

</br>

// Display a hex digit </br>

void print_hex_digit(int x) </br>

{ </br>

  switch(x) { </br>

  case 0: cout << "0"; break; </br>

  case 1: cout << "1"; break; </br>

  case 2: cout << "2"; break; </br>

  case 3: cout << "3"; break; </br>

  case 4: cout << "4"; break; </br>

  case 5: cout << "5"; break; </br>

  case 6: cout << "6"; break; </br>

  case 7: cout << "7"; break; </br>

  case 8: cout << "8"; break; </br>

  case 9: cout << "9"; break; </br>

  case 10: cout << "A"; break; </br>

  case 11: cout << "B"; break; </br>

  case 12: cout << "C"; break; </br>

  case 13: cout << "D"; break; </br>

  case 14: cout << "E"; break; </br>

  case 15: cout << "F"; break; </br>

  default: </br>

    cout << "Error, print_hex_digit accepts 0-15 only\n"; </br>

    exit(-1); </br>

    break; </br>

  } </br>

}</br>

IR



0

Response Number 2
Name: Infinite Recursion
Date: October 24, 2003 at 15:14:36 Pacific
Reply:

So much for using the code coverter... at any rate... take the br html code out and it should work for ya.

IR


0

Response Number 3
Name: Selma
Date: October 28, 2003 at 06:44:17 Pacific
Reply:

Thank you for your codes! I was able to C and it gave me the correct dec to hex conversion. Another question appears now.

I need to store those hex numbers of an address and put them into a variable so I can access the information at that address. But when I put the string of numbers together into a variable called 'AddString', I do not only has the hex number, but also some beginning and terminal symbols like this
0X*0|*0|*0|*0|*0| *0|*2|*5|, where the hex number is only 0X00000025. What can I do to get rid of the * and | ?

Here's my modified code for C.

int main()
{
int number=37;
int i;
char* y;
char AddString[15]="0X";
int nonzero = 0;

puts("Hex: ");

for(i = 7; i >= 0; i--)
{
nonzero += (number >> (i * 4)) & 0xF;
{
switch(((number >> (i * 4)) & 0xF))
{
case 0: y[1]='0'; puts("0");break; case 1: y[1]= '1'; puts("1");break;
case 2: y[1]= '2'; puts("2");break;
case 3: y[1]= '3'; puts("3");break;
case 4: y[1]= '4'; puts("4");break;
case 5: y[1]= '5'; puts("5");break;
case 6: y[1]= '6'; puts("6");break;
case 7: y[1]= '7'; puts("7");break;
case 8: y[1]= '8'; puts("8");break;
case 9: y[1]= '9'; puts("9");break;
case 10: y[1]='A'; puts("A");break;
case 11: y[1]='B'; puts("B");break;
case 12: y[1]='C'; puts("C");break;
case 13: y[1]='D'; puts("D");break;
case 14: y[1]='E'; puts("E");break;
case 15: y[1]='F'; puts("F");break;
default:
puts("Error, print_hex_digit accepts 0-15 only\n");
}
strcat(AddString, y);
}
}
printf("Answer is: %s\n", AddString);
return 0;
}

Thank you very much! Your help is being very appreciated!


0

Response Number 4
Name: Infinite Recursion
Date: November 5, 2003 at 20:26:43 Pacific
Reply:

sorry for delays on this, i actually lost this thread in the mycomputing.net feature.
have you got it working yet?

ir


0

Response Number 5
Name: Selma
Date: November 11, 2003 at 13:22:23 Pacific
Reply:

Yep, now I got rid of that problem.

However, when I try to access a memory location with the outputted address, it is not working. Is it because address is in Hex while the output of that program is in string? Is there any way to unwrap the string into a hex number?

Thanks!



0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Convert Hex to Dec and vice versa

convert for to while and vice www.computing.net/answers/programming/convert-for-to-while-and-vice/5589.html

Converting to and from binary. www.computing.net/answers/programming/converting-to-and-from-binary/5222.html

converting text to binary www.computing.net/answers/programming/converting-text-to-binary/4891.html