Very simple question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Very simple question

  1. #1
    Join Date
    Feb 2010
    Posts
    112

    Very simple question

    lets say i open .exe file in hex editor and copy 200 or 300 bytes and want to paste that in some C++ variable


    so which type should that variable be?



    is this ok:

    BYTE myPieceOfCode[300]={
    CB 8B E9 A7 64 18 8E 88 E7 C3 29 72 15 5E 56 57
    AD 32 61 71 EE 46 7B 04 23 02 3E 4A 21 EA 1E 88
    F7 32 56 22 A0 3C 8E F1 A9 3C BA 16 F8 94 3D EC
    C9 3B B4 B1 29 2C DE 12 1C 99 69 EF 1E 09 5C DF
    A8 79 D2 DB B0 64 60 C7 BF AE 74 40 16 7D 0A C9
    3F B3 4E B4 C3 D1 CD 90 91 C2 A3 6E 5F 4D 27 F2
    23 BE 1A 1D 4D 49 7F 59 DE FB 9D AC 66 75 4A 65
    F9 45 4B E3 5E 0B 89 F5 23 B5 86 A6 1E 70 94 1A
    D2 32 9A AC DC DB 05 F1 22 DB 85 3E 8E 44 DA 6C
    33 97 65 AA 4A 13 47 DE 19 0C FB D7 B2 A1 48 FB
    58 6F 69 AF 76 78 32 A2 F6 DB 68 34 E9 5B EF 3A
    D2 64 B4 54 C6 F1 9F 56 74 67 C6 3C 39 E4 6F 2A
    82 9E A2 51 57 5F 4D 16 5B CD 0A F0 74 5E 5E B1
    };

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Very simple question

    unsigned char.

  3. #3
    Join Date
    Feb 2010
    Posts
    112

    Re: Very simple question

    and BYTE can not do?

    also do i need to put commas in between like CB, 8B ,E9 ,A7 ,64, 18, 8E, 88 ,E7 ,57 ?

    do i need to put 300 commas manually?

    and also can i put like this BYTE * myPieceOfCode [] = {CB, 8B ,E9 ,A7 ,64, 18, 8E, 88 ,E7 ,57.....}; ???

    whats the difference?

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,127

    Re: Very simple question

    You can't copy and paste from a binary editor into a C++ variable. What are you really trying to do? Your post doesn't make sense.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Very simple question

    Quote Originally Posted by suncica2222 View Post
    and BYTE can not do?

    also do i need to put commas in between like CB, 8B ,E9 ,A7 ,64, 18, 8E, 88 ,E7 ,57 ?
    Your problem is not the type -- the real problem is that you are not aware of hexadecimal constants.
    Code:
    BYTE myPieceOfCode [] = {0xCB, 0x8B ,0xE9};
    Regards,

    Paul McKenzie

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Very simple question

    Quote Originally Posted by suncica2222 View Post
    and BYTE can not do?
    BYTE is the same as unsigned char

    Quote Originally Posted by suncica2222 View Post
    also do i need to put commas in between like CB, 8B ,E9 ,A7 ,64, 18, 8E, 88 ,E7 ,57 ?

    do i need to put 300 commas manually?
    ... or you could use some text editor...

    Quote Originally Posted by suncica2222 View Post
    ... and also can i put like this BYTE * myPieceOfCode [] = {CB, 8B ,E9 ,A7 ,64, 18, 8E, 88 ,E7 ,57.....}; ???
    Yes, you can. You also have to insert 0x before each byte...

    Quote Originally Posted by suncica2222 View Post
    ... whats the difference?
    Between what and what?
    Victor Nijegorodov

  7. #7
    Join Date
    Feb 2010
    Posts
    112

    Re: Very simple question

    Quote Originally Posted by VictorN View Post
    Yes, you can. You also have to insert 0x before each byte...

    Is the only way to write "0x" 300 times manually or is there some tool?

    And when I copy code from hex editor it copies offsets too,then I need to delete them manually,how can I avoid that?


    Quote Originally Posted by VictorN View Post
    Between what and what?

    Between:

    1. BYTE myArray[300];

    and

    2. BYTE * myArray[];

    The first needs to be size 300 al the time,and second can be any size?

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Very simple question

    Quote Originally Posted by suncica2222 View Post
    Is the only way to write "0x" 300 times manually or is there some tool?
    I prefer to use some text editor (for instance, an IDE editor allows you to use Replace command for a whole file or for selection)

    Quote Originally Posted by suncica2222 View Post
    Between:

    1. BYTE myArray[300];

    and

    2. BYTE * myArray[];

    The first needs to be size 300 al the time,and second can be any size?
    (1) is OK providing you need no more than 300 bytes
    (2) is wrong because you are trying to declare an array of pointers.
    The correct one would be:
    Code:
    BYTE  myArray[] = {...};
    Victor Nijegorodov

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Very simple question

    Quote Originally Posted by suncica2222 View Post
    Is the only way to write "0x" 300 times manually or is there some tool?
    You could write a program to do it for you I guess.....

  10. #10
    Join Date
    Feb 2010
    Posts
    112

    Re: Very simple question

    Thank you all for help so far!


    Quote Originally Posted by VictorN View Post
    I prefer to use some text editor (for instance, an IDE editor allows you to use Replace command for a whole file or for selection)
    So how would be exactly in MSVC++2008 to copy selection of an .exe and paste it in smoothly with ',' and"0x" {0xAA,0xAA....} as I want to do in this example?




    Quote Originally Posted by VictorN View Post
    (2) is wrong because you are trying to declare an array of pointers.
    The correct one would be:
    Code:
    BYTE  myArray[] = {...};

    So if I say

    BYTE * myArray;

    it is a simple pointer,but could I use this as array (myArray[x]) right there without using operator "new" on it?

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Very simple question

    Quote Originally Posted by suncica2222 View Post
    So how would be exactly in MSVC++2008 to copy selection of an .exe and paste it in smoothly with ',' and"0x" {0xAA,0xAA....} as I want to do in this example?
    Menu Edit -> Replace...

    Quote Originally Posted by suncica2222 View Post
    So if I say

    BYTE * myArray;

    it is a simple pointer,but could I use this as array (myArray[x]) right there without using operator "new" on it?
    No!
    Victor Nijegorodov

  12. #12
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Very simple question

    Quote Originally Posted by suncica2222 View Post
    So how would be exactly in MSVC++2008 to copy selection of an .exe and paste it in smoothly with ',' and"0x" {0xAA,0xAA....} as I want to do in this example?
    What's the problem? Replace every space with ",0x" (remove the quotes).
    So if I say

    BYTE * myArray;

    it is a simple pointer,but could I use this as array (myArray[x]) right there without using operator "new" on it?
    Just because you see a "*" doesn't mean "new" is to be used or even mean you have to explicitly declare a pointer.
    Code:
    void SomeFunction(BYTE *p, int number)
    {
    }
    
    //...
    BYTE whatever[10];
    SomeFunction(whatever, 10);  //
    Hexadecimal constants and arrays/pointers are basic fundamentals of the language. Are you using a C++ book?

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Feb 2010
    Posts
    112

    Re: Very simple question

    Thank you very much,you have been great help!!!

  14. #14
    Join Date
    Apr 2007
    Posts
    153

    Re: Very simple question

    Most hex editors have an export command that will do all this automatically for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center