60 likes | 75 Views
Do we need to convert integers (or longs, or floats) to strings ??. YES ---. Strings. Whenever we store numeric values to an ASCII file, we MUST convert to a string. How do we convert ???. Much like we did when converted from decimal to binary.
E N D
Do we need to convert integers (or longs, or floats) to strings ?? YES --- Strings Whenever we store numeric values to an ASCII file, we MUST convert to a string How do we convert ??? Much like we did when converted from decimal to binary. Consider the conversion needed for 8710 to binary: 2 87 1 1 0 1 0 1 1 1 2 43 1 • Remember the conversion: • Divide by 2 (for binary) • Collect from bottom to top 2 21 1 2 10 0 2 5 1 What does this have to do with converting from integers to string ??? 2 2 0 2 1 1
Instead of dividing by 2 and keeping track of the remainder, we divide by 10 and keep track of the remainder Strings Consider the integer: 5409 “5409” 10 5409 9 Convert to: ‘9’ 10 540 0 ‘0’ Convert to: The only difference is that we must first convert the digit to a character 10 54 4 ‘4’ Convert to: 10 5 5 ‘5’ Convert to: 0 Collect from bottom: How do we convert to a character ??? Simple: Add 48 (the character ‘0’) to the remainder
Consider the following C Code: void main() { intdecimal = 5409, // The integer we wish to convert idx = 0; // The character array index charbin[10]; // the character array for out string while (decimal > 0) // Continue until the quotient is 0 (zero) { bin[idx++] = decimal % 10 +'0'; // store the remainder decimal = decimal / 10; } // get the new quotient bin[idx] = '\0'; } // set in the null character & decrement the index Strings This would execute as: decimal idx decimal > 0 decimal %10 + 48 decimal /10 bin 5409 % 10 +48 =57 5409 0 TRUE 5409/10 =540 “9” 540 1 TRUE 540 % 10 + 48 = 48 540/10 =54 “90” 54 2 TRUE 54 % 10 + 48 = 52 54/10 =5 “904” 5 3 TRUE 5 % 10 + 48 = 53 5/10 =0 “9045” 0 4 FALSE and: bin[idx] = '\0' “9045\0”
NOTE, however, that our string is: “9045\0” --- We must reverse the order --- Strings If we were to ADD the c code: intoffset = 0; // we will start with the first character chartemp; // for temporary storage idx--; // DON’T move ‘\0’ (idx now contains 3) while (idx > offset) { temp = bin[idx]; // store uppermost non-swapped character bin[idx--] = bin[offset]; // move lower char. to upper & decrement bin[offset++] = temp; } } // move in the old uppermost character Following the loop: idx offset bin idx>offset temp bin[idx--] bin[offset++] bin 3 0 “9045\0” TRUE ‘5’ ‘9’ ‘5’ “5049\0” 2 1 “5049\0” TRUE ‘4’ ‘0’ ‘4’ “5409\0” “5409\0” FALSE *** We are out of the loop 1 2 And the string is in the correct order
Must we write this code each time we write numeric values to an ASCII file ??? Strings • YES and No ---- • Each time we write numeric data to an ASCII, we MUST convert. • Because the conversions are so common, there are readily available library routines in <stdlib.h> Function Name Meaning Action itoa integer toalpha Convert: int to string ltoa longtoalpha Convert: long to string ftoa floattoalpha Convert: float to string Check Your Manuals: The parameters passed are different
This Concludes The Slides for this Section Choose an Option: Repeat Slides for this Section Go To Slide Index For Chapter 5 Go To Slide Index For Chapter 6 Go To Slide Index For Textbook Go To Home Page