Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

a hex dumper in C.. what do you think?

Name: Anonymous 2007-03-05 12:26 ID:k9b8HLy9

Here's the code tell me what you think

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define HEXFILE "hex.txt"
#define NONPRINTCHAR '.'
#define HEXNUM 8

int checksc(int c) {
    if(c == '\n' || c == '\r' || !isprint(c))
    return NONPRINTCHAR;
    return c;
}
int main(int argc, char **argv) {
   
    FILE *fp, *fp2;
    char buffer[HEXNUM + 2];
    int i = 0;
    int c;
    /* checking for arguments */
    if(argc == 1 || argc > 3) {
        fprintf(stderr, "%s -[option] file\n", argv[0]);
        return 1;
    }
    /* opening files and error checking */
    if(!(fp = fopen(argv[argc-1], "rb"))) {
            fprintf(stderr, "ERROR: unable to open %s for reading.\n", argv[1]);
            return 2;
    }
    if(!(fp2 = fopen(HEXFILE, "w"))) {
            fprintf(stderr, "ERROR: unable to open %s for writting.\n", HEXFILE);
            fclose(fp);
            return 3;
    }
    fprintf(fp2, "---- hex dump of %s ----\n\n", argv[argc-1]);
    memset(buffer, 0, HEXNUM + 2);
        while((c = fgetc(fp)) != EOF) {
            if(argc == 3) buffer[i] = (char)checksc(c);
            ++i;
            i%=HEXNUM;
            fprintf(fp2, "0x%x", c); // i would use %#x but it doens't work with 0.
            if(i) fputc(' ', fp2);
                else {
                    if(argc == 2) fputc('\n', fp2);
                    else if(argc == 3)
                            fprintf(fp2, "\t\t%s\n", buffer);
                    continue;
                    }
            if(c < 16) fputc(' ', fp2);
        }
    if(i) {
        buffer[i] = 0;
        fprintf(fp2, "\t\t%s\n", buffer);
    }
    fprintf(fp2, "\n\n---- EOF ----");
    fclose(fp);
    fclose(fp2);
    return 0;
}

Name: Anonymous 2007-03-05 12:50 ID:Sk6nmEpK

Just wow.

Name: Anonymous 2007-03-05 12:54 ID:Heaven

Learn how to use perror.

Name: Anonymous 2007-03-05 13:01 ID:3EhMBYtC

Not bad, though I would've singled out 0 as a special case and used %0#x for all others.

Name: Anonymous 2007-03-05 14:57 ID:k9b8HLy9

>>3
i know how to use it
>>4
yes but then it makes the formatting look very ugly.
btw that %0#x would ruin the format as well, because it would look like 00xhh
except if it was a typo and you ment to type %#x

Anyways thanks for the feedback.

ps
>>2
you liked it or what?

Name: Anonymous 2007-03-05 14:58 ID:k9b8HLy9

lol i just realized that zero is after %..
i suck cocks.

Name: Anonymous 2007-03-05 15:54 ID:Heaven

This code is absolute shit.

Name: Anonymous 2007-03-05 16:09 ID:eMubSKfr

One word. Shit. Thread over.

Name: Anonymous 2007-03-05 18:56 ID:pQt1RAzd

>>5

How the fuck would having zero as a special case make the formatting look ugly?

Name: Anonymous 2007-03-05 21:08 ID:k9b8HLy9

>>9 try it.

Name: Anonymous 2007-03-06 0:05 ID:79W2Xuf1

#include <stdio.h>

int main()
{
    char c;
    while (scanf("%c", &c) == 1)
    {
        printf("0x");
        if (c >= 0 && c < 16) putchar('0');
        printf("%hhx ", c);
    }
    putchar('\n');
    return 0;
}


Don't ask me how the fuck a char could possibly be negative. The hh flag isn't ISO C90 compliant, but it was easiest way I could find to prevent long ass strings like "0xffffff96" (yep, even with a padding of 2.) The # printf modifier only works for non-zero values, according to the man page. I could have gone with two printfs, but I feel this way's neater.

