PDA

View Full Version : As many languages as we can


GnuVince
05-24-2002, 05:18 PM
Let's write a VERY basic hexadecimal dumper in as many languages as we can. I,ve got here a O'Caml version:





let _ =

try

let myfile = open_in Sys.argv.(1) in

try

while true do

Printf.printf "%02x%02x " (Char.code (input_char myfile))(Char.code

(input_char myfile));

done

with End_of_file -> ()

with _ ->

Printf.printf "Usage: %s <file>\n" Sys.argv.(0)





13 lines. I'm going to write one in Ruby now. Those who have interest in everything that's useless, get in your editors and take 3 minutes to code that thing :)

Sample output:

656c 2074 205f 0a3d 2020 7420 7972 200a 2020 2020
6c20 7465 6d20 6679 6c69 2065 203d 706f 6e65 695f
206e 7953 2e73 7261 7667 282e 2931 6920 0a6e 2020
2020 2020 7274 0a79 2020 2020 2020 2020 7720 6968
656c 7420 7572 2065 6f64 200a 2020 2020 2020 2020
2020 5020 6972 746e 2e66 7270 6e69 6674 2220 3025
7832 3025 7832 2220 2820 6843 7261 632e 646f 2065
6928 706e 7475 635f 6168 2072 796d 6966 656c 2929
4328 6168 2e72 6f63 6564 200a 2020 2020 2020 2020
2020 2820 6e69 7570 5f74 6863 7261 6d20 6679 6c69
2965 3b29 200a 2020 2020 2020 2020 6f64 656e 200a
2020 2020 7720 7469 2068 6e45 5f64 666f 665f 6c69
2065 3e2d 2820 0a29 2020 7720 7469 2068 205f 3e2d
200a 2020 2020 5020 6972 746e 2e66 7270 6e69 6674
2220 7355 6761 3a65

inkedmn
05-24-2002, 09:06 PM
here's python :)


#!/usr/bin/env python

def hexify(argument):
list = []
file = open(argument)
chars = file.read()
for item in chars:
num = ord(item) # get ascii value for character
hex_data = hex(num) # pass ascii value to hex function
list.append(hex_data) # add result to list
x = 0
for item in list:
if x < 100: # just going to print a few
print item,
x += 1
hexify('gpl.txt')


and here's the output:


C:\python>python hex.py
0x9 0x9 0x20 0x20 0x20 0x20 0x47 0x4e 0x55 0x20 0x47 0x45 0x4e 0x45 0x52 0x41 0x
4c 0x20 0x50 0x55 0x42 0x4c 0x49 0x43 0x20 0x4c 0x49 0x43 0x45 0x4e 0x53 0x45 0x
a 0x9 0x9 0x20 0x20 0x20 0x20 0x20 0x20 0x20 0x56 0x65 0x72 0x73 0x69 0x6f 0x6e
0x20 0x32 0x2c 0x20 0x4a 0x75 0x6e 0x65 0x20 0x31 0x39 0x39 0x31 0xa 0xa 0x20 0x
43 0x6f 0x70 0x79 0x72 0x69 0x67 0x68 0x74 0x20 0x28 0x43 0x29 0x20 0x31 0x39 0x
38 0x39 0x2c 0x20 0x31 0x39 0x39 0x31 0x20 0x46 0x72 0x65 0x65 0x20 0x53 0x6f 0x
66 0x74 0x77

iDxMan
05-25-2002, 01:02 AM
#!/usr/bin/perl

die "$0 <file> \n" if (!@ARGV);

