Jump to content











Photo
- - - - -

Batch to export Hex to a table


  • Please log in to reply
17 replies to this topic

#1 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 14 September 2011 - 04:50 PM

Ok.

If you have read the http://reboot.pro/2959/ CHS-LBA topic, you' ll understand there was an OT.
While I agree it was seen as troll, I didn't, at the time, know any other related topic.
This part of the forum, Appscripts, seems to have been at the beginning related to Winbuilder,
as I myself thought, as scripts usually have seemed to be. But in fact

they don't need to be exclusively related to WinBuilder

. (Readme first, by Staff)


Cool :)

Also I thought the batch would be straightforward, but then the trolling was long.

I had posted in that particular topic, the CHS-LBA translations one, as there were in the topic some
spreadsheets containing hexcode. Though my goal was not toward CHS-LBA,
there was talk of the issue of entering hex in a spreadsheet.
Also in the Fat_make xls (Wonko),

To import bootsector data:
1) Open a bootsector or floppy or volume image with Tiny Hexer
2) Select the whole sector
3) Run Tools->Scripts->Copy selection to clipboard as formatted hex data
4) Run Edit_>Paste to new
5) Paste "Text"
6) Edit->Find/Replace
7) Enter a single [SPACE] in the "Find" box and tick the "Find text" box
8) Click on Find and in the Replace box type 09
9) Click on Replace all
10) You will have 511 replacements done.
11) Select All
12) Copy
13) Select cell A1 in the Excel sheet
14) Paste special ->Text


Basically I agree there was a troll on my part, but it was toward the above.
Which was what I was trying to do. My goal was just toward hex-editing in general, but the quoted thing was for me that apparently there was no easy way.

It also gave me hope, as the question had been raised. (hex is not a Very popular thing)
(so I thought I'd post there, to share)

The batch

The above quoted text is toward a Tiny Hexer script. I would've gone Tiny Hexer, but I'd started.
So I thought I 'd share, as it can be useful.


The batch will create an html table. The aim is for it to be usable in Excel or other similar program.
At the moment it does create a working table. The posts by Wonko were also helpful in cleaning the batch, and making it simpler.

Find the info at http://reboot.pro/2959/page__st__25 (and Wonko's code)

Hope it helps.

R.

#2 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 14 September 2011 - 05:15 PM

(Wonko) in other topic : Using GOTO :EOF is NOT equivalent to a GOTO :label when a CALL is used.


me: IF NOT !Hex_Code!.==. (ECHO ^<td align=center^>^<p^>!Hex_Code!^</p^>^</td^>>>%htm%) ELSE GOTO :EOF
is the same (idea) as
if not %str%.==. goto _end
when _end contains echo this and that.

Yeah, I see the difference of what you mean.

#3 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 15071 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 14 September 2011 - 05:21 PM

Yeah, I see the difference of what you mean.


Good. :)

:cheers:
Wonko

#4 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 15 September 2011 - 10:58 AM

Ok, hi.

Tried the code you put (as before I thought it was a "helpful snippet").
Good :) .
Two things
-the two characters of each (byte? I always mix up the lingo at that point -though I know how it works) are in the same cell.
Having them seperated would be nice for other purposes.
-the copied 00s appear 0 in Excel, though there' probably a format thing which would work..

Which part of

SET /A Counter=0

