1 / 39

Encoding of alphanumeric and special characters

Encoding of alphanumeric and special characters. As previously noted, a byte can contain a numeric value in the range 0-255 . Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets !

niles
Download Presentation

Encoding of alphanumeric and special characters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Encoding of alphanumeric and special characters As previously noted, a byte can contain a numeric value in the range 0-255. Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets! Alphanumeric and special characters of the Latin alphabet are stored in memory as integer values encoded using the ASCII code. (American standard code for the interchange of information). Other codes requiring 16 bits per character have been developed to support languages having large number of written symbols.

  2. A program for displaying the ASCII encoding scheme • The correspondence between decimal, hexadecimal, and character representations can be readily generated via the following simple program. • #include <stdio.h> • intmain(intargc, char *argv[]) • { • intc; • c = ' '; /* Same as c = 32 or c = 0x20 */ • while (c <= 'z') • { • printf(“%3d %02x %c \n", c, c, c); • c = c + 1; • } • }

  3. Output of the ASCII table generator : class/210/examples ==> gcc -o p2 p2.c class/210/examples ==> p2 | more 32 20 33 21 ! 34 22 " 35 23 # 36 24 $ 37 25 % 38 26 & 39 27 ' 40 28 ( 41 29 ) 42 2a * 43 2b + 44 2c , 45 2d - 46 2e . 47 2f / 48 30 0 49 31 1 :

  4. Output of the ASCII table generator 58 3a : 59 3b ; 60 3c < 61 3d = 62 3e > 63 3f ? 64 40 @ 65 41 A 66 42 B : 89 59 Y 90 5a Z 91 5b [ 92 5c \ 93 5d ] 94 5e ^ 95 5f _ 96 60 ` 97 61 a 98 62 b :

  5. Output of the ASCII table generator 120 78 x 121 79 y 122 7a z There are also a few special characters that follow z.

  6. Control characters: The ASCII encodings between decimal 0 and 31 are used to encode what are commonly called control characters. Control characters having decimal values in the range 1 through 26 can be entered from the keyboard by holding down the ctrlkey while typing a letter in the set a through z. Some control characters have “escaped code” representations in C, but all may be written in octal. Dec Keystroke Name Escaped code 4 ctrl-D end of file ‘\004' 8 ctrl-H backspace '\b' 9 ctrl-I tab '\t' 10 ctrl-J newline '\n' 12 ctrl-L page eject '\f' 13 ctrl-M carriage return '\r'

  7. Formatted I/O: Printing integer values: • As previously noted integer values are stored in computer memory using a binary representation. • To communicate the value of an integer to a human, it is necessary to produce the string of ASCII characters that correspond to the rendition of the integer in the Latin alphabet. • For example the consider the byte: 1001 0100 • This is the binary encoding of the hex number 0x94 which is the decimal number 9 * 16 + 4 = 148. Therefore, if this number is to be rendered as a decimal number on a printer or display, threebytes corresponding to the ASCII encodings of 1, 4, and 8 must be sent to the printer or display. These bytes are expressed in hexadecimal as: • 31 34 38

  8. Formatted I/O: Printing integer values: In the C language, run time libraries provide functions that interface with the Operating System to provide this service. Some of these functions may actually be implemented as macros that call the actual RTL functions. The printf() function (actually a macro that converts to fprintf(stdout, ) ) is used to produce the ASCII encoding of an integer and send it to an output device or file.

  9. Formatted I/O: Printing integer values • Format codes specify how you want to see the integer represented. • %c Consider the integer to be the ASCII encoding of a character and render that character • %dProduce the ASCII encoding of the integer expressed as a decimal number • %xProduce the ASCII encoding of the integer expressed as a hexadecimal number • %oProduce the ASCII encoding of the integer expressed as an octal number

  10. Formatted I/O: Printing integer values • Specifying field width: The format codesmay be preceded by an optional field width specifier. The code %02x shown below forces the field to be padded with leading 0's if necessary to generate the specified field width. • #include <stdio.h> • intmain(intargc, char *argv[]) • { • int x; • int y = 78; • x = 'A' + 65 + 0101 + 0x41 + '\n'; • printf("X = %d \n", x); • printf("Y = %c %3d %02x %4o \n", y, y, y, y); • } • /home/rlowe==> gcc -o p1 p1.c • /home/rlowe==> p1 • X = 270 • Y = N 78 4e 116 • The number of values printed by printf() is determined by the number of distinct format codes.

  11. Output redirection • The printf() function sends its output to a logical file commonly known as the standard outputor simply stdout. • When a program is run in the Unix environment, the logical file stdoutis by default associated with the screen being viewed by the person who started the program. • The > operator may be used on the command line to cause the standard output to be redirectedto a file: • class/210/examples ==> p1 > p1.output • A file created in this way may be subsequently viewed using the cat command (or edited using a text editor). • class/210/examples ==> cat p1.output • X = 270 • Y = N 78 4e 116

  12. Input of integer data • When a human enters numeric data using the keyboard, the values passed to a program are the ASCII encodings of the keystrokes that were made. • For example, when I type: • 123.45 • 6 bytes are produced by the keyboard. The hexadecimal encoding of these bytes is: • 31 32 33 2E 34 35 - hex • 1 2 3 . 4 5 - ascii • To perform arithmetic using my number, it must be converted to internal floating point representation.

  13. Input of integer data • The scanf()function is used to • 1 - consume the ASCII encoding of a numeric value • 2 - convert the ASCII string to the proper internal representation • 3 - store the result in a memory location provided by the caller. • As with printf(), a format code controls the process. The format code specifies both the input encodingand the desired type of value to be produced: • %d string of decimal characters int • %x string of hex characters unsigned int • %o string of octal characters unsigned int • %c asciiencoded character char • %f floating point number in decimal float • %lf floating point number in decimal double • %e floating pt in scientific notation float

  14. Input of integer data Also as with printf() the number of values that scanf() will attempt to read is determined by the number of format codes provided. For each format code provided, it is mandatory that a variable be provided to hold the data that is read.

  15. Input of integer data • Specifying the variables to receive the values: • It is extremely important to note that: • the valueto be printed is passed to printf() • but • the addressof the variable to receive the value must be passed to scanf() • The & operator in C is the “address of “ operator.

  16. Input of integer data • Formatted input of integer values • /* p3.c */ • #include <stdio.h> • intmain(intargc, char *argv[]) • { • int a; • int r; • int b; • r = scanf(“%d %d“, &a, &b); • printf(“Got %d items with values %d %d \n”, • r, a, b); • }

  17. Input of integer data Care and feeding of input format specifiers Embedding of extra spaces and including '\n' in scanf() format strings can also lead to wierdbehavior and should be avoided. Specification of field widths is dangerous unless you really know what you are doing.

  18. Input of integer data – Effect of invalid input • /* p3.c */ • #include <stdio.h> • int main(intargc, char *argv[]) • { • int a; • int r; • int b; • r = scanf(“ %d %d “, &a, &b); • printf(“Got %d items with values %d %d \n”, • r, a, b); • } • If you accidentally enter a non-numeric value, scanf() will abort and return only 1 value. The value 4927 represents the uninitialized value of b. • class/210/examples ==> p3 • 1 t 6 • Got 1 items with values 1 4927

  19. Input of integer data • Pitfalls of field widths: • It is legal to specify field widths to scanf() but usually dangerous to do so! • class/210/examples ==> cat p4.c • #include <stdio.h> • intmain(intargc, char *argv[]) • { • int a; • int r; • int b; • r = scanf(" %2d %2d ", &a, &b); • printf("Got %d items with values %d %d \n", • r, a, b); • } • class/210/examples ==> p4 • 123 456 • Got 2 items with values 12 3

  20. Input of integer data Detecting end-of-file with scanf() scanf() returns the number of values it actually obtained (which may be less than the number of values requested); therefore, the proper way to test for end-of-file is to ensure that the number of values obtained was the number of values requested.

  21. Input of integer data • For example, • /* p4b.c */ • #include <stdio.h> • intmain(intargc, char *argv[]) • { • int a; • int r; • int b; • while ((r = scanf("%d %d", &a, &b)) == 2) • { • printf("Got %d items with values %d %d \n", • r, a, b); • } • }

  22. Input of integer data • Input redirection • Like the stdoutthe stdinmay also be redirected. To redirect both stdinand stdoutuse: • a.out < input.txt > output.txt • when invoked in this manner when the program a.outreads from the stdinvia scanf() or • fscanf(stdin,.) the input will actually be read from a file named input.txt and any data written to the stdoutwill end up in the file output.txt.

  23. The necessity of format conversion As stated previously, the %dformat causes scanf() to convert the ASCII text representation of a number to an internal binary integer value. One might think it would be possible to avoid this and be able to deal with mixed alphabetic and numeric data by simply switching to the %c format. However, if we need to do arithmetic on what we read in, this approach does not work.

  24. The necessity of format conversion • /* p11c.c */ • #include <stdio.h> • int main(void) • { • char i, j, k; • scanf("%c %c %c", &i, &j, &k); • printf("The first input was %c \n", i); • printf("The third input was %c \n", k); • printf("Their product is %c \n", i * k); • } • class/210/examples ==> p11c • 2 C 4 • The first input was 2 • The third input was 4 • Their product is (

  25. The necessity of format conversion Why do we get “('' and not 8 for the answer? Since no format conversion is done the value stored in iis the ASCII code for the numeral 2 which is 0x32 = 50 Similarly the value stored in j is 0x34 = 52. When they are multiplied, the result is 50 x 52 = 2600 which is too large to be held in 8 bits. The 8 bit product is (2600 mod 256) (the remainder when 2600 is divided by 256). That value is 2600 - 2560 = 40 = 0x28 which according to the ASCII table is the encoding of “(''

  26. Floating point input and output The %e and %f format codes are used to print floating point numbers in scientific and decimal format. The l modifier should be used for doubles. Field size specificiation is of the form: field-width.number-of-digits-to-right-of-decimal

  27. Floating point input and output • #include <stdio.h> • intmain(intargc, char *argv[]) • { • float a; • double b; • float c; • double d; • a = 1024.123; • b = 1.024123e+03; • scanf("%f %le", &c, &d); • printf("a = %10.2f b = %8.4lf c = %f d = %8le \n", • a, b, c, d); • } • class/2100/examples ==> gcc -o p5 p5.c • class/2100/examples ==> p5 • 12.4356 1.4e22 • a = 1024.12 b = 1024.1230 c = 12.435600 d = 1.400000e+22

  28. General Input and Output • The C language itself defines no facility for I/O operations. I/O support is provided through two collections of mutually incomptiblefunction libraries • Low level I/O • open(), close(), read(), write(), lseek(), ioctl() • Standard library I/O • fopen(), fclose() - opening and closing files • fprintf(), fscanf() - field at a time with data conversion • fgetc(), fputc() - character (byte) at a time • fgets(), fputs() - line at a time • fread(), fwrite(), fseek() - physical block at a time

  29. General Input and Output • Our focus will be on the use of the standard I/O library: • Function and constant definitions are obtained via the #include facility. To use the standard library functions: • #include <stdio.h> • #include <errno.h>

  30. Standard library I/O The functions operate on ADT's of type FILE *(which is defined in stdio.h). Three special FILE *'s are automatically opened when any process (program) starts: stdin Normally keyboard input (but may be redirected with < ) stdout Normally terminal output (but may be directed with > or 1> ) 1 stderr Normally terminal output (but may be directed with 2> ) Since these files are predeclared and preopendthey must not be declared nor opened in your program!The following example shows how to open, read, and write disk files by name. In our assignments we will almost always read from stdin and write to stdout or stderr. 1The 1> and 2> notation is supported by the shfamily of shells incudingbash,but is not supported in csh, tcsh.

  31. Example • #include <stdio.h> • int main() { • FILE *f1; • FILE *f2; • int x; • f1 = fopen(“in.txt”, “r”); • if (f1 == 0) { • perror(“f1 failure”); • exit(1); • } • f2 = fopen(“out.txt”, “w”); • if (f2 == 0) { • perror(“f2 failure”); • exit(2); • } • if (fscanf(f1, “%d”, &x) != 1) { • perror(“scanf failure”); • exit(2); • } • fprintf(f2, “%d”, x); • fclose(f1); • fclose(f2); • }

  32. Examples of the use of p6 In this example, we have not yet created in.txt class/2100/examples ==> gcc -o p6 p6.c class/2100/examples ==> p6 f1 failure:: No such file or directory cat: in.txt: No such file or directory Here, in.txt is created with the cat command: class/210/examples ==> cat > in.txt 99 Now if we rerun p6 and cat out.txt we obtain the correct answer class/2100/examples ==> p6 class/2100/examples ==> cat out.txt 99 Note: The scanf() and printf() functions are actually macros or abbreviations for: fscanf(stdin, ...) and (stdout, ...)

  33. Character at a time input and output The fgetc() function can be used to read an I/O stream one byte at a time. The fputc() function can be used to write an I/O stream one byte at a time. Here is an example of how to build a catcommand using the two functions. The p10 program is being used here to copy its own source code from standard input to output.

  34. Character at a time input and output • class/2100/examples ==> p10 < p10.c • /* p10.c */ • #include <stdio.h> • main() • { • int c; • while ((c = fgetc(stdin)) > 0) • fputc(c, stdout); • } • While fputc() and fgetc() are fine for building interactive applications, they are very inefficient and should neverbe used for reading or writing a large volume of data such as a • photographic image file.

  35. Line at a time input and output • The fgets(buffer_address, buf_size, file) function can be used to read from a stream until either: • 1 - a newline character '\n' = 10 = 0x0a is encountered or • 2 - the specified number of characters - 1 has been read or • 3 - end of file is reached. • There is no string data type in C, but a standard convention is that a string is an array of characters in which the end is of the string is marked by the presence of a byte which has the value binary 00000000 (sometimes called the NULLcharacter). • fgets() will append the NULL character to whatever it reads in.

  36. Line at a time input and output Since fgets() will read in multiple characters it is not possible to assign what it reads to a single variable of type char. Thus a pointer to a buffermust be passed (as was the case with scanf()). The fputs() function writes a NULL terminated string to a stream (after stripping off the NULL). The following example is yet another cat program, but this one works one line at a time.

  37. Line at a time input and output • class/2100/examples ==> p11 < p11.c 2> countem • /* p11.c */ • #include <stdio.h> • #include <string.h> • main() • { • unsigned char *buff = NULL; • int line = 0; • buff = malloc(1024); // alloc storage to hold data • if (buff == 0) • exit(1); • while (fgets(buff, 1024, stdin) != 0) • { • fputs(buff, stdout); • fprintf(stderr, "%d %d \n", line, strlen(buff)); • line += 1; • } • free(buff); // release the storage malloc allocated • }

  38. Line at a time input and output Here buffis declared a pointer and the storage which will hold the data is allocated with malloc() Alternatively we could have declared unsigned char buff[1024] and not used malloc()

  39. Line at a time input and output Here is the output that went to the standard error. Note that each line that appeared empty has length 1 (the newline character itself) and the lines that appear to have length 1 actually have length 2. class/210/examples ==> cat countem 0 12 1 1 2 19 3 20 4 1 5 7 6 2 7 24 8 18 9 1 10 24 11 18 12 15 13 1 14 41 15 5 16 27 17 55 18 17 19 4 20 2

More Related