while(<>) {
chomp;
foreach ( split(//) ) {
printf("%X ", ord());
}
print "\n";
}


Did I really just waste 3 minutes on that? :D

oh yeah. the output of it parsing itself :


23 21 2F 75 73 72 2F 62 69 6E 2F 70 65 72 6C

64 69 65 20 22 24 30 20 3C 66 69 6C 65 3E 20 5C 6E 22 20 69 66 20 28 21 40 41 52 47 56 29
3B

77 68 69 6C 65 28 3C 3E 29 20 7B
20 20 20 63 68 6F 6D 70 3B
20 20 20 66 6F 72 65 61 63 68 20 28 20 73 70 6C 69 74 28 2F 2F 29 20 29 20 7B
20 20 20 20 20 20 70 72 69 6E 74 66 28 22 25 58 20 22 2C 20 6F 72 64 28 29 29 3B
20 20 20 7D
20 20 20 70 72 69 6E 74 20 22 5C 6E 22 3B
7D



-r

phubuh
05-25-2002, 10:24 AM
import java.io.*;
import java.util.Vector;

public class HexDump {
private File inputFile;
private FileInputStream inputStream;

private Vector data;

HexDump(String filename) throws Exception {
inputFile = new File(filename);
inputStream = new FileInputStream(inputFile);
data = new Vector();
}

private void read() throws Exception {
byte b;

while ((b = (byte)inputStream.read()) != -1)
data.add(new Integer(b));
}

public String toHexString() {
String hex = new String();

for(int i = 0; i < data.size(); i++) {
hex = hex.concat(
Integer.toString(((Integer) data.elementAt(i)).intValue(),16));
hex = hex.concat(" ");
}

return hex;
}

public static void main(String[] args) {
HexDump hexer = null;

try {
hexer = new HexDump(args[0]);
hexer.read();
} catch (Exception e) {
System.err.println(e);
}

System.out.println(hexer.toHexString());
}
}

Okay, we got Java done.

phubuh
05-25-2002, 10:41 AM
<html>
<head>
<title>hexdump</title>
<script language="JScript">
function convert(input, outputTextArea) {
var output = "";
var length = input.length;
for(var i = 0; i < length; i++) {
output += input.charCodeAt(i).toString(16);
if(i % 2 != 0) {
output += " ";
}
}
outputTextArea.value = output;
}
</script>
</head>

<body>
<textarea id="input" rows="8"></textarea>


<textarea id="output" rows="8"></textarea>


<button onClick="convert(document.getElementById('input').value, document.getElementById('output'))">Convert</button>
</body>
</html>

iDxMan
05-25-2002, 02:33 PM
Quick php hack:


<html>
<head>
<title>Hex Dump</title>
<body>
<form action="<?=$PHP_SELF?>" method="GET">
Input File on server :
<input type="text" name="file" value="<?=$file?>" size=40 maxsize=60>


<input type="submit" value="Submit">
</form>
<hr>
<pre>
<?php
if ($file) {
if (file_exists($file)) {
$buf = file ($file, 1);
while (list(, $line) = each($buf)) {
foreach (preg_split('//', $line, -1, PREG_SPLIT_NO_EMPTY) as $c) {
echo dechex(ord($c)), " ";
}
echo "\n";
}
}
else {
echo "File ($file) does not exist";
}
}
?>
</pre>
</body>
</html>

phubuh
05-26-2002, 08:23 AM
Here's an ... uhm... compact version of iDxMan's code:

perl -nle 'chomp;printf"%X ",ord()foreach split//'


Sample usage:

> echo hello mister > text.txt
> perl -nle 'chomp;printf"%X ",ord()foreach split//' < text.txt
68 65 6C 6C 6F 20 6D 69 73 74 65 72

Or just...

> echo hello mister | perl -nle 'chomp;print68 65 6C 6C 6F 20 6D 69 73 74 65 72

Ooor...

perl -nle 'chomp;printf"%X ",ord()foreach split//'
hello mister!
68 65 6C 6C 6F 20 6D 69 73 74 65 72 21

iDxMan
05-26-2002, 01:42 PM
Here's an ... uhm... compact version of iDxMan's code


ah, who wants compact oneliners? Its about beauty. :D


-r

Strike
05-26-2002, 02:19 PM
Originally posted by iDxMan


ah, who wants compact oneliners? Its about beauty. :D


-r Perl is?
/me laughs

iDxMan
05-26-2002, 02:48 PM
Originally posted by Strike
Perl is?
/me laughs

> eye of the beholder.

Yes, it can easily (and does far too many times) turn into obfuscated line noise. That doesn't mean it can't be beautiful when written properly.


-r

file13
05-26-2002, 06:30 PM
here's Ada 95:


----------------------------------------------------------------------------
-- hexer.adb
--
-- file13@dallaslamers.org
-- http://www.qlippoth.com/
--
-- This is Ada 95. Please see http://www.gnat.com
-- for the GNU Ada compiler.
--
-- To compile: gnatmake -O2 -gnatp hexer
----------------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Command_Line; use Ada.Command_Line;

procedure Hexer is

Char : Character;
File : File_Type;
Quit : exception;

procedure Print_Usage is
begin
Put_Line (Current_Error, "Usage: hexer [file]");
New_Line;
Set_Exit_Status (Failure);
raise Quit;
end Print_Usage;

begin

if Argument_Count = 0 or Argument_Count > 2 then
Print_Usage;
end if;

Open (File, In_File, Argument (1));
loop
exit when End_Of_File (File);
Get (File, Char);
Put (Character'Pos (Char), 0, 16);
Put (" ");
end loop;
Close (File);

exception
when Quit =>
null;

end Hexer;


lemmie spit out a Lisp one and MAYBE Fortran 90.... ;)

file13
05-26-2002, 08:05 PM
here's Common Lisp:


;;;; hexer.lsp
;;;;
;;;; file13@dallaslamers.org
;;;; http://www.qlippoth.com/
;;;;
;;;; This is generic Lisp. Since commandline processing is
;;;; implementation dependent we simply ask for the filename.
;;;;
;;;; Depending on your Lisp, you may need to change the (quit)
;;;; command. (quit) works with Cmucl, Clisp, Ecls....

(defun print-hex-dump (filename)
(with-open-file (file filename :direction :input)
(do ((i (read-char file nil file)
(read-char file nil file)))
((eq i file))
(format t "~x " (char-code i)))))

(defun main ()
(format t "Enter file to open: ")
(print-hex-dump (read-line))
(terpri)
(quit);or your Lisp's quit command. Ex: Allegro = (exit)
)

(main)


:)

file13
05-26-2002, 08:46 PM
BTW: here's a Clisp command line version


(defun print-hex-dump (filename)
(with-open-file (file filename :direction :input)
(do ((i (read-char file nil file)
(read-char file nil file)))
((eq i file))
(format t "~x " (char-code i)))))