FOR /L %%A IN (0,3,1200) DO (

SET Hex_Code=!hex_contents%:~%%A,2!

SET /A Counter+=1

is doing the counting (the "step")?
is it 'the' 3, or is it the Counter+=1? :frusty:

Thanks.
:cheers:,
R.


Edit;
Putting each in a cell is necessary.
eg F1 F2 is F1space F2space.
That's why I used to do repeats in the old code, so as to copy F copy 1 don't copy space. Then we echo </td><td>
Maybe in (your) new code,

(copy one, then </td><td>. Then copy second the same way.
Then " if multiple of 3 don't copy.')


:cheers:
r.

#5 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 15071 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 15 September 2011 - 11:34 AM

It is COMPLETELY unclear.

The given code was an example to show you how things are SIMPLER than what you initially thought.

Quite obviously the Counter is to give a SIZE (width) to the table, in the posted example it is 16, as in:

IF !Counter!==16 SET /A Counter=0 &ECHO ^</tr^>^<tr^>>>%htm%



Are you really-really sure that a SPACE is in the output table after the hex code?
I wonder what this does :unsure::

SET Hex_Code=!hex_contents%:~%%A,2!

(make sure you don't have a SPACE after the final "!")

But what would happen if you change (unneededly) this line:

IF NOT !Hex_Code!.==. (ECHO ^<td align=center^>^<p^>!Hex_Code!^</p^>^</td^>>>%htm%) ELSE GOTO :EOF

to:

IF NOT !Hex_Code!.==. (ECHO ^<td align=center^>^<p^>!Hex_Code: =!^</p^>^</td^>>>%htm%) ELSE GOTO :EOF

(that's colon/Space/Equal sign)
:dubbio:

WHY the heck do you want to use an HTML table to use in Excel it remains a mistery to me.
Excel by default attempts to parse whatever you give it as if they were NUMBERS.
00 is not a number (as well as 01 up to 09, they will be "automagically translated to 0,1 ....9).
Use a .csv format to export data that is to be imprted/parsed by Excel AND put double quotes around values so that they will be treated as TEXT.

Example:
Your SPACE delimited TEXT format:

00 01 02 AA

The stoopid HTML table snippet::

<td align=center><p>00</p></td>
<td align=center><p>01</p></td>
<td align=center><p>02</p></td>
<td align=center><p>AA</p></td>

What excel will get:

0 1 2 AA


A .csv snippet:

"00","01","02","AA"



What excel will get:

00 01 02 AA

Mind you that the actual separator is the one used in International Control Panel, default in English it is Comma "," in Italian it is semi colon ";" canppt say in French.

BTW you can import allright your .txt file in Excel, simply use the wizard (that should open automatically when you try to open a .txt file, choose "Delimited" and SPACE as delimiter, select all columns and set them as "text".

:cheers:
Wonko

#6 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 15 September 2011 - 12:21 PM

Ga! sorry I did so many edits to my post (wasn't aware it was being read at that point)
:(
Generally the gist is there though.



The given code was an example

Ok that clears things.


Excel by default attempts to parse whatever you give it as if they were NUMBERS.
00 is not a number (as well as 01 up to 09, they will be "automagically translated to 0,1 ....9).
Use a .csv format to export data that is to be imprted/parsed by Excel AND put double quotes around values so that they will be treated as TEXT

.
I want it to be numbers. Except for the A to F. Actually a "1" formated as text still does add up as number.




A .csv snippet:

Quote
"00","01","02","AA"

But I would prefer "0","0","0","1","0","2", etc.


BTW you can import allright your .txt file in Excel, simply use the wizard (that should open automatically when you try to open a .txt file, choose "Delimited" and SPACE as delimiter, select all columns and set them as "text"


Yeah. Just wanted to avoid that. Even wanted to avoid .csvs, though I didn't know they existed. (well, yes, but not why they existed).

WHY the heck do you want to use an HTML table to use in Excel it remains a mistery to me.

-maybe easier to read? (in browser?)(to test).
Also, more common than .csv. (more understandable ? for me ;))

Actually it is not that important. Either the formatting, or the whole thing. Just makes Hex editing quicker. Maybe. Maybe not. :)
:cheers:

:)
Blop
Edit,

All wonko's quotes on .csvs

and

-maybe easier to read? (in browser?)(to test).
Also, more common than .csv. (more understandable ? for me ;))


Maybe yep, (instead of)

WHY the heck do you want to use an HTML table to use in Excel


(We should)
make the batch export to .csv.
(echoing ,s commas is prbbly easier than anything. and .. for next line. :)

#7 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 15 September 2011 - 02:10 PM

Help! :)


@ECHO OFF

SETLOCAL

ECHO batch to bring hex to a .csv table.

ECHO.

rem SET /p hex=where did you store your hex?

SET /p csv=what name for your table?(no ext)

 

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

FOR /F "tokens=* delims=" %%? IN (%hex%) DO SET hex_contents=%%?

CALL :parse_line

ECHO.

ECHO File %csv%.csv written...

GOTO :EOF

 

:parse_line

SET /A Counter=0

FOR /L %%A IN (0,3,1200) DO (

SET Hex_Code=!hex_contents%:~%%A,2!

SET /A Counter+=1

IF NOT !Hex_Code!.==. (ECHO !Hex_Code!,>>%csv%.csv) ELSE GOTO :EOF

IF !Counter!==16 SET /A Counter=0 &ECHO !Hex_Code!..>>%csv%.csv

)

GOTO :EOF


Why is the

IF !Counter!==16 SET /A Counter=0 &ECHO !Hex_Code!..>>%csv%.csv

not going to 16 to echo the ".."?

B7,..F5,..00,etc


:cheers:
R

+ its doing "~". Arh! This is tedious!

Edit
Editing/ adding, rather than new post, when not groundbreaking.

Obviously [quotename='Wonko the Sane' timestamp='1316086445' post='137843']IF NOT !Hex_Code!.==. (ECHO ^<td align=center^>^<p^>!Hex_Code: =!^</p^>^</td^>>>%htm%) ELSE GOTO :EOF

(that's colon/Space/Equal sign)[/quote] for the colon, Maybe, not. :P

but for the rest..
[quote]This is tedious![/quote].

Edit Haha :)

Been looking at all that too long.

[quote name='Wonko the Sane' timestamp='1316086445' post='137843']


Quite obviously the Counter is to give a SIZE (width) to the table, in the posted example it is 16, as in:
... [/quote]
Huhu!

#8 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 15071 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 15 September 2011 - 03:35 PM

Now what could be the equivalent of the html table code:

</tr><tr>

used in the posted batch?

IF !Counter!==16 SET /A Counter=0 &ECHO ^</tr^>^<tr^>>>%htm%

Could it be a LINE FEED? (actually CR+LF) :dubbio:

If yes, this may work:

IF !Counter!==16 SET /A Counter=0 &ECHO.>>%csv%.csv



Obviously you cannot change the way Excel works.
If you had spent 2 (two) minutes checking the spreadsheets I posted on the now closed thread you could have found a simple way to convert the hex values (which are TEXT) into decimal numbers WITHOUT using any specific Excel function (which at least on old versions of Excel had to be installed additionally and that most people won't have on their machine, using a simple Vlookup that is compatible with other spreadsheet programs like OpenOffice, or Spread32)
Use *any* editor capable of parsing .csv, example:
http://www.ultraedit.../CSV_files.html

or a more dedicated tool, like:
http://csved.sjfrancke.nl/
;)

:cheers:
Wonko

#9 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 15 September 2011 - 03:49 PM

Yeah. This is getting.. Uur! for want of a better word.


Yeah, in last post/edit, I realized,

Quite obviously the Counter is to give a SIZE (width) to the table, in the posted example it is 16, as in:...


Thanks for the links. and the info in last post :thumbsup: .



Been looking at all that too long.


Huhu!



#10 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 15 September 2011 - 04:03 PM

BTW I saved a .csv in Excel, ('ll try from Spread32 too). The format is.

1,2..3,..


, being the cell
.. being the row
is it what you meant?

Now what could be the equivalent of the html table code:
Quote



</tr><tr>



used in the posted batch?



batch:

IF !Counter!==16 SET /A Counter=0 &ECHO !Hex_Code!..>>%csv%.csv


:cheers:
R.

#11 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 15 September 2011 - 04:15 PM


Obviously you cannot change the way Excel works.
If you had spent 2 (two) minutes checking the spreadsheets I posted on the now closed thread you could have found a simple way to convert the hex values (which are TEXT) into decimal numbers WITHOUT using any specific Excel function (which at least on old versions of Excel had to be installed additionally and that most people won't have on their machine, using a simple Vlookup that is compatible with other spreadsheet programs like OpenOffice, or Spread32)



Do you mean this:

To import bootsector data:
1) Open a bootsector or floppy or volume image with Tiny Hexer
2) Select the whole sector
3) Run Tools->Scripts->Copy selection to clipboard as formatted hex data
4) Run Edit_>Paste to new
5) Paste "Text"
6) Edit->Find/Replace
7) Enter a single [SPACE] in the "Find" box and tick the "Find text" box
8) Click on Find and in the Replace box type 09
9) Click on Replace all
10) You will have 511 replacements done.
11) Select All
12) Copy
13) Select cell A1 in the Excel sheet
14) Paste special ->Text

