Convert String to an Integer

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2118 Views - Last Post: 03 February 2009 - 12:49 PM Rate Topic: -----

#1 parkour86   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 26-January 09

Convert String to an Integer

Posted 02 February 2009 - 06:36 PM

I am trying to pass the string variable bar to a pointer foo. I have found that the only way to pass anything through ChangeMemVal is to use an integer. I am getting an error when I run this. For some reason it won't let me put foo equal to bar. Any help would be great thanks.


int main()
{
	string bar = "0x00EB0C62";

	int *foo = bar;

	ChangeMemVal("pinball.exe", (void*) foo, "100000", 4); 

	return 0;
}


This post has been edited by parkour86: 03 February 2009 - 09:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Convert String to an Integer

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Convert String to an Integer

Posted 02 February 2009 - 06:46 PM

See cstdlib (stdlib.h) specifically atoi
Was This Post Helpful? 0
  • +
  • -

#3 parkour86   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 26-January 09

Re: Convert String to an Integer

Posted 02 February 2009 - 06:55 PM

View Postn8wxs, on 2 Feb, 2009 - 05:46 PM, said:

See cstdlib (stdlib.h) specifically atoi


Thanks for the quick response. I have tried to use atoi but all that comes out is 0. I believe it's because there are letters in the string.
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Convert String to an Integer

Posted 02 February 2009 - 07:24 PM

To convert hexadecimal values, use strtol
Was This Post Helpful? 0
  • +
  • -

#5 parkour86   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 26-January 09

Re: Convert String to an Integer

Posted 03 February 2009 - 09:04 AM

View Postn8wxs, on 2 Feb, 2009 - 06:24 PM, said:

To convert hexadecimal values, use strtol


Alright, I used strtol but it converted the hex value to decimal. I want foo to be equal to bar.

For example:

string bar = "0x00EB0C62";
int *foo = bar;


So in the end
int foo = 0x00EB0C62;
Was This Post Helpful? 0
  • +
  • -

#6 Plus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Convert String to an Integer

Posted 03 February 2009 - 09:59 AM

:: your theory is wrong,

:: int take a hexadecimal 0x0000FFFF but it need a conversion,

 int foo = (int)0x0000FFFF; 


:: a simple cast should do it,

:: you can define memory addresses in hex using preprocessor-style,

 #define __FOO 0x0000FFFF 


:: to print your hexadecimal use this,

 cout << hex << foo; 


:: hopefully this helps ...

This post has been edited by Plus: 03 February 2009 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

#7 parkour86   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 26-January 09

Re: Convert String to an Integer

Posted 03 February 2009 - 10:06 AM

View PostPlus, on 3 Feb, 2009 - 08:59 AM, said:

:: your theory is wrong,

:: int take a hexadecimal 0x0000FFFF but it need a conversion,

 int foo = (int)0x0000FFFF; 


:: a simple cast should do it,

:: you can define memory addresses in hex using preprocessor-style,

 #define __FOO 0x0000FFFF 


:: to print your hexadecimal use this,

 cout << hex << foo; 


:: hopefully this helps ...



Ok that makes since. So your saying I should set it up like this?

string bar = "0x00EB0C62";
int foo = (int)bar;



I don't want to use #define because in the end i'm going to be reading the addresses from a text file.

This post has been edited by parkour86: 03 February 2009 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

#8 parkour86   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 26-January 09

Re: Convert String to an Integer

Posted 03 February 2009 - 10:38 AM

Alright I have compiled the code as such:

int main()
{
	string bar = "0x00EB0C62";

	int foo = (int)bar;
 
	cout << hex << foo;
	ChangeMemVal("pinball.exe", (void*) foo, "100000", 4);

	return 0;
}



When I run it the output is a hex a decimal number that is not even close to the origanal one.
Here's what foo outputted: 22fc80
Was This Post Helpful? 0
  • +
  • -

#9 matthew180   User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

Re: Convert String to an Integer

Posted 03 February 2009 - 10:40 AM

View Postparkour86, on 3 Feb, 2009 - 08:04 AM, said:

So in the end
int foo = 0x00EB0C62;


Why don't you just set foo then?

int foo = 0x00EB0C62;