(defun clisp-main ()
(when (null *args*)
(format t "Usage: hexer [file]~%")
(quit))
(print-hex-dump (car *args*)))

(clisp-main)


http://clisp.cons.org

file13
05-27-2002, 11:49 AM
here's Fotran 90:


! hexer.f90
!
! file13@dallaslamers.org
! http://www.qlippoth.com/
!
! Like Lisp, Fortran 90 has no standard way to get
! commandline. So we simply ask for the filename.
!
! Tested on Lahey Fortran: http://www.lahey.com/
! To compile: lf95 hexer.f90

program hexer

implicit none

character(len=256) :: filename
integer :: ios, file13 = 13 ! ;)
character :: char

print *, "Enter filename to dump hex: "
read *, filename
open (unit=file13, file=filename, status="old", iostat=ios)

do
read (unit=file13, fmt='(a1)', iostat=ios, advance="no") char
if (ios == -1) exit ! -1 is end of file
write (unit=*, fmt='(z3)', advance="no") ichar (char)
end do

close (unit=file13)
print *, ""

end program hexer


off to the pool! :D

xilica
05-27-2002, 11:51 AM
nice, jeeze you do know your stuff.

phubuh
05-28-2002, 07:03 AM
I'm thinking of doing it in BeFunge-98, but I'm not sure if it's worth the effort. :geek:

Strike
05-28-2002, 12:16 PM
C code:

#include <stdio.h>

int main(int argc, char **argv)
{
FILE* f;
char* buf;

if (argc != 2) {
printf("Usage: hexdump <filename>\n");
exit(1);
}

f = fopen(argv[1], "r");
if (f == NULL) {
printf("Could not open file %s", argv[1]);
}

buf = (char *)calloc(1, sizeof(char));

while (fread(buf, 1, 1, f)) {
printf("%X ", buf[0]);
}
return 0;
}


Output (on itself):

[ddipaolo@quinn ..ums.net/hexdump]% ./hexdump hexdump.c
23 69 6E 63 6C 75 64 65 20 3C 73 74 64 69 6F 2E 68 3E A A 69 6E 74 20 6D 61 69 6E 28 69 6E
74 20 61 72 67 63 2C 20 63 68 61 72 20 2A 2A 61 72 67 76 29 A 7B A 9 46 49 4C 45 2A 9 66 3B
A 9 63 68 61 72 2A 9 62 75 66 3B A 9 63 68 61 72 9 68 65 78 5B 32 5D 3B A 9 A 9 69 66 20 28
61 72 67 63 20 21 3D 20 32 29 20 7B A 9 9 70 72 69 6E 74 66 28 22 55 73 61 67 65 3A 20 68 65 78
64 75 6D 70 20 3C 66 69 6C 65 6E 61 6D 65 3E 5C 6E 22 29 3B A 9 9 65 78 69 74 28 31 29 3B A
9 7D A A 9 66 20 3D 20 66 6F 70 65 6E 28 61 72 67 76 5B 31 5D 2C 20 22 72 22 29 3B 9 A 9 69
66 20 28 66 20 3D 3D 20 4E 55 4C 4C 29 20 7B A 9 9 70 72 69 6E 74 66 28 22 43 6F 75 6C 64 20
6E 6F 74 20 6F 70 65 6E 20 66 69 6C 65 20 25 73 22 2C 20 61 72 67 76 5B 31 5D 29 3B A 9 7D
A A 9 62 75 66 20 3D 20 28 63 68 61 72 20 2A 29 63 61 6C 6C 6F 63 28 31 2C 20 73 69 7A 65
6F 66 28 63 68 61 72 29 29 3B A A 9 77 68 69 6C 65 20 28 66 72 65 61 64 28 62 75 66 2C 20 31
2C 20 31 2C 20 66 29 29 20 7B A 9 9 70 72 69 6E 74 66 28 22 25 58 20 22 2C 20 62 75 66 5B 30
5D 29 3B A 9 7D A 9 72 65


----edit----
linebreaks, wheee!

Strike
05-28-2002, 12:25 PM
#include <fstream>

using namespace std;

int main(int argc, char **argv)
{
if (argc != 2) {
cerr << "Usage: hexdump <filename>" << endl;
exit(1);
}

ifstream infile;
char buf;

infile.open(argv[1], ios::in);

while (infile) {
infile.get(buf);
cout << hex << (int)buf << " ";
}

return 0;
}


Output (on itself):

[ddipaolo@quinn ..ums.net/hexdump]% ./hexdump hexdump.cpp
23 69 6e 63 6c 75 64 65 20 3c 66 73 74 72 65 61 6d 3e a a 75 73 69 6e 67 20 6e 61 6d 65 73 70 61
63 65 20 73 74 64 3b a a 69 6e 74 20 6d 61 69 6e 28 69 6e 74 20 61 72 67 63 2c 20 63 68 61 72
20 2a 2a 61 72 67 76 29 a 7b a 9 69 66 20 28 61 72 67 63 20 21 3d 20 32 29 20 7b a 9 9 63 65 72
72 20 3c 3c 20 22 55 73 61 67 65 3a 20 68 65 78 64 75 6d 70 20 3c 66 69 6c 65 6e 61 6d 65 3e 22
20 3c 3c 20 65 6e 64 6c 3b a 9 9 65 78 69 74 28 31 29 3b a 9 7d a a 9 69 66 73 74 72 65 61 6d 9
69 6e 66 69 6c 65 3b 9 a 9 63 68 61 72 9 9 62 75 66 3b a a 9 69 6e 66 69 6c 65 2e 6f 70 65 6e 28
61 72 67 76 5b 31 5d 2c 20 69 6f 73 3a 3a 69 6e 29 3b a a 9 77 68 69 6c 65 20 28 69 6e 66 69 6c
65 29 20 7b a 9 9 69 6e 66 69 6c 65 2e 67 65 74 28 62 75 66 29 3b a 9 9 63 6f 75 74 20 3c 3c 20
68 65 78 20 3c 3c 20 28 69 6e 74 29 62 75 66 20 3c 3c 20 22 20 22 3b a 9 7d a a 9 72 65 74 75 72
6e 20 30 3b a 7

Strike
05-28-2002, 12:42 PM
Cheap shell script hack (using xxd):

#!/bin/bash

for file in $*; do
xxd $file;
done

But it can do multiple files!

[ddipaolo@quinn ..ums.net/hexdump]% bash hexdump.sh hexdump.sh hexdump.sh hexdump.sh
0000000: 2321 2f62 696e 2f62 6173 680a 0a66 6f72 #!/bin/bash..for
0000010: 2066 696c 6520 696e 2024 2a3b 2064 6f0a file in $*; do.
0000020: 0978 7864 2024 6669 6c65 3b0a 646f 6e65 .xxd $file;.done
0000030: 0a .
0000000: 2321 2f62 696e 2f62 6173 680a 0a66 6f72 #!/bin/bash..for
0000010: 2066 696c 6520 696e 2024 2a3b 2064 6f0a file in $*; do.
0000020: 0978 7864 2024 6669 6c65 3b0a 646f 6e65 .xxd $file;.done
0000030: 0a .
0000000: 2321 2f62 696e 2f62 6173 680a 0a66 6f72 #!/bin/bash..for
0000010: 2066 696c 6520 696e 2024 2a3b 2064 6f0a file in $*; do.
0000020: 0978 7864 2024 6669 6c65 3b0a 646f 6e65 .xxd $file;.done
0000030: 0a .

GnuVince
05-28-2002, 01:06 PM
Oh, Strike wants multiple files? I can give you multiples files ;)





try

for i = 1 to ((Array.length Sys.argv) - 1) do

let myfile = open_in Sys.argv.(i) in

try

Printf.printf "%s:\n\n" Sys.argv.(i);

while true do

Printf.printf "%02x" (Char.code (input_char myfile));
Printf.printf "%02x " (Char.code (input_char myfile));
done

with End_of_file -> print_string "\n\n------------------------\n\n"

done

with _ ->

Printf.printf "Usage: %s <file>\n" Sys.argv.(0)





ph34r Jar Jar Binks!

Edit: There was a problem with my code. It's now fixed.

inkedmn
05-28-2002, 01:10 PM
oh, as can i...

Strike
05-28-2002, 01:29 PM
I'm re-learning the assembly stuff using The Art of Assembly (http://webster.cs.ucr.edu/) (excellent, VERY LONG, book for anyone interested in reading on x86 assembler)

file13
05-28-2002, 05:30 PM
hey Vince, since you started this, how many languages we got so far?

(me too lazy...) ;)

xilica
05-28-2002, 05:40 PM
visual basic
python
c
o'caml
xxd
fortran 90
lisp
ada 95
php
perl
java


11.......... so far

Strike
05-28-2002, 05:40 PM
um, xilica, I don't even know VB and I can tell that it doesn't do the right thing

first of all, you aren't outputting hex - "180" is not a hex representation of a byte
secondly, you are just outputting 200 random numbers!

another thing - why do you declare myTab but never use it?

Strike
05-28-2002, 05:41 PM
also, xxd isn't a language, that one was in bash
and you forgot to list C++

xilica
05-28-2002, 05:46 PM
Strike: dude im sorry i never heard of some of them. also some guy said C... did anyone do this in c++?

also, to my code...since VB does have a GUI (i love that sound...gooey....lol) there are names for different objects.

also, strike why do you doubt me?

unruly
05-28-2002, 05:48 PM
Originally posted by xilica
here it is in VB:

some random bullshit that appears to be code of some sort....

ahh my text box ran out of roomm..anyway, thats it

magically delicous... now, could you make it work like it's supposed to?

xilica
05-28-2002, 05:49 PM
Originally posted by unruly


magically delicous... now, could you make it work like it's supposed to?

lol, ur even a bigger spammer then me.....first off do you even know the language of VB?

Strike
05-28-2002, 05:51 PM
xilica, I did it in C++ directly below my C version and right above my bash version.

Also, I doubt you because it doesn't take a brain to see that what you are doing doesn't do any hex conversion whatsoever. You are making random numbers (and not even converting them to hex) and then printing those out.