If yes, that's the reason I posted in that topic to begin with.
(as posted in the 'intro' of this one.

If not, fair enough.

Been on this too long anyway.

Thanks for the info.

:cheers:

R.

#12 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 15071 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 15 September 2011 - 07:02 PM

Try this batch:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET Stupid_space_separated=00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13


SET Separator=,
SET TxtQual='
SET Csv=mynice.csv
CD.>%csv%
SET /A Counter=0
CALL :parse_line
IF NOT "%Smart_csv%".=="". ECHO %Smart_csv:~1,100%>>%csv%

GOTO :EOF

:parse_line
FOR /L %%A IN (0,3,1200) DO (
SET Hex_Code=!Stupid_space_separated%:~%%A,2!
SET /A Counter+=1
IF NOT !Hex_Code!.==. (SET Smart_csv=!Smart_csv!%Separator%%TxtQual%!Hex_Code!%TxtQual%) ELSE GOTO :EOF
IF !Counter!==16 SET /A Counter=0 &ECHO !Smart_csv:~1,100!>>%csv% & SET Smart_csv=
)
GOTO :EOF


BUT the problem with Excel importing the 00 ÷09 as numbers remains :w00t:

It is seemingly a "feature" of Excel, so you need anyway to go through the "import wizard" and telling it that the cells are TEXT.

Not even the DIF .dif format seemingly holds this info, so the only way is the SYLK .slk format :ph34r:


Spread32 obviously opens the file as it should be :thumbup:
http://www.byedesign.co.uk/

:cheers:
Wonko

#13 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 15 September 2011 - 07:16 PM

WoooohOOOO!

I think I'm there!! :2nd:


(btw I sort you were getting at me, you cant change the way Excel works!!





I,d totally forgotten a step I used before, was actually the cleaning the table thing posted in the past.

Echo, why I'd avoided echo in the first place, echo does the CR!!!!!!!!!!!!!!!!


So even in the "thank you Very much Wonko" much simpler batch, cleaning the table was still needed.


Posted code,

ECHO OFF

SETLOCAL

ECHO batch to bring hex to a .csv table.

ECHO.

rem SET /p hex=where did you store your hex?

SET /p csv=what name for your table?(no ext)

 

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

FOR /F "tokens=* delims=" %%? IN (%hex%) DO SET hex_contents=%%?

CALL :parse_line

ECHO.

ECHO File %csv%.csv written...

GOTO :EOF

 

:parse_line

SET /A Counter=0

FOR /L %%A IN (0,3,1200) DO (

SET Hex_Code=!hex_contents%:~%%A,2!

SET /A Counter+=1

IF NOT !Hex_Code!.==. (ECHO !Hex_Code!,>>%csv%.csv) ELSE GOTO :EOF

IF !Counter!==16 SET /A Counter=0 &ECHO !Hex_Code!..>>%csv%.csv

)

GOTO :EOF


(repost) is ok except for read on. I'd tried in Excel, the non cleaned ver. So it was just doing the cells in a column, as the .csv code for go new column IS CR!!!!!

Even looking at the csv in the hexeditor, it looked as though the coding of the batch had NOT done the 16 jump, as there were .. everywhere, actually as if it Had done the 16jump, but EVERYWHERE!! Appearing like The whole code was wrong. It wasn't. This is still not the final ver probably, as some stuff I wanna see about. ( 1number per cell).

cleanup
For the cleanup too work,


IF NOT !Hex_Code!.==. (ECHO !Hex_Code!,>>%csv%.csv) ELSE GOTO :EOF

IF !Counter!==16 SET /A Counter=0 &ECHO !Hex_Code!..>>%csv%.csv


must be

IF NOT !Hex_Code!.==. (ECHO !Hex_Code!,>>%csv%.csv) ELSE GOTO :EOF

IF !Counter!==16 SET /A Counter=0 &ECHO !Hex_Code!cr>>%csv%.csv

Then open the .csv in the hexeditor, find/replace (in hex) 0d0a with nothing. and then 6372 with 0d0a. :)
(Will put that at the end of the batch)
(or, automate).

Code is:

ECHO OFF

SETLOCAL

ECHO batch to bring hex to a .csv table.

ECHO.

SET /p hex=where did you store your hex?

SET /p csv=what name for your table?(no ext)

 

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

FOR /F "tokens=* delims=" %%? IN (%hex%) DO SET hex_contents=%%?

CALL :parse_line

ECHO.

ECHO File %csv%.csv written...

GOTO :EOF

 

:parse_line

SET /A Counter=0

FOR /L %%A IN (0,3,1200) DO (

SET Hex_Code=!hex_contents%:~%%A,2!

SET /A Counter+=1

IF NOT !Hex_Code!.==. (ECHO !Hex_Code!,>>%csv%.csv) ELSE GOTO :EOF

IF !Counter!==16 SET /A Counter=0 &ECHO !Hex_Code!cr>>%csv%.csv

)

GOTO :EOF

Edit, had forgotten to delete a rem. sorry.
Edit. This sucks. It gives 1, 2, 3, 0, numbers.
:cheers:

#14 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 15 September 2011 - 07:17 PM

Cross posting :)
:cheers:

