reading binary files as strings

For questions about using Classic.

Post » Sun Jan 31, 2010 7:04 pm

well, I'm trying to read a string from a jpeg file. the thing is, some image editors save a lot of useful data in the file (exif) which i'd like to read from my app. i was trying to open jpeg with editbox's load file action, no go. also i'm afraid this would be not so efficient. so is there any way to accomplish this one? the best way i'd like to do it is somehow read jpegs like windows' notepad does when you opens them. thanks in advance.
B
81
S
50
G
10
Posts: 555
Reputation: 13,009

Post » Sun Jan 31, 2010 7:45 pm

This is probably a job for The Pythons
B
3
S
2
G
4
Posts: 1,445
Reputation: 4,665

Post » Tue Feb 02, 2010 12:22 am

well, how do I?

i thought i'm fairly familiar with python, until my simple

[code:3fsqu5d5]f = open("new.jpg", "r")
text = f.read()
f.close()
EditBox.SetText(text)[/code:3fsqu5d5]

did return some bull:!:hit. again, i'd like to read jpeg into editbox JUST LIKE windows' notepad does, halp plox :?

edit: "rb" mode also does not work

edit2: of course .txt file are read with no problems :/
B
81
S
50
G
10
Posts: 555
Reputation: 13,009

Post » Tue Feb 02, 2010 12:47 am

Hmm... using the Text Manipulator I can read the first line of a .jpg file (and it looks exactly like if you open in Notepad) but I can't seem to figure out how to read the rest of the lines :?
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Tue Feb 02, 2010 1:29 am

You need to replace the NULL bytes (which automatically terminates a string) with something else.

For example:

f = open("python_error.jpg", "rb")
text = f.read().replace('\x00','\x32')
f.close()
EditBox.SetText(text)

Example .cap: [url:23zw79yn]http://www.box.net/shared/rkknb1p4uk[/url:23zw79yn]
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Tue Feb 02, 2010 3:59 pm

[quote="scidave":lf78yhts]You need to replace the NULL bytes (which automatically terminates a string) with something else.

For example:

f = open("python_error.jpg", "rb")
text = f.read().replace('\x00','\x32')
f.close()
EditBox.SetText(text)

Example .cap: [url:lf78yhts]http://www.box.net/shared/rkknb1p4uk[/url:lf78yhts][/quote:lf78yhts]


you, dear sir, just won one free internet. big love!
B
81
S
50
G
10
Posts: 555
Reputation: 13,009


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 6 guests