+ Reply to Thread
Results 1 to 7 of 7

Need help converting Hex to Long / Reading file contents at binary level

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Post Need help converting Hex to Long / Reading file contents at binary level

    I'm trying to read consecutive bytes to a long variable.

    BACKGROUND
    I have code that can do this on a file. However this code is too slow for me as each file usually needs multiple reads. So I decided I could greatly speed this up by reading each file once into an array then searching the array instead. So I needed code that can do this conversion on an array instead.

    And this is what I came up with:
    Please Login or Register  to view this content.

    THE PROBLEM
    But there's a catch. Before ditching the original code and replacing with my shiny new function, I decided to test a few thousand files with both methods to see whether they return the same result.

    I tested over 3,000 files. And both methods returned the same result for every file tested...except three.

    My new function does NOT return the correct value for these three files.

    So I am looking for any forum user who has experience with reading files / converting Hex to Long. I am hoping he/she can help me work out why my function falls over on these files.

    (I cant upload the files however I can provide the hex values at the exact point where the functions differ - I will do this in the next post)
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Need help converting Hex to Long / Reading file contents at binary level

    Per the OP - I am uploading the details of the three files which I will refer to from now on as File01, File06 and File09.

    I have provided for each file:
    1. the read position at which the first variance occurs
    2. the long result (aka Header) for each method (class is the original method, NEW = my new function)
    3. the hex values for four bytes from the read position onwards



    File01
    09:28:49 class method - position(base0):=5357 Header:=101118975
    09:28:49 NEW method - position(base0):=5357 Header:=6747135

    Hex Values (starting from this position): FF F3 06 06


    File06
    09:28:49 class method - position(base0):=5478 Header:=285671167
    09:28:49 NEW method - position(base0):=5478 Header:=18284287

    Hex Values (starting from this position): FF FE 06 11


    File09
    09:28:49 class method - position(base0):=5614 Header:=-503120385
    09:28:49 NEW method - position(base0):=5614 Header:=237174271

    Hex Values (starting from this position): FF FD 02 E2


    Optional - In case it helps - Further background on what I am doing: http://www.excelforum.com/excel-prog...his-works.html
    Last edited by mc84excel; 07-30-2015 at 10:18 PM. Reason: provide additional details

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Need help converting Hex to Long / Reading file contents at binary level

    I am thinking I can improve the response rate of this thread if I upload a workbook to demonstrate the problem. See attached workbook - you can run it on any file you like.

    For the purpose of this test I will be using one of the sample pictures that come with Windows 7.

    If I run the workbook on Koala.jpg at position 0 I get results of -520103681 for both methods.

    If I run the workbook on Koala.jpg at position 255 I get two different results. Why?
    Attached Files Attached Files

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Need help converting Hex to Long / Reading file contents at binary level

    Quote Originally Posted by mc84excel View Post
    I am thinking I can improve the response rate of this thread if I upload a workbook to demonstrate the problem....
    Aha..., you finally got the password
    To be honest, if I opened a thread, and I just saw just a bunch of words or just screenshots, I immediately hit the x button on the upper right corner of this tab on my browser.


    The problem of your function is at :
    Please Login or Register  to view this content.
    Hex(avarBuffer(lngReadPos + something))
    So lets say the value of avarBuffer is 10, return of Hex(10) is just "A", but actually you need "0A" (with leading zero)



    So your code should be like :
    Please Login or Register  to view this content.


    But, then again, you are "in hurry mode" (processing thousand of files), so why use multiple steps to convert the array to string first (using hex function), and then convert this string back to long ?
    We know string operation is much longer than arithmetic operation.
    So, you can use this function instead :
    Please Login or Register  to view this content.

    Test speed file :
    EF1096752b.xls



    Regards
    Last edited by karedog; 08-05-2015 at 05:19 AM.

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Need help converting Hex to Long / Reading file contents at binary level

    Quote Originally Posted by karedog View Post
    Aha..., you finally got the password
    To be honest, if I opened a thread, and I just saw just a bunch of words or just screenshots, I immediately hit the x button on the upper right corner of this tab on my browser.
    Heh Without mentioning names I know one forum user who takes the opposite view - if he sees the thread contains a few words and an attached workbook, he wont bother looking further!

    Quote Originally Posted by karedog View Post
    So lets say the value of avarBuffer is 10, return of Hex(10) is just "A", but actually you need "0A" (with leading zero)
    :slaps forehead: so that's the problem. Thank you very much karedog :reps:

    I am very impressed that you not only identified the problem but you also provided a working function.

    And as if that wasn't enough, you provided an alternative version of the function which you claim is even faster!
    (I haven't speed tested them against one another yet. When I get to another computer with the main project on it - I will try them both out and eventually post the results back to this thread)
    Last edited by mc84excel; 08-03-2015 at 08:22 PM.

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Need help converting Hex to Long / Reading file contents at binary level

    Quote Originally Posted by mc84excel View Post
    Heh Without mentioning names I know one forum user who takes the opposite view - if he sees the thread contains a few words and an attached workbook, he wont bother looking further!
    WHAAT ??!! So I have a negative twin of me ?
    I always told Zeus to never create such thing. Now, all humankind is totally in danger.


    Quote Originally Posted by mc84excel View Post
    I am very impressed that you not only identified the problem but you also provided a working function.
    And as if that wasn't enough, you provided an alternative version of the function which you claim is even faster!
    Tell me, do you think I will win the Nobel Prize this year ?


    Regards

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Need help converting Hex to Long / Reading file contents at binary level

    @ karedog
    LOL Probably not You see the most I was hoping for was for someone to tell me where I was going wrong.

    And one of my pet peeves is when I solve someones thread and they just say 'thanks' (sometimes not even that) and worse - they cant be bothered to spend one second to rep you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Converting large numbers into binary
    By excelerat8r in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-01-2014, 03:24 PM
  2. Importing a Binary File, displaying as Hex, and saving the file as a Binary file again
    By bluenose616 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-26-2014, 04:46 PM
  3. [SOLVED] reading contents of a file without opening the file using INDIRECT function
    By luv2glyd in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-22-2013, 01:55 PM
  4. Reading a binary file
    By Andrew-R in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-13-2012, 10:16 AM
  5. Reading the last few numbers of a long number
    By gannon_w in forum Excel General
    Replies: 3
    Last Post: 10-20-2010, 11:30 AM
  6. #Num Error while converting nos above 512 into BINARY
    By e4excel in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 06-22-2010, 11:45 AM
  7. Reading binary file
    By Maxi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-14-2006, 07:20 PM
  8. Formula for converting Binary to Hexadecimal
    By Teraawete Tekena in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 03-23-2006, 03:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1