Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to read back a char as an int in c
    primarykey
    data
    text
    <p>I am scrolling through a file of characters and saving its data into an array. the file looks like this: <a href="http://pastebin.com/dx4HetT0" rel="nofollow">http://pastebin.com/dx4HetT0</a></p> <p>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.</p> <p>My code is as follows:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #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(); } } </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload