how to convert from unsigned byte to an integer?

Viewed 233

I am trying to read a file which includes unsigned bytes and I am trying to read them as an integer range of [0,255].

When I look at extended ascii table, when i read that "┌", it is equal to 218, but my program takes as 195 or 226 I don't know why.

This problem happens also on a lot of characters which are in extended part(more than 128).

Why can't I read as ASCII equivalent and how can I fix this? Thanks for reply..

Here is my code,


int main()
{
   unsigned int temp = 0;
   int bytesread;
   int fd = open("inputs.txt", O_RDONLY);

   if(fd == -1)
   {
       printf("An error occured.. \n");
       exit(-1);
   }
   else
   {
       bytesread = read(fd, &temp, 1);
   }

   printf("%d", temp);
   return 0;
}
3 Answers

If you're seeing lots of 195, the input is probably in UTF-8 character encoding.

ASCII only goes up to 127 , there's no single standard "extended ascii". There is ISO-8859-1 but that does not have . Perhaps you refer to CP 437 .

Your ways forward from here would fall into one of two broad approaches:

  • Convert the file from UTF-8 into another encoding such as CP437, using tools for your operating system or otherwise.
  • Read UTF-8 in your C program; you can either do this from scratch or use a pre-existing library.

It's possible the character is stored in the file using UTF-8 encoding.

For example, the character has a Unicode hex codepoint 250c, and the UTF-8 byte sequence is e2 94 8c. The e2 is equal to your decimal 226 which suggests that your character may in fact be in a nearby Unicode block and UTF-8 encoded.

As has been suggested in comments, it would be very helpful if you provided a hexdump of the file, for example like this:

hexdump -C inputs.txt

This code

   bytesread = read(fd, &temp, 1);

reads one byte into the first byte of an unsigned int, which almost certainly is larger than a single byte. So where your data that you read ends up in the int value depends on your system.

If you're going to read a single byte, it's usually much easier to just use an [unsigned] char so you always know where it will end up. To convert an unsigned char into an int, you can just assign it:

int main()
{
   int fd = open("inputs.txt", O_RDONLY);

   if(fd == -1)
   {
       // perror() will tell you **WHAT** error occurred
       perror( "open()" );
       exit(-1);
   }

   // this is now an unsigned char
   unsigned char temp;

   // read() returns ssize_t, not int
   ssize_t bytesread = read( fd, &temp, sizeof( temp ) );
   if ( bytesread != sizeof( temp ) )
   {
       perror( "read()" );
       close( fd );
       exit( -1 );
   }

   close( fd );

   // there are a lot of ways to do this
   printf( "unsigned int value: %u\n", ( unsigned int ) temp );

   // this is another way - it prints the hex value
   printf( "hex value: %hhx\n", temp );

   // this prints the char value:
   printf( "char value: '%c'\n", temp;

   // this converts that unsigned char into an int:
   int intvalue = temp;

   // yes, it's that simple.
   printf( "int value: %d\n", intvalue  );

   return 0;
}

Note that results can be different if sizeof( int ) == sizeof( unsigned char ). In that case, there can be unsigned char values that can not be represented as an int value.

Related