read file in binary mode

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
blue83
Posts: 157
Joined: 11 Apr 2018, 06:38

read file in binary mode

27 Feb 2021, 05:05

How to read file .jpg or .pdf in binary mode to be ready for upload with API?

thanks,
blue
User avatar
Smile_
Posts: 859
Joined: 03 May 2020, 00:51

Re: read file in binary mode

27 Feb 2021, 05:51

Code: Select all

Filename := "path\to\the\file"
File := FileOpen(Filename, "r")
File.RawRead(BinData, FileLen := File.Length())

Msgbox % Filename " size: " FileLen " bytes`nFirst 2 bytes: " NumGet(BinData, 0, "UChar") " " NumGet(BinData, 1, "UChar")
blue83
Posts: 157
Joined: 11 Apr 2018, 06:38

Re: read file in binary mode

27 Feb 2021, 06:23

Hi,

Thank you for answers, but still I am missing something.

In Python code is:

Code: Select all

file = open("Capture.jpg", 'rb')
print(file)

Output is: <_io.BufferedReader name='Capture.jpg'>
That works, but I am trying to translate that code into ahk.

Caputre.jpg has 49KB, but after uploading with API has 50 KB, and can not be open.

Thanks,
blue
just me
Posts: 9560
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: read file in binary mode

27 Feb 2021, 06:43

I don't know Python, but I assume we don't need the output of print(file). What a you trying to do with the read sata?
blue83
Posts: 157
Joined: 11 Apr 2018, 06:38

Re: read file in binary mode

27 Feb 2021, 06:46

I am trying upload file using API.

For now my biggest problem is when I read that file like you and @Smile_ said I get some strange Chinese sings?!?

Blue
just me
Posts: 9560
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: read file in binary mode

27 Feb 2021, 06:52

When do you get 'strange signs'?
blue83
Posts: 157
Joined: 11 Apr 2018, 06:38

Re: read file in binary mode

27 Feb 2021, 06:56

When I put variable in msgbox.
just me
Posts: 9560
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: read file in binary mode

27 Feb 2021, 06:58

You cannot display binary data with a MsgBox or other AHK commands/functions expecting a string.
User avatar
Smile_
Posts: 859
Joined: 03 May 2020, 00:51

Re: read file in binary mode

27 Feb 2021, 08:51

Take a look
Python:

Code: Select all

import binascii
import os 
cwd = os.path.dirname(os.path.realpath(__file__))
File = open(cwd + "\\Capture.jpg", 'rb')
print(binascii.hexlify(File.read()))
Auto-Hotkey:

Code: Select all

Filename := "Capture.jpg"
File := FileOpen(Filename, "r")
File.RawRead(BinData, FileLen := File.Length())
Loop, % FileLen
    Out .= Format("{:X}", NumGet(BinData, A_Index - 1, "UChar"))
FileAppend, b'%Out%', Out.txt
The python output and the text file content 'Out.txt' (Auto-Hotkey output) are the same, so I think the problem some where else in your code.
blue83
Posts: 157
Joined: 11 Apr 2018, 06:38

Re: read file in binary mode

27 Feb 2021, 12:55

Thank you @Smile_ for clarification.

I was 99% sure that this is the reason, but I will try to find another way.

At the end this is good post for someone else with same problem.

Blue

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Chunjee, Draken, Google [Bot] and 102 guests