Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

How can I convert a text file or a String from UTF-8 format to ASCII?

For example, the following string "ZÜRICH-ZOLLI" once outputted into a text file. If the file has the format UTF-8 is has 13 characters but when the format is ASCII the number of characters is 12.

The endpoint where the file is being sent only accepts the ASCII-encoded version, and fails on the UTF-8 encoded string salesforce normally produces.

share|improve this question

3 Answers 3

UTF-8 is a superset of ASCII. The character codes 0-127 (i.e. the ASCII characters) are directly mapped to the binary values 0-127 so if your UTF-8 string only consists of ASCII characters it is already in ASCII format.

Beyond that all you can really do is strip out the non-ascii characters from your string or replace them with some ASCII character. This regex should do it for you.

if(!str.isAsciiPrintable) str = str.replaceall('[^\\x00-\\x7F]', '');
share|improve this answer
data = EncodingUtil.urlEncode(data, 'ASCII');
data = EncodingUtil.urlDecode(data, 'ASCII');

(What if there is a byte order marker?)

share|improve this answer
2  
this worked for me! and I was able to strip the remnant byte order mark as well –  Wesley Jan 18 '13 at 12:48
    
That's interesting. I guess this would depend on whether you want to strip out the characters or replace with a '?'. Though of course you could easily modify replaceall to use a '?'. –  Greg Grinberg Jan 18 '13 at 19:11
1  
the ?s are a bummer :( off-platform, iconv //TRANSLIT could at least represent Ü as U –  bigassforce Jan 18 '13 at 20:43
  //parsing csv blob file to proper format
  string csvAsString = ApexController.blobToString(csvblobfile.body,'ISO 8859-2');
   //parse CSV
  List<List<String>> parsedCSVRecords = ApexController.parseCSV(csvAsString,false);


  //Add below static methods in your controller

   public Class ApexController{

   public static List<List<String>> parseCSV(String contents,Boolean skipHeaders) {

    List<List<String>> allFields = new List<List<String>>();

    // replace instances where a double quote begins a field containing a comma
    // in this case you get a double quote followed by a doubled double quote
    // do this for beginning and end of a field
    contents = contents.replaceAll(',"""',',"DBLQT').replaceall('""",','DBLQT",');
    // now replace all remaining double quotes - we do this so that we can reconstruct
    // fields with commas inside assuming they begin and end with a double quote
    contents = contents.replaceAll('""','DBLQT');
    // we are not attempting to handle fields with a newline inside of them
    // so, split on newline to get the spreadsheet rows
    List<String> lines = new List<String>();
    try {
        lines = contents.split('\n');
    } catch (System.ListException e) {
        System.debug('Limits exceeded?' + e.getMessage());
    }
    system.debug('Line num i s'+lines.size());
    Integer num = 0;
    for(String line : lines) {
        // check for blank CSV lines (only commas)
        if (line.replaceAll(',','').trim().length() == 0) break;

        List<String> fields = line.split(',');  
        List<String> cleanFields = new List<String>();
        String compositeField;
        Boolean makeCompositeField = false;
        for(String field : fields) {
            if (field.startsWith('"') && field.endsWith('"')) {
                cleanFields.add(field.replaceAll('DBLQT','"'));
            } else if (field.startsWith('"')) {
                makeCompositeField = true;
                compositeField = field;
            } else if (field.endsWith('"')) {
                compositeField += ',' + field;
                cleanFields.add(compositeField.replaceAll('DBLQT','"'));
                makeCompositeField = false;
            } else if (makeCompositeField) {
                compositeField +=  ',' + field;
            } else {
                cleanFields.add(field.replaceAll('DBLQT','"'));
            }
        }

        allFields.add(cleanFields);
    }
    if (skipHeaders) allFields.remove(0);
    return allFields;       
} 


  public static String blobToString(Blob input, String inCharset){
     String hex = EncodingUtil.convertToHex(input);
     System.assertEquals(0, hex.length() & 1);
    final Integer bytesCount = hex.length() >> 1;
    String[] bytes = new String[bytesCount];
    for(Integer i = 0; i < bytesCount; ++i)
    bytes[i] =  hex.mid(i << 1, 2);
    return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset);
 }
share|improve this answer
2  
Can you please explain what your code is suppose to do ? –  SF_user Jun 8 at 7:29
    
This code will parse non utf csv files . blobToString function can convert any input blob to specific string format like UTF-8,ISO 8859-2 etc.. –  Debadyuti Sil Jun 17 at 5:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.