LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 05-30-2005, 06:26 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
Convert from hex to dec


I have been googling the entire lesson and for some reason everything I try fails.

I have a program that uses a memory address. I used to read this from the stack pointer but now I want to be able to type the address from the command line, as an argument to the program.

Now, this address is stored in a long, but how can I convert from the string argument to a long? Here goes the program in some pseudo code:

Code:
unsigned long getsp() {
      __asm__("movl %esp,%eax");
}

int main(int argc, char *argv[]) {

/* used this before, it shows the address of esp */
      long sp = getsp();
      printf("sp: 0x%x (%f)\n", sp, sp);

/* want to read from argument sent to program */
      long new = convert_from_hex_to_long(argv[1]);
      same printf as before;

}
Regards.
 
Old 05-30-2005, 06:35 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
use strtol() to convert a string from hex to a long
 
Old 05-30-2005, 08:17 AM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Quote:
Originally posted by jim mcnamara
use strtol() to convert a string from hex to a long
Already tried, here is why it fails:

An address is 8 'characters' long, the problem is that it works with up tp 7, then something strange happens:

Code:
$ cat convert.c
#include <stdlib.h>
#include <stdio.h>

unsigned long getsp() {
        __asm__("movl %esp,%eax");
}

int main(int argc, char *argv[]) {

/* used this before, it shows the address of esp */
        unsigned long sp = getsp();
        printf("sp: 0x%x (%d)\n", sp, sp);

/* want to read from argument sent to program */
        unsigned long test = strtol(argv[1], 0, 16);
        printf("test: 0x%x (%d)\n", test, test);
}
$ ./convert b 
sp: 0xbffff568 (-1073744536)
test: 0xb (11)
$ ./convert bf
sp: 0xbffff568 (-1073744536)
test: 0xbf (191)
$ ./convert bfff
sp: 0xbffff568 (-1073744536)
test: 0xbfff (49151)
$ ./convert bffff55
sp: 0xbffff568 (-1073744536)
test: 0xbffff55 (201326421)
$ ./convert bffff558
sp: 0xbffff558 (-1073744552)
test: 0x7fffffff (2147483647)
With the last one the long flips over, but it's decimal value is 16 less then the value of sp, so why can sp be stored but not the address I typed in?
Any ideas?
 
Old 05-30-2005, 10:42 AM   #4
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
strtol() returns a (signed) long, and 0xbffff558 is out of range for that. Try strtoul(), which returns an unsigned long.
 
  


Reply



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
How to convert a float to its binary or hex representation in Python? zero79 Programming 1 09-01-2005 10:19 AM
How to convert a float to its binary or hex representation in Python? zero79 Linux - Software 1 08-29-2005 09:30 PM
dec to hex ewt3y Linux - General 2 08-16-2005 12:01 PM
Hex socks Linux - General 4 02-17-2005 12:05 PM
hex? Nox Solaris Linux - Newbie 3 12-07-2002 08:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:57 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration