Domanda

I have an array of bytes that contains a sentence. I need to convert the lowercase letters on this sentence into uppercase letters. Here is the function that I did:

 public void CharUpperBuffAJava(byte[] word) {
     for (int i = 0; i < word.length; i++) {
        if (!Character.isUpperCase(word[i]) && Character.isLetter(word[i])) {
            word[i] -= 32;
        }
     }
    return cchLength;
 }

It will work fine with sentences like: "a glass of water". The problem is it must work with all ANSI characters, which includes "ç,á,é,í,ó,ú" and so on. The method Character.isLetter doesn't work with these letters and, therefore, they are not converted into uppercase.

Do you know how can I identify these ANSI characters as a letter in Java?

EDIT

If someone wants to know, I did method again after the answers and now it looks like this:

public static int CharUpperBuffAJava(byte[] lpsz, int cchLength) {
    String value;
    try {
        value = new String(lpsz, 0, cchLength, "Windows-1252");
        String upperCase = value.toUpperCase();
        byte[] bytes = upperCase.getBytes();
        for (int i = 0; i < cchLength; i++) {
            lpsz[i] = bytes[i];
        }
        return cchLength;
    } catch (UnsupportedEncodingException e) {
        return 0;
    }
}
È stato utile?

Soluzione

You need to "decode" the byte[] into a character string. There are several APIs to do this, but you must specify the character encoding that is use for the bytes. The overloaded versions that don't use an encoding will give different results on different machines, because they use the platform default.

For example, if you determine that the bytes were encoded with Windows-1252 (sometimes referred to as ANSI).

String s = new String(bytes, "Windows-1252"); 
String upper = s.toUpperCase();

Altri suggerimenti

Convert the byte array into a string, supporting the encoding. Then call toUpperCase(). Then, you can call getBytes() on the string if you need it as a byte array after capitalizing.

Can't you simply use:

String s = new String(bytes, "cp1252");
String upper = s.toUpperCase(someLocale);

Wouldn't changing the character set do the trick before conversion? The internal conversion logic of Java might work fine. Something like http://www.exampledepot.com/egs/java.nio.charset/ConvertChar.html, but use ASCII as the target character set.

I am looking at this table:

http://slayeroffice.com/tools/ascii/

But anything > 227 appears to be a letter, but to make it upper case you would subtract 27 from the ASCII value.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top