Tell you what - prove me wrong. Run this hexdumper on this webpage (far greater than 200 characters), and paste the output.

unruly
05-28-2002, 05:53 PM
Originally posted by xilica


lol, ur even a bigger spammer then me.....first off do you even know the language of VB?

actually, yeah, back in the day, when AOL was teh winnar's ISP, I used to do a lot of stupid stuff with VB...

I've long since forgotten that nightmare of pseudo-code and moved on.

xilica
05-28-2002, 05:53 PM
tell u what strike... i know everyone hates VB....... i wnat your opinion on the language since everyone here is "poo pooing it"

actually,
i think python, lisp, scheme, bash, etc... are OUTDATED...but you know what, who cares, these languages can actually DO SOMETHING.

GnuVince
05-28-2002, 05:59 PM
xilica: DO you understand english?

YOUR: This means that the object we're talking about belongs to xilica in this particular case
PROGRAM: the thing you coded
DOESN'T: Negation
WORK: Does what is expected

The program is supposed to take its input from a file and output everything in HEX (base 16). The fact that there are no letters in your output and than some values are larger than two digits proves us beyond a shadow of a doubt that your program is unfunctional. Stop bugging Strike, he was just telling you that your program does not even come close to the original objective.

xilica
05-28-2002, 06:03 PM
darnit,darnit,darnit,darnit,darnit,darnit,darnit,darnit,darnit


I make an ATTEMPT to try but all I do is get everyone pissed off. I didn't know we had to get the HEX from a file. Thank you GnuVince for explaining it to me. How come no one else gets berated for TRYING to contribute.

Also, I have deleted my VB code..

GnuVince
05-28-2002, 06:10 PM
Considering the first line of the whole thread was "Let's write a very basic hexadecimal dumper...", and that everyone got it, maybe I could suggest that you read the task before coding? If someone orders a hamburger and you bring a pizza, it's not ok.

xilica
05-28-2002, 06:12 PM
Originally posted by GnuVince
Considering the first line of the whole thread was "Let's write a very basic hexadecimal dumper...", and that everyone got it, maybe I could suggest that you read the task before coding? If someone orders a hamburger and you bring a pizza, it's not ok.

Oh, i did read it. Well, maybe for me but the word Hexadecimal dumper meant when you wrote a program it would just dump out random hex. But everyone except me got it, maybe I think different.

file13
05-28-2002, 06:18 PM
yes.....well so we got:

ada 95
bash
c
c++
common lisp
o'caml
fortran 90/95
java
javascript
php
perl
python

?

please make corrections. just trying to figure out what we need...boy there's lots.....good exscuse to play with more languages.... :D

so going from the com.lang.* how bout we shoot for:

assembly (x86)
basic (pick one)
cobol (i personally don't know any modern COBOL)
pascal (or delphi)
objective c
ruby (or did Vince already do it?)
scheme
smalltalk
tcl

? :)

GnuVince
05-28-2002, 06:29 PM
Ruby:



#!/usr/bin/env ruby



begin

myfile = File.open(ARGV[0]).read

myfile.each_byte { |x|

printf ("%02x ", x)

}

print "\n"

rescue

puts "Usage: $0 <file>"

end

Strike
05-28-2002, 06:47 PM
I'm this close to getting the asm version to work
I'll post about it in another forum, see if anyone can tackle it

Strike
05-29-2002, 05:43 PM
I got it! It's a whopping 80 bytes smaller than the compiled C one. I'll attach it, since it's a tad bit long. Here is some of the output:

[ddipaolo@quinn ..ums.net/hexdump]% ./hexdump
67 6C 6F 62 61 6C 20 6D 61 69 6E A 65 78 74 65 72 6E 20 70 72 69 6E 74 66 A 65 78 74 65 72 6E
20 70 75 74 63 68 61 72 A 65 78 74 65 72 6E 20 66 6F 70 65 6E A 65 78 74 65 72 6E 20 66 72 65
61 64 A A 5B 73 65 63 74 69 6F 6E 20 2E 74 65 78 74 5D A A 6D 61 69 6E 3A A 9 70 75 73 68 9
65 62 70 9 9 9 9 9 3B 20 73 65 74 20 75 70 20 73 74 61 63 6B 20 66 72 61 6D 65 20 66 6F 72 20 64
65 62 75 67 67 65 72 A 9 6D 6F 76 9 9 65 62 70 2C 20 65 73 70 A A 9 3B 20 4F 70 65 6E 20 66
69 6C 65 A 9 6D 6F 76 9 9 65 61 78 2C 20 66 69 6C 65 4D 6F 64 65 9 9 3B 20 4C 61 73 74 20 61
72

But, it's 65 lines, so I don't want to paste it all.

Oh, btw, instructions for running:

mv hexdump.txt hexdump.asm
nasm -f elf hexdump.asm
gcc -o hexdump hexdump.o
./hexdump

Strike
05-29-2002, 06:22 PM
Oh yeah, and it was bad enough just getting it to dump a fixed filename, so I'm not going to bother having it read command line args :D Feel free to fix it up to do so if you (like|dare)

file13
05-30-2002, 12:39 PM
check out:

http://www.leto.net/papers/writing-a-useful-program-with-nasm.txt

