Presentation is loading. Please wait.

Presentation is loading. Please wait.

String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,

Similar presentations


Presentation on theme: "String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,"— Presentation transcript:

1

2 String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters, numerical characters and a large set of characters for a variety of purposes like: ! @ # $ % ^ & * ( ) _ +

3 String Literal Definition A string literal is a set of characters delimited with double quotations like: "Seymour Snodgrass" and "SSN: 123-45-6789"

4 // Java1601.java // This program demonstrates how to declare five String objects. // Note that all five string objects store the same information. public class Java1601 { public static void main (String args[]) { String s1 = "Tango"; System.out.println("s1: " + s1); String s2; s2 = "Tango"; System.out.println("s2: " + s2); String s3 = new String("Tango"); System.out.println("s3: " + s3); String s4 = new String(); s4 = "Tango"; System.out.println("s4: " + s4); char Dance[] = {'T','a','n','g','o'}; String s5 = new String(Dance); System.out.println("s5: " + s5); System.out.println(); }

5 Mathematical Addition 100 + 200 = 300 int x = 100; x += 200;

6 String Concatenation “100” + “200” = “100200” String x = “100”; x += “200”;

7 // Java1602.java // This program shows how to concatenate strings using the // + operator and the method. public class Java1602 { public static void main (String args[]) { String s1 = "Argentine"; String s2 = "Tango"; String s3 = s1 + " " + s2; System.out.println("s3: " + s3); String s4 = "Argentine"; s4 = s4.concat(" Tango"); System.out.println("s4: " + s4); System.out.println(); }

8 String Method concat s1 = s2.concat("hiss"); concat concatenates the method argument to the object String. If s2 == "boo" then s1 will become "boohiss"

9 // Java1603.java // This program demonstrates the use of the method. public class Java1603 { public static void main (String args[]) { String s1 = "Argentine"; String s2 = "Tango"; String s3 = s1 + " " + s2; System.out.println(s1 + " has " + s1.length() + " characters."); System.out.println(s2 + " has " + s2.length() + " characters."); System.out.println(s3 + " has " + s3.length() + " characters."); System.out.println(); }

10 // Java1604.java // This program demonstrates how to access individual characters of // a String object with the method. public class Java1604 { public static void main (String args[]) { String s1 = "Madam I'm Adam"; String s2 = ""; int N = s1.length() - 1; for (int K = N; K >= 0; K--) s2 += s1.charAt(K); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); }

11 String Method charAt letter = s.charAt(k); Method charAt returns the character stored at the k th index location of the String object s. The first character is at index 0. If s == "Aardvark" and k == 4 then letter becomes v

12 // Java1605.java // This program demonstrates how to access specified characters of // a string with the method, where SI is the // StartIndex and EI is one less than the EndIndex. public class Java1605 { public static void main (String args[]) { String s = "Racecar"; int n = s.length(); for (int k = 1; k <= n; k++) System.out.println(s.substring(0,k)); System.out.println(); for (int k = 0; k <= n-3; k++) System.out.println(s.substring(k,k+3)); System.out.println(); }

13 String Method substring s1 = “Aardvark”; s2 = s1.substring(0,k); Method substring returns a set of consecutive characters from String s1, starting at index 0, and ending at index k-1. s3 = s1.substring(4,8); s2 becomes "Aard" and s3 becomes "vark" s1 01234567 Aardvark

14 // Java1606.java // This program shows the method, which returns the index of the first // occurrence of the string argument, and the method, which returns the // index of the last occurrence of the string argument. public class Java1606 { public static void main (String args[]) { String s1 = "racecar"; String s2 = "racecar in the carport"; String s3 = "car"; int index1 = s1.indexOf(s3); int index2 = s1.lastIndexOf(s3); int index3 = s2.indexOf(s3); int index4 = s2.lastIndexOf(s3); int index5 = s1.indexOf("qwerty"); System.out.println("With \"" + s1 + "\" car starts at " + index1 + " and last shows up at " + index2); System.out.println("With \"" + s2 + "\" car starts at " + index3 + " and last shows up at " + index4); System.out.println("With \"" + s3 + "\" Qwerty shows up at " + index5); System.out.println(); }

