benz
benz

Reputation: 4629

String UTF-8 conversion Issue

I am encountering an issue in String conversion to UTF-8. Basically, i have developed a DayDescriptionEnum which will actually, get the day of week and return description accordingly. Kindly see the below code.

public enum DayDescriptionEnum {

    SUNDAY(1, "Sunday", "\\u0627\\u0644\\u0623\\u062d\\u062f"),
    MONDAY(2, "Monday", "\\u0627\\u0644\\u0625\\u062b\\u0646\\u064a\\u0646"),
    TUESDAY(3, "Tuesday", "\\u0627\\u0644\\u062b\\u0644\\u0627\\u062b\\u0627\\u0621"),
    WEDNESDAY(4, "Wednesday", "\\u0627\\u0644\\u0623\\u0631\\u0628\\u0639\\u0627\\u0621"),
    THURSDAY(5, "Thursday", "\\u0627\\u0644\\u062e\\u0645\\u064a\\u0633"),
    FRIDAY(6, "Friday", "\\u0627\\u0644\\u062c\\u0645\\u0639\\u0629"),
    SATURDAY(7, "Saturday", "\\u0627\\u0644\\u0633\\u0628\\u062a");

    private long dayofWeek;
    private String dayDescriptionEnglish;
    private String dayDescriptionArabic;

    private DayDescriptionEnum(long dayofWeek, String dayDescriptionEnglish, String dayDescriptionArabic) {
        this.dayofWeek = dayofWeek;
        this.dayDescriptionEnglish = dayDescriptionEnglish;
        this.dayDescriptionArabic = dayDescriptionArabic;
    }

    public long getDayofWeek() {
        return dayofWeek;
    }

    public String getDayDescriptionEnglish() {
        return dayDescriptionEnglish;
    }

    public String getDayDescriptionArabic() {

        return dayDescriptionArabic;
    }
}


  public static DayDescriptionEnum getDescriptionOfDay(long dayOfWeek){
        DayDescriptionEnum dayDescriptionEnum = DayDescriptionEnum.SUNDAY;

        if(PermitReportConstants.SUNDAY == dayOfWeek){
            dayDescriptionEnum =  DayDescriptionEnum.SUNDAY;
        }

        if(PermitReportConstants.MONDAY == dayOfWeek){
            dayDescriptionEnum =  DayDescriptionEnum.MONDAY;
        }

        if(PermitReportConstants.TUESDAY == dayOfWeek){
            dayDescriptionEnum =  DayDescriptionEnum.TUESDAY;
        }

        if(PermitReportConstants.WEDNESDAY == dayOfWeek){
            dayDescriptionEnum =  DayDescriptionEnum.WEDNESDAY;
        }

        if(PermitReportConstants.THURSDAY == dayOfWeek){
            dayDescriptionEnum =  DayDescriptionEnum.THURSDAY;
        }

        if(PermitReportConstants.FRIDAY == dayOfWeek){
            dayDescriptionEnum =  DayDescriptionEnum.FRIDAY;
        }

        if(PermitReportConstants.SATURDAY == dayOfWeek){
            dayDescriptionEnum =  DayDescriptionEnum.SATURDAY;
        }

        return dayDescriptionEnum;
    }

The above method takes day of week and returns the Enum accordingly. Now when i am displaying this string, its showing description as "\u0627\u0644\u0623\u062d\u062f" same. I researched and realized i need to convert the String into UTF-8 format. So i put the code below as

byte[] utf8Bytes = null ;
        try {
             utf8Bytes = dayDescriptionEnum.getDayDescriptionArabic().getBytes("UTF-8");
            String arabicDescription = new String(utf8Bytes,"UTF-8");
            readyToPrintDeliver.setContractDayDescAr(arabicDescription);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

Still no effect. My question is, i had already converted the string to unicode. Where am i making a mistake? Kindly help.

Thanks.

Upvotes: 0

Views: 289

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533880

Most IDEs support UTF-8 encoding. While using unicode is more portable, being able to type the intended characters natively in your code might be more natural.

public enum DayDescriptionEnum {

    SUNDAY(1, "Sunday", "الأحد"),
    MONDAY(2, "Monday", "الإثنين"),
    TUESDAY(3, "Tuesday", "الثلاثاء"),
    WEDNESDAY(4, "Wednesday", "الأربعاء"),
    THURSDAY(5, "Thursday", "الخميس"),
    FRIDAY(6, "Friday", "الجمعة"),
    SATURDAY(7, "Saturday", "السبت");

This compiles & runs fine, and if you can read Arabic, it might be easier to work with and check.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1504032

UTF-8 is a red herring here. You don't need to convert to UTF-8 and back at all - you just need to fix your strings so that you're not escaping the backslash:

SUNDAY(1, "Sunday", "\u0627\u0644\u0623\u062d\u062f"),
// etc

Within a string literal, \\ is used for the backslash character - so in your original code, the strings genuinely contained backslashes, u characters, and hex digits... the only thing that was an escape sequence was the \\ repeatedly. You don't want that - you just want the Unicode characters, using \uXXXX as a Unicode escape sequence.

With the code above, you don't need to convert at all - just use:

readyToPrintDeliver.setContractDayDescAr(dayDescriptionEnum.getDayDescriptionArabic());

Upvotes: 7

Related Questions