-2

I'm loosing my mind at the moment and below is what I'm trying to do.

char* buffer;
sprintf(buffer, "0x%08x", 5);
*(int *)(0x834AF2AC + 0x1a) = ?buffer?;

Buffer = 0x05000000

I need to set that in memory, if I just set 05 it will set 0x00000005

Question asked better. How can I convert an INT into a format of "0x%08x" So 5 becomes 0x05000000

ANSWERD: The correct answer is *(int *)(0x834AF2AC + 0x1a) = 5<<24;

user2887749
  • 79
  • 1
  • 2
  • 8
  • 2
    You have bigger problems than converting to `int`. You are writing to some location you shouldn't be writing to. You need to initialize `buffer`. – juanchopanza Nov 19 '13 at 06:14
  • So, `int x = 0x05000000;` doesn't work for you? – Retired Ninja Nov 19 '13 at 06:18
  • Why do you need the `0x` in your string? Are you looking for a generic way to parse a string that represents an integer? Do you want to consider octals, too? What about longs? – Ray Toal Nov 19 '13 at 06:20
  • xx and xxx shall be ignored in this question. – user2887749 Nov 19 '13 at 06:21
  • 1
    So, it still doesn't really make sense. Do you have a string you need to parse or an int you're trying to make larger? – Retired Ninja Nov 19 '13 at 06:38
  • My question is very simple, I'd like to convert 5 into 0x05000000 as an integer. Why this is requested is unimportant, I cannot set it any other way. Yes I know it makes little sense. – user2887749 Nov 19 '13 at 06:43
  • 1
    So the string has absolutely nothing to do with it? `int x = 5 << 24;` – Retired Ninja Nov 19 '13 at 06:47
  • Look: *(int *)(0x834AF2AC + 0x1a) = 0x05; < does not work *(int *)(0x834AF2AC + 0x1a) = 0x05000000; < does work but my values not static, its going to be dynamic from a param so I'll need to convert integers as "1", "2", "3" so on into the working format. Yes it sets memory but according to the game not how it wants it set. – user2887749 Nov 19 '13 at 06:54
  • 1
    Because you're putting it in the wrong address. It would be much easier to get a good answer if you explained why you're doing this and didn't confuse the issue with the `int -> string -> int` nonsense. – Retired Ninja Nov 19 '13 at 06:58
  • What do you mean I'm putting it in the wrong address? I'm not understanding why you need to understand my app to answer how to convert 5 to 0x05000000 – user2887749 Nov 19 '13 at 07:00
  • `0x5000000 == (5 << 24)` Assuming your numbers are 255 or less then you're off by 3 bytes and should be using a char pointer rather than an int pointer. Explaining why you want to do something completely avoids the entire xy problem you have here resulting in answers that are not helpful to you. – Retired Ninja Nov 19 '13 at 07:09
  • What is everyone talking about in my X problem? I'm so confused right now. That address is valid, very valid I can read / write to it. – user2887749 Nov 19 '13 at 07:10
  • Listen, I have no clue why 0x05 is not working over 0x05000000 but all I know is it works okay? I've tried to use a char pointer and it wont set the value now. – user2887749 Nov 19 '13 at 07:14
  • Thank you, the answer is *(int *)(0x834AF2AC + 0x1a) = 5<<24; :) – user2887749 Nov 19 '13 at 07:29

3 Answers3

2

Something like this:

#include <iostream> // for std::cout, std::endl
#include <string>   // for std::string, std::stoi
int main()
{

  std::string s{"0x05"};
  int i = std::stoi(s, nullptr, 16); // convert base 16 number in s to int
  std::cout << i << std::endl;

}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Not going to work, thanks anyway. I've updated my question again. – user2887749 Nov 19 '13 at 06:32
  • @user2887749 Your question makes no sense. An `int` is an `int`. `0x0005` is exactly the same as `5`. – juanchopanza Nov 19 '13 at 06:36
  • I understand that, but according to the game I am writing memory to it is invalid and not okay to do so. – user2887749 Nov 19 '13 at 06:44
  • @user2887749 that could well be the fact that you have not allocated any memory for your buffer, as I mentioned in the very first comment. – juanchopanza Nov 19 '13 at 06:45
  • Hmm, I'm having trouble understanding and thanks for your help man. – user2887749 Nov 19 '13 at 06:47
  • Look: *(int *)(0x834AF2AC + 0x1a) = 0x05; < does not work *(int *)(0x834AF2AC + 0x1a) = 0x05000000; < does work but my values not static, its going to be dynamic from a param so I'll need to convert integers as "1", "2", "3" so on into the working format. – user2887749 Nov 19 '13 at 06:48
0

I'm not sure if I understand correctly but if you want to convert an entire string to int, then I would suggest stringstream.

http://www.cplusplus.com/reference/sstream/stringstream/stringstream/

For hexadecimal string:

#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::stringstream

int main () {

  std::stringstream ss;

  ss << std::hex << 0x05;

  int foo;
  ss >> foo;

  std::cout << "foo: " << foo << '\n';

  return 0;
}
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
0

Two result from google which points to stackoverflow (result 1 and 2).

Convert char to int in C and C++

C char* to int conversion

Community
  • 1
  • 1
frank
  • 147
  • 6