there's info on doing the command line stuff with nasm. :)

yet more stuff to do that i don't have time for...like posting here.... :D

ChefNinja
06-25-2002, 06:20 AM
Hmm, here's an attempt in VB, not sure if I got it right though.


Private Sub cmdOpen_Click()

Open txtLocation.Text For Input As #1
txtDump.Text = Input(FileLen(txtLocation.Text), #1)
Close #1

Call HexConvert


End Sub

Public Function HexConvert()

Dim StringLength As Integer
Dim MyString As String
Dim MyChar As String
Dim myHex As String
Dim ConvertedString As String


MyString = txtDump.Text
StringLength = Len(MyString)

For I = 1 To StringLength

MyChar = Mid$(MyString, I, 1)
If MyChar <> "" Then
myHex = Hex(Asc(MyChar))
ConvertedString = ConvertedString & myHex & " "

txtDump.Text = ConvertedString

End If
Next I

End Function


Meh.. Here's the output


77 68 6F 61 20 74 68 69 73 20 69 73 20 63 6F 6F 6C 20 77 68 61 74 20 61 6D 20 69 20 64 6F 69 6E 67 20 63 6F 6E 76 65 72 74 69 6E 67 20 73 68 69 74 20 69 6E 74 6F 20 68 65 78 3F


I think I did this right... not sure :o

Strike
06-25-2002, 08:52 AM
Ouch
VB make head hurt

Hey, is that where people who Type Like This With Every Word Capitalized get their style from? Because that whole thing is really really really dumb in my opinion.

gish
06-25-2002, 10:06 AM
yes it is...
:(
:sick:

jemfinch
06-26-2002, 08:38 PM
Ok, since formatting functions (like printf) are so amazingly slow, let's see these programs without using a formatting function. I think that'll be much more interesting since all formatting functions are basically the same.

Jeremy

GnuVince
06-26-2002, 11:08 PM
let _hex = "0123456789abcdef"

let hex_of_int n =
let hex_s = String.create 2 in
hex_s.[0] <- _hex.[n lsr 4];
hex_s.[1] <- _hex.[n land 15];
hex_s

let _ =
try
for i = 1 to ((Array.length Sys.argv) - 1) do
let myfile = open_in Sys.argv.(i) in
try
Printf.printf "%s:\n\n" Sys.argv.(i);
while true do
output stdout (hex_of_int (Char.code (input_char myfile))) 0 2;
output stdout (hex_of_int (Char.code (input_char myfile))) 0 2;
print_char ' ';
flush stdout;
done
with End_of_file -> print_string "\n\n------------------------\n\n"
done
with _ ->
Printf.printf "Usage: %s <file>\n" Sys.argv.(0)


hex_of_int is based loosely on your string_to_hexadecimal jeremy in case you wondered.

jemfinch
06-27-2002, 07:34 PM
I'm curious, if you'd like to do some testing.

First, get a big file. Several megabytes, hopefully.

Time your program on that file a few times, picking the smallest time.

Then modify your program slightly so hex_of_int takes as an argument the string it returns. Then keep just one two character string sitting around and continually pass it to hex_of_int, printing it each time.

I'm curious how much faster the version that doesn't allocate a new two charater string on each call is.

Jeremy

Dru Lee Parsec
06-27-2002, 08:20 PM
Phubuh:

Change your method name public String toHexString() to
public String toString() and then you can just print it with

System.out.println(hexer);

Just let the object display itself.

GnuVince
06-27-2002, 08:22 PM
OK, I tested my first version on a video file:

[vince@vincent: ~/prog/ocaml/hex]% du supertimor.mpg
2.0M supertimor.mpg
[vince@vincent: ~/prog/ocaml/hex]% time ./hex2 supertimor.mpg
[hex dump]
./hex2 supertimor.mpg 1.24s user 1.20s system 32% cpu 7.409 total


Modified version coming soon (hopefully)

GnuVince
06-27-2002, 08:31 PM
Code for the new version (I hope it's what you had in mind):


let _hex = "0123456789abcdef"

let hex_of_int hex_s n =
hex_s.[0] <- _hex.[n lsr 4];
hex_s.[1] <- _hex.[n land 15];
hex_s

let _ =
try
let jemfinch = String.make 2 '_' in
for i = 1 to ((Array.length Sys.argv) - 1) do
let myfile = open_in Sys.argv.(i) in
try
Printf.printf "%s:\n\n" Sys.argv.(i);
while true do
output stdout (hex_of_int jemfinch (Char.code (input_char
myfile))) 0 2;
output stdout (hex_of_int jemfinch (Char.code (input_char
myfile))) 0 2;
print_char ' ';
flush stdout;
done
with End_of_file -> print_string "\n\n------------------------\n\n"
done
with _ ->
Printf.printf "Usage: %s <file>\n" Sys.argv.(0)


And the time output

./hex3 supertimor.mpg 1.03s user 1.10s system 30% cpu 7.049 total

kmj
06-27-2002, 08:44 PM
hey, where's that code for coloring o'caml stuff? I'd like to try my hand at converting it for other languages.

GnuVince
06-27-2002, 09:04 PM
kmj: http://darkhost.mine.nu:81/shl.tgz

It was written by PrBacterio, but very quickly, and as he said "it's very hacky".

ChefNinja
06-29-2002, 09:24 AM
SWEET! :) This is one of my first C++ programs, I know there is already a C++ version of this up here, but since I am reading this book and trying to learn it, I thought I may as well give it a try :)


#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>


void main()
{

ifstream fp;
char fname[12];
char input;

cout << "Enter the name of the file you wish to open.\n";
cin >> fname;

fp.open(fname, ios::in);

while (fp.get(input))
{
cout << "0x" << hex << (int)input << " ";
}
fp.close();

return;

}


Yeah it has to ask you for the file name because I don't know how to grab the file name from the command line.. anybody who wants to help me out a little here is very welcome to :)

Danger Fan
06-29-2002, 03:16 PM
<Really bad at C> Maybe this will work?</Really bad at C>


#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>


void main(int argc, char *argv[])
{

if(argc < 2) {
printf("Usage: ./progname filename");
}

ifstream fp;

fp.open(&argv[1], ios::in);

while (fp.get(input))
{
cout << "0x" << hex << (int)input << " ";
}
fp.close();

return;

}


Don't laugh, I don't remember any C. Plus I'm too lazy to look it up :)

Strike
07-04-2002, 02:56 AM
ChefNinja:
you don't need (shouldn't put) the ".h"s on the end of the include files. The files without the .h's are the C++ wrappers for the C files of the same name.

