0

I am scrolling through a file of characters and saving its data into an array. the file looks like this: http://pastebin.com/dx4HetT0

I've deleted the header information so it's literally a text file with numbers in it. I want to convert these char numbers into bytes in my program so I can do some conversion maths on them.

My code is as follows:

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

#include "../DataProcessing/include/packet.h"

int main ( int argc, char *argv[] )
{
    /*
    // We assume argv[1] is a filename to open
    FILE *file = fopen( argv[1], "r" );
    */

    FILE *file = fopen("C:\\log_hex.txt", "r");

    /* fopen returns 0, the NULL pointer, on failure */
    if ( file == 0 )
    {
        printf( "Could not open file\n" );
    }
    else 
    {
        int x;
        int count = 0;
        int byteArray[99999];
        /* read one character at a time from file, stopping at EOF, which
           indicates the end of the file.  Note that the idiom of "assign
           to a variable, check the value" used below works because
           the assignment statement evaluates to the value assigned. */
        while  ( ( x = fgetc( file ) ) != EOF )
        {
            byteArray[count] = x;
            printf( "%c", x );
            count++;
        }
        fclose( file );
        getchar();
    }
}

byteArray gets filled with the characters but not in the way I want - I'm getting a character 0 represented as the numerical value 53, 4 is represented as 52, space is represented as 32.... how can I read the character number, and make that number the char value in my byteArray?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
dannybeckett
  • 3
  • 1
  • 4
  • You are looking for `strtol()`. –  Aug 08 '13 at 14:27
  • possible duplicate of [Converting string to integer C](http://stackoverflow.com/questions/7021725/converting-string-to-integer-c) –  Aug 08 '13 at 14:27
  • [Check this question too](http://stackoverflow.com/questions/7787423/c-get-nth-byte-of-integer) – Suvarna Pattayil Aug 08 '13 at 14:27
  • @H2CO3: why not scanf, as he has already a FILE* open? – andyn Aug 08 '13 at 14:27
  • 3
    @andyn Because `scanf()` is evil and does not do what you think it does. But if you wish so, go ahead... –  Aug 08 '13 at 14:28
  • why not atoi? Im not as advanced as alot of people i was just wondering... – SD1990 Aug 08 '13 at 14:49
  • @SD1990: because `atoi()` does not detect errors. – Crowman Aug 08 '13 at 15:35
  • The [`atoi()` function](http://pubs.opengroup.org/onlinepubs/9699919799/functions/atoi.html) is defined, basically, as a call to the [`strtol()` function](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html) but without error handling. Therefore, unless you really _like_ Undefined Behavior, `strtol()` is the way to go. – This isn't my real name Aug 08 '13 at 16:31
  • @ElchononEdelson: `atoi()` should never give you undefined behavior, and it's basically equivalent to `strtol(nstr, NULL, 10)`. The chief objection to `atoi()` is that, unlike `strtol()`, it gives you no way of distinguishing whether a return value of `0` means the supplied string did not begin with digits and no conversion therefore took place, or whether `nstr` consisted of `"0"` and was successfully converted. – Crowman Aug 08 '13 at 17:07
  • @PaulGriffiths Please, feel free to read the [documentation for the `atoi()` function](http://pubs.opengroup.org/onlinepubs/9699919799/functions/atoi.html), which clearly states the conditions under which the `atoi()` function does, in fact, give you Undefined Behavior. (N1256 section 7.20.1, N1570 section 7.22.1). Conditions, I note, which are explicitly handled by the [`strtol()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html) function. (N1256 7.20.1.4 8, N1570 7.22.1.4 8) – This isn't my real name Aug 08 '13 at 18:28
  • @ElchononEdelson: You're correct, that's what you get for cutting corners and relying on man pages. – Crowman Aug 08 '13 at 18:55
  • Thank you for explaining that in detail and not being complete A@!*@@@!£$£s about it, like im sure other users would have been. Id actually never heard of strtol() before and thus the question. – SD1990 Aug 09 '13 at 07:06

2 Answers2

0

You are reading in byte values in ascii. You need to either use a library function like strtol to convert to actual values.

Although I think there is a typo in your question - I doubt 0 is coming out as 53, that's ascii for 3

If you know it is a digit, you can do

x-='0';

to get the value.

Oliver Matthews
  • 7,497
  • 3
  • 33
  • 36
  • 1
    It's not a decimal digit but a hexadecimal one. –  Aug 08 '13 at 14:29
  • @Oliver Matthews This is a screenshot of what I'm getting back - http://snag.gy/gEyJa.jpg Perhaps that's `0x53`. Would you mind explaining how `x-='0'` works? – dannybeckett Aug 08 '13 at 14:35
  • Oh, you meant [0] is coming out as 53. That makes sense - the first digit is a 5. -'0' works because the character '0' and the ascii value 48 are the same and because ascii stores 0-9 in order as 48-57 (dec). You could just as easily write `x-=48;`, it's just `-'0'` makes it clearer what you are doing. – Oliver Matthews Aug 08 '13 at 15:03
  • @OliverMatthews: Just for clarity, they're not equivalent. C does not require an ASCII character set, but whatever character set you do have, it does require that the decimal digits be contiguous and in order. So with `char c` where `c` is a decimal digit, `c -= '0'` is guaranteed to get you the value of the digit it represents. `c -= 48` is *not* guaranteed to get you this, if your implementation is not using a character set where `'0' == 48`. – Crowman Aug 08 '13 at 15:45
0

Note that you are storing a char value inside an integer array. Also you are printing an integer value in char format.

Since there are numbers with more than one digit in your input file, you probably need a char buffer to store the whole number (series of digits, e.g. read numeric characters until you read a space). After that you need to convert your buffer to integer, using strtol as already stated.

To test your results make sure to use the correct format in printf. Use "%d" for integers, "%c" for characters etc.

Alek Sobczyk
  • 813
  • 1
  • 7
  • 6
  • I'm struggling with this char buffer/read until space code, how would you do it? – dannybeckett Aug 08 '13 at 15:22
  • A bit late but something like this : declare a `char buf[3]`. Then use another loop inside your main loop, `while(x!=' ')` to make sure you read the whole number inside buf, and not just one digit. Then use strtol to convert buf to int. memset buf to '\0' after each number read. – Alek Sobczyk Aug 15 '13 at 16:10