15 String Methods indexOf & lastIndexOf indexOf returns the first occurrence of a substring s1.indexOf(“hum”); returns 0 s1.indexOf(“ku”); returns 10 s1.indexOf(“qwerty”); returns -1 lastIndexOf returns the last occurrence of a substring s1.lastIndexOf(“hum”); returns 4 s1.lastIndexOf(“ku”); returns 14 s1.lastIndexOf(“qwerty”); returns -1 Note: The i in i ndexOf is lowercase, but in last I ndexOf it’s capital! s1 0123456789101112131415161718192021 humuhumunukunukuapua’a By the way, its the State Fish of Hawaii.

16 // Java1607.java // This program demonstrates the String // method, which replaces all occurrences of the Old character // with the New character. // This method creates a new String object with the replaced // characters. public class Java1607 { public static void main (String args[]) { String s1 = "racecar"; String s2 = s1.replace('r','l'); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); }

17 String Method replace String s2 = s1.replace('m','b'); Method replace returns a string such that every occurrence of the old character ( 'm' ) is replaced by the new character ( 'b' ). If s1 == "madam" then s becomes "badab"

18 // Java1608.java // This program demonstrates the String and methods. public class Java1608 { public static void main (String args[]) { String s1 = "racecar"; String s2 = "RaCeCaR"; String s3 = "RACECAR100"; String s4 = s1.toUpperCase(); String s5 = s2.toUpperCase(); String s6 = s3.toUpperCase(); System.out.println("s1 --> s4: " + s4); System.out.println("s2 --> s5: " + s5); System.out.println("s3 --> s6: " + s6); System.out.println(); s1 = s4.toLowerCase(); s2 = s5.toLowerCase(); s3 = s6.toLowerCase(); System.out.println("s4 --> s1: " + s1); System.out.println("s5 --> s2: " + s2); System.out.println("s6 --> s3: " + s3); System.out.println(); }

19 String Methods toUpperCase and toLowerCase s1 = s2.toLowerCase; s3 = s1.toUpperCase; Method toLowerCase returns a string with lower-case letters. Method toUpperCase returns a string with upper-case letters. Any characters that are not letters will be ignored by both methods and returned in their same relative string position.

20 // Java1609.java // This program demonstrates the method of the String class, // which is shown to convert four data types to a string. // Note that is a static method and must be called using //. public class Java1609 { public static void main (String args[]) { String s1 = String.valueOf(1000); String s2 = String.valueOf(123.321); String s3 = String.valueOf(true); String s4 = String.valueOf('A'); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println("s3: " + s3); System.out.println("s4: " + s4); System.out.println(); }

21 String static Method valueOf String s1 = String.valueOf(1000); String s2 = String.valueOf(123.321); String s3 = String.valueOf(true); String s4 = String.valueOf('A'); Method valueOf converts the provided parameter and returns a string. Four overloaded valueOf methods are displayed. Note that the valueOf method is a static method (or class method) that is called with the String class identifier.

22 // Java1610.java // This program converts string values to integer and double values // using the and methods of the // and classes. public class Java1610 { public static void main (String args[]) { String s1 = "12345"; String s2 = "123.321"; int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); System.out.println(n1 + " + " + n1 + " = " + (n1 + n1)); System.out.println(n2 + " + " + n2 + " = " + (n2 + n2)); System.out.println(); }

23 Integer static method parseInt and Double static method parseDouble int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); Method parseInt converts a String into an int. Method parseDouble converts a String into a double. Parameters that include non-numerical characters will compile, but will cause a run-time error.

24 // Java1611.java // This program checks the equality of two strings with the == operator. // The program executes as you might expect. public class Java1611 { public static void main (String args[]) { String s1 = "Foxtrot"; String s2 = "Waltz"; String s3 = "Foxtrot"; if (s1 == s2) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1 == s3) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); }

25 // Java1612.java // This program checks equality of strings, but this time a string entered at the keyboard // is used for comparison. This program has unexpected results. import java.util.Scanner; public class Java1612 { public static void main (String args[]) { Scanner input = new Scanner(System.in); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = input.nextLine(); if (s1 == s2) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1 == s3) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); }

26 // Java1613.java // This program uses the method, which removes "white space" from both ends of // the string argument. This method is used to try and solve the problem of the previous // program. import java.util.Scanner; public class Java1613 { public static void main (String args[]) { Scanner input = new Scanner(System.in); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = input.nextLine(); if (s1 == s2) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1 == s3. trim ()) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); }

27 String Method trim s1 = s2.trim(); String method trim returns a string with "white space" removed from both ends of the String object.

28 // Java1614.java // This program demonstrates the method, which is capable of // testing equality of string objects correctly. import java.util.Scanner; public class Java1614 { public static void main (String args[]) { Scanner input = new Scanner(System.in); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = input.readLine(); if ( s1.equals(s2) ) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if ( s1.equals(s3) ) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); }

29 What Is Going On? Part 1 s1 @dff6ccd s2 @3b0eb0 dff6ccd Foxtrot 3b0eb0 Waltz s3 @18d107f 18d107f Foxtrot

30 What Is Going On? Part 2 s1 @dff6ccd s2 @3b0eb0 dff6ccd Foxtrot 3b0eb0 Waltz s3 @18d107f 18d107f Foxtrot

31 // Java1615.java // This program demonstrates the method, which returns an integer value. // The value is 0 when the strings are equal, otherwise a value is returned that // indicates the relative distance between the strings. public class Java1615 { public static void main (String args[]) { String s1 = "AARDVARK"; String s2 = "ZEBRA"; String s3 = "AARDVARK"; String s4 = "BART"; int value1 = s1.compareTo(s2); int value2 = s1.compareTo(s3); int value3 = s2.compareTo(s1); int value4 = s1.compareTo(s4); System.out.println("value1: " + value1); System.out.println("value2: " + value2); System.out.println("value3: " + value3); System.out.println("value4: " + value4); System.out.println(); }

32 String methods equals and compareTo if (s1.equals(s2)) int distance = s3.compareTo(s4); Method equals returns true is s1 == s2, and false otherwise. Method compareTo returns 0 if s3 == s4, otherwise an integer is returned based on the difference between s3 and s4. If s3 < s4, the returned value is negative. If s3 > s4, the returned value is positive.

33 String Objects are Immutable A mutator is a method that mutates or alters object values. The character contents of a String object cannot be altered, which means that String objects are immutable. You need to construct a StringBuffer object if it is necessary to change the character content after the object is constructed.

34 What do you mean "A String cannot be altered?!" If you look at these statements: it sure looks like name is being altered. In reality what happens is the old String object is destroyed and a new object (with a new memory address) is created. String name = "Bob"; name = "Joe";

35

36 AP Exam Alert Number System conversion questions have shown up on recent APCS Examinations. Even if you are not taking the AP Exam, a good understanding of Number Systems is vital for most technology fields.

37 Counting In Other Number Systems Counting is something that you will likely take for granted, at least counting in base-10. In base-10 there are ten different single digits from 0 to 9. Counting in base-10 requires cycling through these ten digits. Every time 9 is reached the counting starts over at 0, and at the same time the digit in the next column is incremented by one. 0123456789 10111213141516171819............................... 90919293949596979899 100101102103104105106107108 109 However, consider the fact that there are 60 seconds in a minute, 60 minutes in an hour. How about 12 inches to a foot, 3 feet to a yard, and 1,760 yards to a mile. Not everything is done in base-10.

38 The Odometer Analogy The odometer is the part of the car that tells you how many miles you have driven. Example: Since each place holder can hold 10 possible digits (0-9), we can say that this odometer is counting in base-10. If we continued to let it count, we would see the following: 314266 314267 314268 314269 314270 314272 314273 314274 314275 314271314276

39 The Broken Odometer Now suppose somebody decides to remove all of the 8s and 9s from your car’s odometer. Since each place holder can hold 8 possible digits (0-7), we can say that this odometer is counting in base-8. If we continued to let it count, we would see the following: 314266 314267 314270 314271 314272 314274 314275 314276 314277 314273314300

40 Counting Rules for Numbers of All Bases There are as many single digits as the base value. The largest single digit is 1 less than the base value. You could say that in base N the range of single digits is from 0 to N-1. Base 100, 1, 2, 3, 4, 5, 6, 7, 8, 9 Base 90, 1, 2, 3, 4, 5, 6, 7, 8 Base 80, 1, 2, 3, 4, 5, 6, 7 Base 70, 1, 2, 3, 4, 5, 6 Base 60, 1, 2, 3, 4, 5 Base 50, 1, 2, 3, 4 Base 40, 1, 2, 3 Base 30, 1, 2 Base 20, 1

41

42

43 List the next 10 base-8 numbers after 321 322 323 324 325 326 327 330 331 332 333 List the next 10 base-8 numbers after 406 407 410 411 412 413 414 415 416 417 420 List the next 10 base-4 numbers after 121 122 123 130 131 132 133 200 201 202 203 List the next 10 base-3 numbers after 11 12 20 21 22 100 101 102 110 111 112 List the next 10 base-2 numbers after 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111

44 Counting In Base-16 Counting in base-16 is significant in computer science. You will see that computer memory addresses are displayed in base-16 or hexadecimal. A computer’s NIC (Network Interface Card) has a special address which is a base-16 number. Future IP addresses will use base-16 as well. There is a unique relationship between base-2 and base-16 that we will look at later. This relationship is the reason base-16 is used in Assembly Language. Right now you need to learn how to count in base-16.

45 The Really Messed Up Odometer Now suppose somebody decides to add 6 digits (A-F) to your car’s odometer. Since each place holder can hold 16 possible digits (0-F), we can say that this odometer is counting in base-16. If we continued to let it count, we would see the following: 3AC266 3AC267 3AC268 3AC269 3AC26A 3AC26C 3AC26D 3AC26E 3AC26F 3AC26B3AC270

46 Counting In Base-16 (continued)

47 List the next 10 base-16 numbers after 100 101 102 103 104 105 106 107 108 109 10a List the next 10 base-16 numbers after 64 65 66 67 68 69 6a 6b 6c 6d 6e List the next 10 base-16 numbers after 1001 1002 1003 1004 1005 1006 1007 1008 1009 100a 100b List the next 10 base-16 numbers after abc abd abe abf ac0 ac1 ac2 ac3 ac4 ac5 ac6 List the next 10 base-16 numbers after 999 99a 99b 99c 99d 99e 99f 9a0 9a1 9a2 9a3

48

49 Flashback to Elementary School Do you remember learning about the One’s Column, Ten’s Column, Hundred’s Column’s, Thousand’s column, etc. back in elementary school. Understanding how Base-10 works is essential to understanding how other bases work. Consider the representation of the number 1,234,567 below: 1,000,000100,00010,0001,000100101 1234567 1,000,000200,00030,0004,000500607 1,000,000 + 200,000 + 30,000 + 4,000 + 500 + 60 + 7 = 1,234,567

50 Scientific Notation Flashback Do you remember learning about powers of 10, exponents, and Scientific Notation. This is also essential. Consider the representation of the number 1,234,567 below: 1,000,000100,00010,0001,000100101 10 6 10 5 10 4 10 3 10 2 10 1 10 0 1234567 1 * 10 6 2 * 10 5 3 * 10 4 4 * 10 3 5 * 10 2 6 * 10 1 7 * 10 0 1,000,000200,00030,0004,000500607 1*10 6 + 2*10 5 + 3*10 4 + 4*10 3 + 5*10 2 + 6*10 1 + 7*10 0 = 1,234,567

51 Applying the Rule to Other Bases Let us see what happens with a base-5 number. Consider the representation of the number 13204 5 below: 6251252551 5454 5353 5252 5151 5050 13204 1 * 5 4 3 * 5 3 2 * 5 2 0 * 5 1 4 * 5 0 6253755004 13204 base-5 = 1*5 4 + 3*5 3 + 2*5 2 + 0*5 1 + 4*5 0 = 625 + 375 + 50 + 0 + 4 = 1,054 base-10

52 3214 base-5 = 3 x 5 3 + 2 x 5 2 + 1 x 5 1 + 4 x 5 0 70416 base-8 = 7 x 8 4 + 0 x 8 3 + 4 x 8 2 + 1 x 8 1 + 6 x 8 0 5A9D base-16 = 5 x 16 3 + A x 16 2 + 9 x 16 1 + D x 16 0 3204 base-4 = Incorrect base-4 number You should notice that once the first step is done – the expansion step – all that remains is simple arithmetic. Base-16 is a special case and requires an extra step, which will be shown on the next slide. 3204 base-4 is intentionally not expanded because it is not a valid number. Digit 4 is not possible in base-4. The only 4 digits allowed in base-4 are 0, 1, 2 & 3. Other Examples

