Re: Encoding problem
Re: Encoding problem
- Subject: Re: Encoding problem
- From: "Jonathan Fleming" <email@hidden>
- Date: Tue, 24 Jun 2003 02:38:05 +0100
From: Borut Bolhina <email@hidden>
To: email@hidden
Subject: Encoding problem
Date: Tue, 17 Jun 2003 12:39:27 +0200
Hello.
I encountered an encoding problem which is probably not WO related, but
more likely Apple's java implementation. The project I am working on
involves uploading XML via WOFileUpload. These XML files are constructed
by different third parties, so different encodings (UTF-8, ISO8859-2,
Cp1250) must be correctly handled.
--------------------------------------------------------------------------------------------------
Hi Borut,
When I had an encoding problem these guys helped me out, maybe stuff in
their code can help you too if you still need it
Jonathan :^)
From :
"Jonathan 'Wolf' Rentzsch" <email@hidden>
Subject :
Re: Encryption Code Problem
Date :
Tue, 3 Jun 2003 15:32:48 -0500
Here's my version. It's been through
extensive unit tests (grin).
// HexCoder.java
//
// Copyright (C) 2001 Petite Abeille. All Rights Reserved.
// This class is hereby released for all uses. No warranties whatsoever.
//
// by Petite Abeille <email@hidden>
//
// Sat Sep 8 2001: Created.
import org.apache.log4j.Logger;
public class HexCoder {
private static Logger log = Logger.getLogger( HexCoder.class );
private static final char[] Map = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f' };
public static String encode(byte[] someBytes) {
if (someBytes != null) {
StringBuffer aBuffer = new StringBuffer();
int length = someBytes.length;
for (int i = 0; i < length; i++) {
byte aByte = someBytes[i];
aBuffer.append(HexCoder.Map[((aByte >> 4) & 0x0f)]);
aByte &= 0x0f;
aBuffer.append(HexCoder.Map[aByte]);
}
return aBuffer.toString();
}
throw new IllegalArgumentException("HexCoder.decode: null bytes.");
}
public static byte[] decode(String aString) {
if (aString != null) {
int aLength = aString.length();
byte[] someBytes = new byte[aLength / 2];
int anIndex = 0;
int anotherIndex = aLength / 2;
char[] someChars = aString.toCharArray();
while (anIndex < aLength) {
char upperChar = someChars[anIndex];
char lowerChar = someChars[anIndex + 1];
someBytes[--anotherIndex] =
(byte) ((HexCoder.charToNibble(upperChar) << 4)
+ HexCoder.charToNibble(lowerChar));
anIndex += 2;
}
return someBytes;
}
throw new IllegalArgumentException("HexCoder.decode: null string.");
}
private static int charToNibble(char aChar) {
if ((aChar >= '0') && (aChar <= '9')) {
return aChar - '0';
}
if ((aChar >= 'A') && (aChar <= 'F')) {
return aChar - 'A' + 10;
}
if ((aChar >= 'a') && (aChar <= 'f')) {
return aChar - 'a' + 10;
}
return 0;
}
}
--------------------------------------------------------------------------------
From :
Georg Tuparev <email@hidden>
To :
"Jonathan 'Wolf' Rentzsch" <email@hidden>
Subject :
Re: Encryption Code Problem (REFERS TO THE ABOVE)
Date :
Mon, 16 Jun 2003 14:49:51 +0200
Well, decode() is a bit buggy.
My unit test does not pass:
public class HexCoderTestCase extends WOUTTestCase {
private static final byte[] bytes = {1,2,3,4};
protected void setUp() throws Exception {
super.setUp();
// your setup code here
}
protected void tearDown() throws Exception {
// your tearDown code here
super.tearDown();
}
public void testEncodingAndDecoding() {
String stringFromBytes = HexCoder.hexEncode(bytes);
byte[] revertBytes = HexCoder.hexDecode(stringFromBytes);
assertTrue(stringFromBytes.equals("01020304"));
assertTrue(HexCoder.hexEncode(bytes).equals(HexCoder.hexEncode(revertByt
es)));
}
}
Here is the fixed version:
public static byte[] hexDecode(String aString) throws
IllegalArgumentException {
if (aString != null) {
int aLength = aString.length();
byte[] someBytes = new byte[aLength / 2];
int anIndex = 0;
int anotherIndex = 0;
char[] someChars = aString.toCharArray();
while (anIndex < aLength) {
char upperChar = someChars[anIndex];
char lowerChar = someChars[anIndex + 1];
someBytes[anotherIndex++] =
(byte) ((HexCoder.charToNibble(upperChar) << 4)
+ HexCoder.charToNibble(lowerChar));
anIndex += 2;
}
return someBytes;
}
else throw new IllegalArgumentException("HexCoder.decode: null
string.");
}
--------------------------------------------------------------------------------
From :
email@hidden
Subject :
Re: Encryption Code Problem
Date :
Thu, 5 Jun 2003 14:58:44 +0100
This might be of help to you also, it was for me.
import java.net.URLEncoder;
import java.net.URLDecoder;
Hope it all helps
Jonathan ;^)
_________________________________________________________________
Find a cheaper internet access deal - choose one to suit you.
http://www.msn.co.uk/internetaccess
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.