Thursday, 8 August 2013

how to read back a char as an int in c

how to read back a char as an int in c

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:
/*
* File: main.c
* Author: danielbeckett
*
* Created on 08 August 2013, 12:34
*/
#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?

No comments:

Post a Comment