./hex < infile > outfile

Also, I/O streams are fine. Why clutter such a simple program with argument parsing?

Name: Anonymous 2007-03-06 0:15 ID:79W2Xuf1

Don't ask me how the fuck a char could possibly be negative.
Just discovered unsigned char. Put unsigned before the char, omit c >= 0 &&.

Name: Anonymous 2007-03-06 5:46 ID:QsPw0j9L

Hint 1: char is signed by default, which is a design flaw of C.
Hint 2: Stop treating characters as bytes you fucking noob! Use wchar_t whenever possible. All character sets are legacy (even ASCII - American Shit Code for Idiots and Imbeciles), the only character set that's not legacy is ISO 10646 and the only character encodings that are not legacy are the Unicode Transformation Formats (UTF).

Name: Anonymous 2007-03-06 7:53 ID:79W2Xuf1

>>13
Is there a way to store a byte as a byte, rather than inside an unsigned int, because if there is, I'm all ears.

Name: Anonymous 2007-03-06 8:00 ID:LHWL4EeZ

In C++, and I believe C also, a char defined to be a byte.  If you're on a machine with 9-bit bytes, a char would have 9 bits also.  An array of char is an array of bytes. 

If your intent is to work with bytes, then using a char is the correct strategy.  >>13 is just being pissy.

Name: Anonymous 2007-03-06 9:13 ID:ndwszCxE

#include <stdio.h>

int main(){
 int c;
 while((c=getchar())!=EOF){
  printf("0x%02x ", c);
 }
 putchar('\n');
 return 0;
}

Name: Anonymous 2007-03-06 9:30 ID:dspnoMdy

[/spoiler][/spoiler]

Name: Anonymous 2007-03-06 14:09 ID:HMWlpYWA

>>17
Fails at spoilers!

Name: Anonymous 2007-03-07 8:44 ID:YyJhA2Em

#include <stdio.h>
main(){for(int c;++c;printf((c=getchar())+1?"0x%02x ":"\n",c));}

Name: Anonymous 2007-03-07 9:21 ID:Heaven

>>19
oops...
#include <stdio.h>
main(){for(int c;++c;printf(c+1?"0x%02x ":"\n",c=getchar()));}

Name: Anonymous 2007-03-07 10:09 ID:Heaven

need less 0x

Name: Anonymous 2007-03-07 11:43 ID:+lmfRaUf

>>19-20
What manner of faggotry is that?

module Main where
import Text.Printf

main :: IO ()
main = getContents >>= mapM_ (printf "%02x")

Name: Anonymous 2007-03-07 13:51 ID:Heaven

>>21
ok
#include <stdio.h>
main(){for(int c;c;printf(++c?" %02x ":"\n",c=getchar()));}


>>22
one that's easier to read and faster than the one in your post.

Name: Anonymous 2007-03-07 16:07 ID:+lmfRaUf

>>23
Not at all, my Haskell implementation is lazy, meaning that the code will not be run if you do not run the main procedure.

Also, the code is much more legible and logical.

Name: Anonymous 2007-03-07 16:14 ID:lTJI+6vi

Well thats useless. Way to go writing something thats been around since the eighties.

Name: Anonymous 2007-03-07 16:27 ID:jQCa0kHv

>>24
Legible my ass. >>= (that was NOT a smiley)

Name: Anonymous 2007-03-07 16:45 ID:Es3ATMzx

>>26
Too bad C can't sequence monadic operations.
I mean slow as fuck.

Name: Anonymous 2007-03-07 19:09 ID:NZNN5sbn

>>24
learn what "lazy" means before using that word, please.

Name: Anonymous 2011-02-04 11:27

Name: Anonymous 2012-03-23 23:41

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

Name: Sgt.Kabu뇲ㄎkimanš㹠 2012-05-28 23:04

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List