printing bytes from memory (NOOB question)

This is a discussion on printing bytes from memory (NOOB question) within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm new to C++ so please help me... I have an address in memory, and I want to print ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    9

    Question printing bytes from memory (NOOB question)

    Hi,
    I'm new to C++ so please help me...

    I have an address in memory, and I want to print out 100 bytes from this address on.
    one by one in HEX.

    How do I do that?


    Thanks

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    You'll probably cause your program to crash. But, if you want to try, show us what you have and we'll go from there.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    ok...
    The pointer to the data is in the second cell in a given array (parameter to my function). and I take it like this:

    Code:
    void myFunction(void** const args)
    {
      DWORD32* datapointer = (DWORD32*)(args[1]);
      // if I print out this datapointer I get good address, I checked with some memview.
    
      for (int i=0; i<100; i++)
      {
        int val = (int)(*datapointer);  // the values that I get in val, are not what I expect...
        datapointer++;
      }
    }
    All this code is trying to everything with int... and I want byte by byte...
    no idea how to do that...

    seems that I'm way off...
    Thanks for your help!

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    It would be easier to think about if you had the pointer passed as a separate arg.
    Code:
    void myFunction(DWORD32 *datapointer)
    {
      // use datapointer
    }
    It would also be a lot easier if you accessed datapointer as an array of DWORD32s rather than doing pointer arithmetic. Pointer arithmetic can be confusing even for the best of us.
    Code:
    void myFunction(DWORD32 *datapointer)
    {
      for (int i=0; i<100; i++)
      {
        DWORD32 val = datapointer[i];
        // print val
      }
    }
    But remeber that DWORDs are 4 bytes, so you could use uint8_t, a type defined in stdint.h.
    Code:
    #include <stdint.h> // required for uint8_t
    
    void myFunction(uint8_t *datapointer)
    {
      for (int i=0; i<100; i++)
      {
        uint8_t val = datapointer[i];
        cout << (unsigned int)val;
      }
    }
    Last edited by User Name:; 01-25-2011 at 03:24 PM.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    Thanks! it was VERY helpful!

    now maybe we can go on with the last thing that I have here...

    On this array I have 4 bytes that I don't need (just skip them)
    After that, I have a string (terminated by 0), I need to print it out.
    after that, another 4 useless bytes.
    and after that, an int that I need to print too...

    please please....
    THANKS!

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    bump

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    comon if this is soooo easy, can someone post a short code that does that?
    I'll pay $1000... well ok not.. but I'll be thankful

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    Yeah, so easy, we're waiting for you to make a stab at it.

    You've already got all the components you need, all it now needs from you is a bit of application to the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  3. Another Question...memory allocation
    By quickclick330 in forum C Programming
    Replies: 4
    Last Post: 12-12-2007, 03:25 PM
  4. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  5. Question regarding constructors and memory.....
    By INFERNO2K in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2005, 11:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21