Danger Fan:
did that compile with a C compiler? Because you use some C++ stuff in there. cout is C++, as is the ios::in stuff (heck, the :: operator doesn't even exist in C that I know of, as you hardly ever have any scope to resolve in C that isn't done by braces)

ChefNinja
07-04-2002, 03:41 AM
Strike, in VS6 the program won't compile without the .h on the end of the headers... I don't know why :\

Strike
07-04-2002, 05:35 AM
Hmm, maybe it's just a Linux-ism then, though I thought it wasn't. Of course, the C++ code I did write in Windows was REALLY crappy anyway (and not just by default when you consider the platform ;)).

Oh, and getting command line args is easy. They are stored in argv[]. argv[0] is the program name, argv[1] is the first word after that, etc.

ChefNinja
07-04-2002, 07:01 AM
Thanks :)

kmj
07-04-2002, 11:07 AM
chefninja, try <stl\header> instead of just <header>. If that doesn't work, look around through the include directories. The files should be there, but they may be in a different directory. (alternatively, you could set your project settings to look in the proper directory).

ChefNinja
07-04-2002, 02:08 PM
Err, whats wrong with using .h anyways? I would really like to hear these types of things from you guys, it helps alot. The book im reading doesn't mention alot of the things you guys are telling me about good/bad code form, thanks :) BTW, I am going to be picking up a new C++ book tommorow... any reccomendations?

stuka
07-05-2002, 11:03 AM
ChefNinja, Strike, kmj - In VS6, you CAN use the <iostream> style of C++ header, you just have to remember your using statement, or qualify the identifiers with the std namespace. Heck, kmj, I expected YOU to know that one! :P

ChefNinja - the .h is frowned upon for the C++ headers since it automatically moves all the identifiers from the std namespace into the global namespace. I've actually read articles from guys who REALLY know C++ that claimed that doing something like that was acceptable in an implementation file, but next to mortal sin in a header file...it's not that bad in your particular case, but as with any such thing, if you can learn good standard practices now, why not, eh?

