String makeString(int[] codes, boolean debug) throws UnsupportedEncodingException { byte[] b = new byte[codes.length]; for (int i=0; i<codes.length; i++) b[i] = (byte) codes[i]; if (debug) System.out.println(" orgBytes= "+showBytes(b)); String s = new String(b, "UTF-8"); if (debug) System.out.println("convBytes= "+showString( s)); return s; }
System.out.println("char values= "+showString(helloGreek)); System.out.println(" UTF-8= "+showBytes(helloGreek.getBytes("UTF-8"))); System.out.println(" UTF-16= "+showBytes(helloGreek.getBytes("UTF-16"))); System.out.println(" default= "+showBytes(helloGreek.getBytes())); System.out.println("norm values= "+showString(s2)); System.out.println(" UTF-8= "+showBytes(s2.getBytes("UTF-8"))); System.out.println(" UTF-16= "+showBytes(s2.getBytes("UTF-16")));
public void makeNetCDF() throws IOException, InvalidRangeException { String helloGreek = makeString(helloGreekCode, true); helloGreek = ""; //Normalizer.normalize(helloGreek, Normalizer.Form.NFC); System.out.println("normalized= "+showString(helloGreek)); String filename = "C:/data/unicode/helloNorm.nc"; NetcdfFileWriter ncfile = NetcdfFileWriter.createNew(filename, true); ucar.nc2.Dimension dim = ncfile.addDimension(helloGreek, 20); ncfile.addVariable(helloGreek, DataType.CHAR, helloGreek); ncfile.addVariableAttribute(helloGreek, "units", helloGreek); ncfile.create(); ArrayChar.D1 data = new ArrayChar.D1(dim.getLength()); data.setString(helloGreek); ncfile.write(helloGreek, data); ncfile.close(); NetcdfFile nc = NetcdfFile.open(filename); Variable v = ncfile.findVariable(helloGreek); assert v != null; assert v.getShortName().equals(helloGreek); Attribute att = v.findAttribute("units"); assert att != null; assert att.isString(); assert(helloGreek.equals(att.getStringValue())); nc.close(); }