Which is the same as:

int foo = 15404130;

A number is a number, no matter what format you choose to view it in. Someone could probably help you better if you post the prototype for the ChangeMemVal() function, since it looks like your real problem is giving that function what it is looking for.

Matthew
Was This Post Helpful? 0
  • +
  • -

#10 parkour86   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 26-January 09

Re: Convert String to an Integer

Posted 03 February 2009 - 11:19 AM

View Postmatthew180, on 3 Feb, 2009 - 09:40 AM, said:

Why don't you just set foo then?

int foo = 0x00EB0C62;

Which is the same as:

int foo = 15404130;

A number is a number, no matter what format you choose to view it in. Someone could probably help you better if you post the prototype for the ChangeMemVal() function, since it looks like your real problem is giving that function what it is looking for.

Matthew


I don't want to just set foo equal to 0x00EB0C62 because I will be reading the hex addresses in from a text file.


Here's the ChangeMemVal function

bool ChangeMemVal(const char * ProcessName, LPVOID MemAddress, int NewVal, int size)
{
	 int OldVal;
	 HANDLE hProcessSnap;
	 HANDLE hProcess = NULL;
	 PROCESSENTRY32 pe32;	
	 hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
	 pe32.dwSize = sizeof( PROCESSENTRY32 );
	 Process32First(hProcessSnap, &pe32);
	 do
	 {		  
		  if(!strcmp(pe32.szExeFile, ProcessName))
		  {
			   hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
			   break;
		  }
	 }
	 while(Process32Next(hProcessSnap, &pe32));
	 CloseHandle( hProcessSnap );
	 if(hProcess != NULL)
	 {
		  ReadProcessMemory(hProcess, MemAddress, &OldVal, size, NULL);
		  cout << MemAddress << "\nOld Value: " << OldVal << endl;
		  WriteProcessMemory(hProcess, MemAddress, &NewVal, size, NULL);	 // write the value
		  cout << "New Value: " << NewVal << endl;
		  CloseHandle(hProcess);	
		  return true;
	 }
	 cout << "\nAn error occured while attempting to modify address.\n";
	 return false;
}


Was This Post Helpful? 0
  • +
  • -

#11 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Convert String to an Integer

Posted 03 February 2009 - 11:25 AM

View Postparkour86, on 3 Feb, 2009 - 01:36 AM, said:

I am trying to pass the string variable bar to a pointer foo. I have found that the only way to pass anything through ChangeMemVal is to use an integer. I am getting an error when I run this. For some reason it won't let me put foo equal to bar. Any help would be great thanks.


int main()
{
	string bar = "0x00EB0C62";

	int *foo = bar;

	ChangeMemVal("pinball.exe", (void*) foo, "100000", 4); 

	return 0;
}



Try using the following - based on your original post


#include <sstream>   // for std::istringstream
#include <iomanip>  // for std::hex
#include <string>  // for std::string

int main()
{
	std::string bar = "0x00EB0C62";
	int foo;

	std::istringstream(bar) >> std::hex >> foo;

	return 0;
}

This post has been edited by Bench: 03 February 2009 - 11:26 AM

Was This Post Helpful? 0
  • +
  • -

#12 parkour86   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 26-January 09

Re: Convert String to an Integer

Posted 03 February 2009 - 11:37 AM

It returns 15404130. Not sure how it got that. I'm starting to believe that it's impossible to copy a string value into an integer.

To bad I just can't do this:

string bar = "0x00EB0C62";
int foo = bar;

cout << foo;


Was This Post Helpful? 0
  • +
  • -

#13 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Convert String to an Integer

Posted 03 February 2009 - 12:03 PM

View Postparkour86, on 3 Feb, 2009 - 06:37 PM, said:

It returns 15404130. Not sure how it got that. I'm starting to believe that it's impossible to copy a string value into an integer.

To bad I just can't do this:

string bar = "0x00EB0C62";
int foo = bar;

cout << foo;

The number you see is absolutely 100% correct...

your string bar is a hexadecimal representation of the value you want to convert to an int. 15404130 is 0x00EB0C62 in base-10.

Hexadecimal, Decimal, Octal, Binary.. etc are all just representations of numbers. (computers only understand base-2). see the output of this program

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <bitset>

