| midrange.com code scratchpad | 
				
					| 
							
								| Name:Routine to determine how much of a unicode string can fit in a double byte field | Scriptlanguage:Java | Tabwidth:4 | Date:06/25/2008 05:25:31 pm | IP:Logged |  | 
				
					| Description:The user was to lazy to give a description | 
				
					| Code: 
							
								
								
								| 
        private boolean isDoubleByte(char c) {
        return c > 255;
    }
     private int calculateUnicodeLengthForDbcsField(String input, int fieldSize) {
        int calculatedLength = 0;
        
        boolean doubleByteMode = false;
        
        int position;
        int realLength = 0;
        
        for (position=0;position<input.length();position++) {
        char c = input.charAt(position);
        realLength++;
        
        boolean doubleByteCharacter = isDoubleByte(c);
         if (!doubleByteMode  && doubleByteCharacter) {
            calculatedLength++;             doubleByteMode = true;
        } else if (doubleByteMode && !doubleByteCharacter) {
            calculatedLength++;             doubleByteMode = false;
        }
         calculatedLength++;
                 if (doubleByteCharacter) {
            calculatedLength++;
        }
        
                                if (position == input.length()-1 && doubleByteMode) {
            calculatedLength++;         }
        
                                                                int sizeDifference = calculatedLength - fieldSize;
        if (sizeDifference > 0) {
            realLength -= (sizeDifference > 1) ? 2 : 1;
            break;
        }
         }
        
        return realLength;
     }
  |  | 
				
					|  |