1

I need to create a program when it run it should extract a image file. to do this I I used a char array to store the data. ex:

char data[]="ÿØÿà......";

I opened the image with a hex editor and copied the data and pasted it as above. but it gives many errors. (that may be because the image data have some bytes that ascii charactors are not available. ex: nul,)

con someone give me some advices on how to do this. how to create a byte array.

thanks in eny advice.

0

2 Answers 2

3

You should use a numeric initializer instead of a string literal... for example

const unsigned char data[] = { 0x01, 0x02, 0x03, 0x04,
                               0x05, 0x06, 0x07, 0x08 };

A simple way is writing a small script that generates the source code by reading the file... in Python it would be something like

data = open("datafile", "rb").read()
i = 0
while i < len(data):
    chunk = data[i:i+8]
    print ("0x%02x, " * len(chunk)) % tuple(map(ord, chunk))
    i += 8
Sign up to request clarification or add additional context in comments.

2 Comments

By the way, you can also generate that declaration using e.g. xxd -i (included in most Linux distributions).
in c++ how can I store a hex string
2

Read the data from the file using fopen or fstream. If you want to embed the file in the exe using a resource compiler.

9 Comments

If I store the data as resource it is possible to access it using programs like resour hacker. I need to embed the image in the exe to prevent accessing it bu users.
@LakshanPerera you can't prevent users from access resources.
Then they just edit your binary. Unless you encrypt it in some one.
A determined enough attacker will find such resource anyway in the data segment of your executable. And if you encrypt it, he can still do a memory dump of the process once you decrypted it in memory (although it's not easy if you decrypt it only when you need it and erase the decrypted data immediately afterwards).
@LakshanPerera: as long as the "something else" is at most as long as the original string you can do that even without any knowledge of the executable format... look for the string "hello world" inside the executable and replace it with something else (using a hex editor); that's it. Looking for text is also easy because it's not so common to find contiguous ASCII characters in a binary file (tools like strings are designed with this assumption in mind). Embedded images are almost as simple to find, since they have recognizable headers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.