int main()
{
    std::string bar = "0x00EB0C62";
    int foo;

    std::istringstream(bar) >> std::hex >> foo;

    std::cout << foo << std::endl;
    std::cout << std::hex << foo << std::endl;
    std::cout << std::oct << foo << std::endl;
    std::cout << std::bitset<32>(foo) << std::endl;

    return 0;
} 


Quote

15404130
eb0c62
72606142
00000000111010110000110001100010
These 4 numbers shown are exactly the same.



edit: changed bitset range to fit all the bits in

This post has been edited by Bench: 03 February 2009 - 12:08 PM

Was This Post Helpful? 1
  • +
  • -

#14 matthew180   User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

Re: Convert String to an Integer

Posted 03 February 2009 - 12:26 PM

View Postparkour86, on 3 Feb, 2009 - 10:37 AM, said:

It returns 15404130. Not sure how it got that. I'm starting to believe that it's impossible to copy a string value into an integer.


15,404,130 in DECIMAL (base 10) *is* 0x00EB0C62 in HEXADECIMAL. They are the same number. In the example just given to you, the value is being output as decimal instead of hex, but the computer does not care, a number is a number.

Try this (tested on my unix box):

#include <stdio.h>
#include <string.h>	// memcpy
#include <stdlib.h>	// strtol
#include <limits.h>	// strtol
#include <sys/types.h> // size_t

size_t fake_read(char *buf, size_t len);

int
main(void)
{
	char ascii_num[20];
	int address;
	size_t bytes_read;

	// Simulate reading number from a text file...
	bytes_read = fake_read(ascii_num, sizeof(ascii_num));

	// The data from a file read is not a null-terminated string,
	// so null terminate the ASCII hex number for strtol().
	ascii_num[bytes_read] = 0;

	// Convert to a real binary number.
	address = strtol(ascii_num, NULL, 16);

	printf("Original string: %s\n", ascii_num);
	printf("Hex number: 0x%08X\n", address);
	printf("Dec number: %d\n", address);

	return(0);
}
// main()



size_t
fake_read(char *buf, size_t len)
{
	len = 10;
	memcpy(buf, "0x00EB0C62", len);
	return len;
}
// fake_read()



Matthew
Was This Post Helpful? 1
  • +
  • -

#15 parkour86   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 26-January 09

Re: Convert String to an Integer

Posted 03 February 2009 - 12:43 PM

View Postmatthew180, on 3 Feb, 2009 - 11:26 AM, said:

View Postparkour86, on 3 Feb, 2009 - 10:37 AM, said:

It returns 15404130. Not sure how it got that. I'm starting to believe that it's impossible to copy a string value into an integer.


15,404,130 in DECIMAL (base 10) *is* 0x00EB0C62 in HEXADECIMAL. They are the same number. In the example just given to you, the value is being output as decimal instead of hex, but the computer does not care, a number is a number.

Try this (tested on my unix box):

#include <stdio.h>
#include <string.h>	// memcpy
#include <stdlib.h>	// strtol
#include <limits.h>	// strtol
#include <sys/types.h> // size_t

size_t fake_read(char *buf, size_t len);

int
main(void)
{
	char ascii_num[20];
	int address;
	size_t bytes_read;

	// Simulate reading number from a text file...
	bytes_read = fake_read(ascii_num, sizeof(ascii_num));

	// The data from a file read is not a null-terminated string,
	// so null terminate the ASCII hex number for strtol().
	ascii_num[bytes_read] = 0;

	// Convert to a real binary number.
	address = strtol(ascii_num, NULL, 16);

	printf("Original string: %s\n", ascii_num);
	printf("Hex number: 0x%08X\n", address);
	printf("Dec number: %d\n", address);

	return(0);
}
// main()



size_t
fake_read(char *buf, size_t len)
{
	len = 10;
	memcpy(buf, "0x00EB0C62", len);
	return len;
}
// fake_read()



Matthew



When you put it that way it makes since. I passed the numbers that are equavalent to the hex and it worked like a charm. I didn't know you could just turn the hex into a decimal and run it through the function. Thanks so much for all your guys' help.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2