Strike
07-05-2002, 12:52 PM
Stuka, yeah I forget about that because g++ allows me to get away with it (bad). :(

Dru Lee Parsec
07-06-2002, 02:52 PM
Strike, in VS6 the program won't compile without the .h on the end of the headers... I don't know why

I think that's just a default behavior of the compiler.

Also, I bet one of the C guys can tell me if this is still needed, but when I was a C++ programmer in years past we always had to wrap our header files like this

[assuming the file name was mystuff.h]

#ifndef MYSTUFF_H
#define MYSTUFF_H
// all the header code goes here
#enddef

Or something like that (It's been about 5 years since I've coded in C)

What this does is fix an issue where you have multiple .c files using the same header file. In many compilers you'll get an error because the code gets linked once, and then tries to link again when the 2nd file tries to use it. So the first time it's linked MYSTUFF_H gets defined and that keeps it from getting linked again.

Does C still require this? I remember back in Visual C++ version 1.5 and 2.0 the IDE would create code that wouldn't wrap the headers this way. That meant that the Microsoft autogenerated code wouldn't run until you modified it.

Strike
07-06-2002, 03:02 PM
Yeah, that sort of thing is still expected (afaik). And yeah, it's to fix "double inclusion", because basically "#include" temporarily copies and pastes the contents of that file into the file that is including it. If another include included that file as well, then you would have multiple declarations of the same functions/classes/whatnot.

augur
06-03-2003, 11:20 AM
Greetings,

Here's an x86 assembly version (DOS), sorry, I don't have a linux box at work:
segment .code
org 0x100

global main
main:
mov di, filename
call GetFilename

mov dx, filename
mov al, 0 ; open for reading
mov ah, 0x3D ; open file
int 0x21
jc FileOpenErr ; error opening? display msg and quit

xchg ax, bx ; save the file handle in bx, required by DOS

call GetFilesize

.readloop:
mov ah, 0x3F
mov cx, 1
mov dx, bufferin
int 0x21
jc FileReadErr
call Convert2Hex
mov dx, bufferout
call Display
dec word [filesize]
cmp word [filesize], 0
jz .done
jmp .readloop

.done:
mov ah, 0x3E ; close file
int 0x21
int 0x20

;*************************************************
FileOpenErr:
mov dx, msgFileOpenErr
call Display
int 0x20

;*************************************************
FileReadErr:
mov dx, msgFileReadErr
call Display
int 0x20

;*************************************************
Display:
mov ah, 9 ; display string '$' terminated
int 0x21
ret

;*************************************************
GetFilesize:
mov ah, 0x42 ; lseek
mov al, 02 ; from end
mov dx, 0 ; offset
int 0x21
jc FileReadErr
mov [filesize], ax ; ax = filesize

mov ah, 0x42 ; lseek
mov al, 0 ; from start
mov dx, 0 ; offset
int 0x21
ret

;*************************************************
Convert2Hex:
mov dl, byte [bufferin]
mov cx, 2
xor ax, ax
mov di, bufferout+2
.loop1:
mov al, dl
shr al, 4
mov si, .hextable
add si, ax
mov al, [si]
mov [di], al
inc di
rol dl, 4
loop .loop1
ret

.hextable db '0123456789ABCDEF'

;*************************************************
GetFilename:
push ds
push ds
pop es
mov ah, 0x62 ; get address of PSP (program segment prefix)
int 0x21

mov ds, bx ; bx = segment of PSP
mov si, 0x80
mov cl, byte [si]
cmp cl, 0
je .exit

inc si
.getlp1:
mov al, byte [si]
cmp al, 0x20 ; space
jne .getsk1
inc si
dec cl
jmp .getlp1

.getsk1:
cld
rep movsb
inc di
mov al, 0
mov byte [di], al ; null terminate

.exit:
pop ds
ret

;*************************************************
segment .data

filename resb 128
filehandle dw 0
filesize dw 0
bufferin db 0
bufferout db '0x $'
msgFileOpenErr db 'Error opening file', 0xA, 0xD, '$'
msgFileReadErr db 'Error reading file', 0xA, 0xD, '$' This is 16bit code, it may look weird to some, DOS is a 16bit OS and it uses segmented memory.

Whiteknight
01-09-2004, 07:51 PM
Originally posted by ChefNinja
Err, whats wrong with using .h anyways? I would really like to hear these types of things from you guys, it helps alot. The book im reading doesn't mention alot of the things you guys are telling me about good/bad code form, thanks :) BTW, I am going to be picking up a new C++ book tommorow... any reccomendations?

I beleive that the .h suffix was officially removed from the ANSI C++ specification sheet. If i remember correctly, the standard library was officially transcribed, and the .h suffix was deemed unnecessary because the standard includes should be built-in to the compiler.

however, i think that user-defined libraries still need the .h suffix. I cant remember the exact spec though. somebody ask ANSI.

silk.odyssey
01-20-2004, 10:50 PM
HexDumper written with Randall Hyde's High Level Assembler ( HLA ).


program HexDump;

#include ("stdlib.hhf")

static

fileHandle : dword;
buf : byte;

begin HexDump;

// Get the number of
// commandline arguments

arg.c();

if ( eax < 2 ) then

stdout.put( "dump <file> " );
exit HexDump;

endif;

// Get the name of the file to dump

arg.v( 1 );

fileio.open( eax , fileio.r );
mov( eax, fileHandle );

xor( ebx, ebx ); // ebx = 0

// Display hex digits sixteen bytes per line

while( fileio.eof( fileHandle ) != true ) do

fileio.read( fileHandle, buf, 1 );
stdout.putbSize( buf, 3, ' ' );
xor( edx, edx ); // ebx = 0

inc( ebx );

// eax = eax / ecx

mov( ebx, eax );
mov( 16, ecx );
div( ecx );

// edx = eax % ecx

if ( edx == 0 ) then

stdout.newln();

endif;

endwhile;

fileio.close( fileHandle );

end HexDump;



sample output


70 72 6F 67 72 61 6D 20 48 65 78 44 75 6D 70 3B
20 D A D A 23 69 6E 63 6C 75 64 65 20 28 22
73 74 64 6C 69 62 2E 68 68 66 22 29 20 D A D
A 73 74 61 74 69 63 20 D A D A 9 66 69 6C
65 48 61 6E 64 6C 65 20 3A 20 64 77 6F 72 64 3B
20 D A 9 62 75 66 20 3A 20 62 79 74 65 3B 20
D A D A 62 65 67 69 6E 20 48 65 78 44 75 6D
70 3B 20 D A D A 9 2F 2F 20 47 65 74 20 74

Whiteknight
02-20-2004, 10:21 AM
I wrote out the code in an HDL called VERILOG, but it doesnt have display output, except for graphs. if anybody else is interested in digital circuit modeling, I could post it, but i dont think it meets the requirements of the project.