53 Converting Base-16 to Base-10 1FA base-16 = ??? base-10 1 * 16 2 + F * 16 1 + A * 16 0 = 1 * 16 2 + 15 * 16 1 + 10 * 16 0 = 1 * 256 + 15 * 16 + 10 * 1 = 256 + 240 + 10 = 506 base-10 The Extra Step After the initial expansion, use this chart and convert all Base-16 letters to their Base-10 values. HexDec A10 B11 C12 D13 E14 F15

54 If number expansion makes sense to you, you can then take any number in any base and determine place holder values. You can also create a general method for expansion for a number with base-Q. 54321 base-Q = 5 x Q 4 + 4 x Q 3 + 3 x Q 2 + 2 x Q 1 + 1 x Q 0

55

56 Converting Base-2 to Base-10 Base 2 is another special case that has a simpler solution or numbers between 0 and 255 – which are the numbers currently used in IP Addresses. Binary (base-2) numbers are frequently written as a group of 8 bits. The 1 st 8 powers of 2 are shown below: To convert the binary number 01010101 to decimal look for the 1s. Add there place values and ignore the 0s. 01010101 base-2 = 64 + 16 + 4 + 1 = 85 base-10 1286432168421 01010101

57 Another way to look at it Suppose you have the binary number 11111110 One way to do this is by adding place values: 11111110 base-2 = 128 + 64 + 32 + 16 + 8 + 4 + 2 = 254 base-10 If you realize that binary 11111111 = decimal 255 you might find a shorter way to do this by subtracting place values of the 0s from 255. 11111110 base-2 = 255 - 1 = 254 base-10 1286432168421 11111110

58

59 The Division Method There are several different ways to convert a base-10 number to another base. One way involves dividing the base-10 number by the desired base value again and again until you get a quotient of 0. If you then take all of the remainders in reverse order, you will have your answer. This method works for any base. The example on the next slide demonstrates converting to base-2 for simplicity. Other examples will follow.

60 Converting Decimal to Binary Convert 201 10 to binary: 201 / 2 = 100 remainder 1 100 / 2 = 50 remainder 0 50 / 2 = 25 remainder 0 25 / 2 = 12 remainder 1 12 / 2 = 6 remainder 0 6 / 2 = 3 remainder 0 3 / 2 = 1 remainder 1 1 / 2 = 0 remainder 1 When the quotient is 0, take all the remainders in reverse order for your answer: 201 10 = 11001001 2

61 Converting Decimal to Base-5 Convert 1001 10 to Base 5: 1001 / 5 = 200 remainder 1 200 / 5 = 40 remainder 0 40 / 5 = 8 remainder 0 8 / 5 = 1 remainder 3 1 / 5 = 0 remainder 1 When the quotient is 0, take all the remainders in reverse order for your answer: 1001 10 = 13001 5

62 Converting Decimal to Octal Convert 2350 10 to Base 8: 2350 / 8 = 293 remainder 6 293 / 8 = 36 remainder 5 36 / 8 = 4 remainder 4 4 / 8 = 0 remainder 4 When the quotient is 0, take all the remainders in reverse order for your answer: 2350 10 = 4456 8

63 Convert 506 10 to hexadecimal: 31 r 10 16 )506 48 26 16 10 1 r 15 16 )31 16 15 0 r 1 16 )1 0 1 Converting Decimal to Hexadecimal Using the division method, when we take the remainders in reverse order we get 1(15)(10). We need to do one extra step to make this right.

64 Convert 506 10 to hexadecimal: 31 r 10 = A 16 )506 48 26 16 10 1 r 15 = F 16 )31 16 15 0 r 1 16 )1 0 1 Converting Decimal to Hexadecimal Extra Step When the 10 and 15 are converted to A and F, we can now properly find the answer of 1FA. HexDec A10 B11 C12 D13 E14 F15

65

