Text to Binary file

This is a discussion on Text to Binary file within the C Programming forums, part of the General Programming Boards category; Input file is ASCII text file (*fr). Each double-seguential-text-characters are one Hexa-number: A0FF3046F0 etc.. A0 = 0xA0, FF = 0xFF, ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    Europe Cz Prague
    Posts
    14

    Text to Binary file

    Input file is ASCII text file (*fr). Each double-seguential-text-characters are one Hexa-number:
    A0FF3046F0 etc..
    A0 = 0xA0, FF = 0xFF, etc.
    I need to write 0xA0 0xFF to new binary file (*fw). It is no problem with "fwrite".
    Problem for me is with reading of each 2 seguential characters from text file. I used loop with:
    for (j = 0; j < 2; j++)
    fread (&a, 1, 1, fr);
    fread (&b, 1, 1, fr);
    j = 0;

    First 2 characters are reading OK but all next characters are shifted with one text character Eg.:
    input file: AABBCCDDEE
    shifted output: AA BC DD etc.
    I tried also aply loop with "fseek (fr, 0, (SEEK_SET+i))" but without success.

    Could you please help me? I cannot find solution on this board.
    Thank you.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you have formatted input, try this:
    Code:
      if ((fp = fopen("junk.txt", "r")) != NULL)
      {
        while (fscanf(fp, "%2x", &i) == 1)
        {
          printf ("Number read as %d\n", i);
        }
      }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    Europe Cz Prague
    Posts
    14
    Thank you very much !!
    It is function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21