#15 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 15 September 2011 - 07:58 PM

What's the SET TxtQual=' for?
I like the way you echoed everything in a line. :clap:
smart! ;)

ACtually I don't care about the TxtQual=' as I want each unit (bit/byte, I don't know) to be F,1,F,2 etc.


Great. Good work. What did we spend all the hours for? :dubbio:

:loleverybody:

R.


edit.
Just to correct myself on previous post, there is also a 20 (hex) to replace with nothing. along with

Then open the .csv in the hexeditor, find/replace (in hex) 0d0a with nothing. and then 6372 with 0d0a.


edit again, blame tiredness. :( .just when

SET Hex_Code=!hex_contents%:~%%A,2!

is A,3 (space).



@ECHO OFF

SETLOCAL

ECHO batch to bring hex to a .csv table.

ECHO.

SET /p hex=where did you store your hex?

SET /p csv=what name for your table?(no ext)

 

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

FOR /F "tokens=* delims=" %%? IN (%hex%) DO SET hex_contents=%%?

CALL :parse_line

ECHO.

ECHO File %csv%.csv written...

GOTO :EOF

 

:parse_line

SET /A Counter=0

FOR /L %%A IN (0,3,1200) DO (

SET Hex_Code=!hex_contents%:~%%A,3!

SET /A Counter+=1

IF not !Hex_Code!.==. (ECHO !Hex_Code!,>>%csv%.csv) ELSE GOTO :EOF

IF  !Counter!==16 SET /A Counter=0 &ECHO !Hex_Code!cr>>%csv%.csv

)

pause

GOTO :EOF


and for a longer file change the
FOR /L %%A IN (0,3,1200) DO (
to bigger multiples (of 3) at 1200.

#16 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 27 September 2011 - 02:26 PM

Hello, hello.

The space separated issue can be diminished. I explain; when you open a file in an hex editor, the bytes are (visually) seperated. Copying it to a text file will be a string of numbers separated by spaces, hex 20.
In Hxd hex editor, I have mainly used, you can play with View/Byte group size. Usually set at one, put it on 16 and the string (copied to txt) will be 16 bytes space 16bytes format. (instead of byte space byte space).
(then you delete, manually, the space. Yes, manually, not great, but much less to do than when byte space byte space)

I still have to look at other hex editors, or get info about some, to see if you can have a non spaced display.

I thought I had something with xvi32-hex-editor as the space is not as obvious. But no.

I was also wondering if command line like dsfi would copy the string 'normally', though selecting a start and stop offset not possible with Dsfi. (question)
one could have an already selected snippet of code (still in gui hex-editor) and then tell dsfi to echo the binary to a txt file or whatever. not tested.

r.

#17 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 15071 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 27 September 2011 - 02:47 PM

I was also wondering if command line like dsfi would copy the string 'normally', though selecting a start and stop offset not possible with Dsfi. (question)
one could have an already selected snippet of code (still in gui hex-editor) and then tell dsfi to echo the binary to a txt file or whatever. not tested.

You seem to confuse the actual hex code with it's textual representation in numbers. :dubbio:

:cheers:
Wonko

#18 betrand

betrand

    Frequent Member

  • Advanced user
  • 467 posts
  •  
    France

Posted 27 September 2011 - 03:51 PM

Yeah, I know what you mean.
Dsfi wouldn't change anything for the goal. I.e. the display.

I'm looking at hex2bin again. maybe maybe not, thinking out loud.
edit actually maybe bin2hex would do a non spaced.
:)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users