66 Converting Any Base to Any Base You now already have what you need to convert from any base to any base. If you want to convert a number from base-4 to base-7, you would do the following 2 step process: 1 st Convert the base-4 number to a base-10 number using the expansion method. 2 nd Convert the base-10 number to a base-7 number using the division method. Converting between bases 2 and 16 can be done this way, but there is a simpler way to convert these 2 bases.

67 Look closely at this table.

68 Conversion between base-2 and base-16 requires that base-2 numbers are grouped in sets of four digits, and leading zeroes must be inserted. Each one of the sets of four base-2 digits is converted to one of sixteen base-16 single digits. You do need to know the first sixteen base-2 numbers, but after that it becomes a game of substitution. Converting Between Hexadecimal & Binary Base-16Base-2 00000 10001 20010 30011 40100 50101 60110 70111 81000 91001 A1010 B1011 C1100 D1101 E1110 F1111

69 Example 1 23CB7 base-16 = ??? base-2

70 Example 1 23CB7 base-16 = ??? base-2 Step 1 – Write the 4-bits (nibble) that corresponds to each hex digit. 2 3 C B 7 0010 0011 1100 1011 0111 Base-16Base-2 00000 10001 20010 30011 40100 50101 60110 70111 81000 91001 A1010 B1011 C1100 D1101 E1110 F1111

71 Example 1 23CB7 base-16 = ??? base-2 Step 1 – Write the 4-bits (nibble) that corresponds to each hex digit. 2 3 C B 7 0010 0011 1100 1011 0111 23CB7 16 = 00100011110010110111 base-2 Base-16Base-2 00000 10001 20010 30011 40100 50101 60110 70111 81000 91001 A1010 B1011 C1100 D1101 E1110 F1111

72 Example 2 10100111110010011110 base-2 = ?? base-16

73 Example 2 10100111110010011110 base-2 = ?? base-16 Step 1 – Separate bits into groups of 4: 1010 0111 1100 1001 1110

74 Example 2 10100111110010011110 base-2 = ?? base-16 Step 1 – Separate bits into groups of 4: 1010 0111 1100 1001 1110 Step 2 – Check the chart and write down the corresponding hex digit for each binary nibble. 1010 0111 1100 1001 1110 A 7 C 9 E Base-16Base-2 00000 10001 20010 30011 40100 50101 60110 70111 81000 91001 A1010 B1011 C1100 D1101 E1110 F1111

75 Example 2 10100111110010011110 base-2 = ?? base-16 Step 1 – Separate bits into groups of 4: 1010 0111 1100 1001 1110 Step 2 – Check the chart and write down the corresponding hex digit for each binary nibble. 1010 0111 1100 1001 1110 A 7 C 9 E 10100111110010011110 2 = A7C9E base-16 Base-16Base-2 00000 10001 20010 30011 40100 50101 60110 70111 81000 91001 A1010 B1011 C1100 D1101 E1110 F1111

76 Example 3 1101011110000000001 base-2 = ??? base-16

77 Example 3 1101011110000000001 base-2 = ??? base-16 Step 1 – Separate bits into groups of 4: Wrong way: 1101 0111 1000 0000 001 0 Right way: 0 110 1011 1100 0000 0001 In this example, the number of bits is not a multiple of 4. One or more extra 0s need to be added to the beginning of the binary number, NOT the end!

78 Example 3 1101011110000000001 base-2 = ??? base-16 Step 1 – Separate bits into groups of 4: 0110 1011 1100 0000 0001 Step 2 – Check the chart and write down the corresponding hex digit for each binary nibble. 0110 1011 1100 0000 0001 6 B C 0 1 Base-16Base-2 00000 10001 20010 30011 40100 50101 60110 70111 81000 91001 A1010 B1011 C1100 D1101 E1110 F1111

79 Example 3 1101011110000000001 base-2 = ??? base-16 Step 1 – Separate bits into groups of 4: 0110 1011 1100 0000 0001 Step 2 – Check the chart and write down the corresponding hex digit for each binary nibble. 0110 1011 1100 0000 0001 6 B C 0 1 1101011110000000001 2 = 6BC01 base-16 Base-16Base-2 00000 10001 20010 30011 40100 50101 60110 70111 81000 91001 A1010 B1011 C1100 D1101 E1110 F1111


Download ppt "String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,"

Similar presentations


Ads by Google