2012-11-21 3 views
1

자바 서블릿에서 헥사 문자열을 디코드하려고합니다.헥사 문자열을 변환하는 방법?

String str = "choKKlate+%2F%2F1%3A%D9%81%DB%8C%D9%81%D8%A7%3E2%3A03008498499%2F%2F"; 

위의 URL에 다음 문자열은 URL 인코딩 된 유니 코드 우르두어 문자

فیفا 

내가 GET 방식으로 str을 문자열 변수를 보내고 우르두어 문자열에 해당

%D9%81%DB%8C%D9%81%D8%A7 

을 포함 . 서블릿에서 URLDecoder를 시도했습니다

URLDecoder.decode(str, "UTF-8"); 

그러나 이것은 urdu 문자 대신 물음표 ('????')를 반환합니다.

이 문제를 해결하는 방법 ??

+1

??? 인쇄를 시도한 결과 일 수 있습니다. 어쩌면 문자열 자체가 맞을 수도 있습니다. – Thilo

+0

디버거 또는 콘솔에서 "this provide"는 어디에 있습니까? –

+0

in tomcat console – Talha

답변

0

여기 undestand는 URL에서 일부 역할을 수행하므로 URL에이 hexa 문자열을 전달하면 안됩니다. 대신이 당신은

package com.encrypt; 

import java.util.Arrays; 

public class Base64 
{ 

public Base64() 
{ 
} 

public static final char[] encodeToChar(byte abyte0[], boolean flag) 
{ 
    int i = abyte0 == null ? 0 : abyte0.length; 
    if(i == 0) 
     return new char[0]; 
    int j = (i/3) * 3; 
    int k = (i - 1)/3 + 1 << 2; 
    int l = k + (flag ? (k - 1)/76 << 1 : 0); 
    char ac[] = new char[l]; 
    int i1 = 0; 
    int j1 = 0; 
    int l1 = 0; 
    do 
    { 
     if(i1 >= j) 
      break; 
     int i2 = (abyte0[i1++] & 0xff) << 16 | (abyte0[i1++] & 0xff) << 8 |  abyte0[i1++] & 0xff; 
     ac[j1++] = CA[i2 >>> 18 & 0x3f]; 
     ac[j1++] = CA[i2 >>> 12 & 0x3f]; 
     ac[j1++] = CA[i2 >>> 6 & 0x3f]; 
     ac[j1++] = CA[i2 & 0x3f]; 
     if(flag && ++l1 == 19 && j1 < l - 2) 
         { 
          ac[j1++] = '\r'; 
        ac[j1++] = '\n'; 
        l1 = 0; 
       } 
      } while(true); 
      i1 = i - j; 
      if(i1 > 0) 
       { 
        int k1 = (abyte0[j] & 0xff) << 10 | (i1 != 2 ? 0 : (abyte0[i - 1] & 0xff) << 2); 
     ac[l - 4] = CA[k1 >> 12]; 
     ac[l - 3] = CA[k1 >>> 6 & 0x3f]; 
     ac[l - 2] = i1 != 2 ? '=' : CA[k1 & 0x3f]; 
     ac[l - 1] = '='; 
    } 
    return ac; 
} 

public static final byte[] decode(char ac[]) 
{ 
    int i = ac == null ? 0 : ac.length; 
    if(i == 0) 
     return new byte[0]; 
    int j = 0; 
    for(int k = 0; k < i; k++) 
     if(IA[ac[k]] < 0) 
      j++; 

    if((i - j) % 4 != 0) 
     return null; 
    int l = 0; 
    int i1 = i; 
    do 
    { 
     if(i1 <= 1 || IA[ac[--i1]] > 0) 
      break; 
     if(ac[i1] == '=') 
      l++; 
    } while(true); 
    i1 = ((i - j) * 6 >> 3) - l; 
    byte abyte0[] = new byte[i1]; 
    int j1 = 0; 
    int k1 = 0; 
    do 
    { 
     if(k1 >= i1) 
      break; 
     int l1 = 0; 
     for(int i2 = 0; i2 < 4; i2++) 
     { 
      int j2 = IA[ac[j1++]]; 
      if(j2 >= 0) 
       l1 |= j2 << 18 - i2 * 6; 
      else 
       i2--; 
     } 

     abyte0[k1++] = (byte)(l1 >> 16); 
     if(k1 < i1) 
     { 
      abyte0[k1++] = (byte)(l1 >> 8); 
      if(k1 < i1) 
       abyte0[k1++] = (byte)l1; 
     } 
    } while(true); 
    return abyte0; 
} 

public static final byte[] decodeFast(char ac[]) 
{ 
    int i = ac.length; 
    if(i == 0) 
     return new byte[0]; 
    int j = 0; 
    int k; 
    for(k = i - 1; j < k && IA[ac[j]] < 0; j++); 
    for(; k > 0 && IA[ac[k]] < 0; k--); 
    byte byte0 = ac[k] != '=' ? 0 : ((byte)(ac[k - 1] != '=' ? 1 : 2)); 
    int l = (k - j) + 1; 
    int i1 = i <= 76 ? 0 : (ac[76] != '\r' ? 0 : l/78) << 1; 
    int j1 = ((l - i1) * 6 >> 3) - byte0; 
    byte abyte0[] = new byte[j1]; 
    int k1 = 0; 
    int l1 = 0; 
    int j2 = (j1/3) * 3; 
    do 
    { 
     if(k1 >= j2) 
      break; 
     int i3 = IA[ac[j++]] << 18 | IA[ac[j++]] << 12 | IA[ac[j++]] << 6 | IA[ac[j++]]; 
     abyte0[k1++] = (byte)(i3 >> 16); 
     abyte0[k1++] = (byte)(i3 >> 8); 
     abyte0[k1++] = (byte)i3; 
     if(i1 > 0 && ++l1 == 19) 
     { 
      j += 2; 
      l1 = 0; 
     } 
    } while(true); 
    if(k1 < j1) 
    { 
     int i2 = 0; 
     for(int k2 = 0; j <= k - byte0; k2++) 
      i2 |= IA[ac[j++]] << 18 - k2 * 6; 

     for(int l2 = 16; k1 < j1; l2 -= 8) 
      abyte0[k1++] = (byte)(i2 >> l2); 

    } 
    return abyte0; 
} 

public static final byte[] encodeToByte(byte abyte0[], boolean flag) 
{ 
    int i = abyte0 == null ? 0 : abyte0.length; 
    if(i == 0) 
     return new byte[0]; 
    int j = (i/3) * 3; 
    int k = (i - 1)/3 + 1 << 2; 
    int l = k + (flag ? (k - 1)/76 << 1 : 0); 
    byte abyte1[] = new byte[l]; 
    int i1 = 0; 
    int j1 = 0; 
    int l1 = 0; 
    do 
    { 
     if(i1 >= j) 
      break; 
     int i2 = (abyte0[i1++] & 0xff) << 16 | (abyte0[i1++] & 0xff) << 8 | abyte0[i1++] & 0xff; 
     abyte1[j1++] = (byte)CA[i2 >>> 18 & 0x3f]; 
     abyte1[j1++] = (byte)CA[i2 >>> 12 & 0x3f]; 
     abyte1[j1++] = (byte)CA[i2 >>> 6 & 0x3f]; 
     abyte1[j1++] = (byte)CA[i2 & 0x3f]; 
     if(flag && ++l1 == 19 && j1 < l - 2) 
     { 
      abyte1[j1++] = 13; 
      abyte1[j1++] = 10; 
      l1 = 0; 
     } 
    } while(true); 
    i1 = i - j; 
    if(i1 > 0) 
    { 
     int k1 = (abyte0[j] & 0xff) << 10 | (i1 != 2 ? 0 : (abyte0[i - 1] & 0xff) << 2); 
     abyte1[l - 4] = (byte)CA[k1 >> 12]; 
     abyte1[l - 3] = (byte)CA[k1 >>> 6 & 0x3f]; 
     abyte1[l - 2] = i1 != 2 ? 61 : (byte)CA[k1 & 0x3f]; 
     abyte1[l - 1] = 61; 
    } 
    return abyte1; 
} 

public static final byte[] decode(byte abyte0[]) 
{ 
    int i = abyte0.length; 
    int j = 0; 
    for(int k = 0; k < i; k++) 
     if(IA[abyte0[k] & 0xff] < 0) 
      j++; 

    if((i - j) % 4 != 0) 
     return null; 
    int l = 0; 
    int i1 = i; 
    do 
    { 
     if(i1 <= 1 || IA[abyte0[--i1] & 0xff] > 0) 
      break; 
     if(abyte0[i1] == 61) 
      l++; 
    } while(true); 
    i1 = ((i - j) * 6 >> 3) - l; 
    byte abyte1[] = new byte[i1]; 
    int j1 = 0; 
    int k1 = 0; 
    do 
    { 
     if(k1 >= i1) 
      break; 
     int l1 = 0; 
     for(int i2 = 0; i2 < 4; i2++) 
     { 
      int j2 = IA[abyte0[j1++] & 0xff]; 
      if(j2 >= 0) 
       l1 |= j2 << 18 - i2 * 6; 
      else 
       i2--; 
     } 

     abyte1[k1++] = (byte)(l1 >> 16); 
     if(k1 < i1) 
     { 
      abyte1[k1++] = (byte)(l1 >> 8); 
      if(k1 < i1) 
       abyte1[k1++] = (byte)l1; 
     } 
    } while(true); 
    return abyte1; 
} 

public static final byte[] decodeFast(byte abyte0[]) 
{ 
    int i = abyte0.length; 
    if(i == 0) 
     return new byte[0]; 
    int j = 0; 
    int k; 
    for(k = i - 1; j < k && IA[abyte0[j] & 0xff] < 0; j++); 
    for(; k > 0 && IA[abyte0[k] & 0xff] < 0; k--); 
    byte byte0 = abyte0[k] != 61 ? 0 : ((byte)(abyte0[k - 1] != 61 ? 1 : 2)); 
    int l = (k - j) + 1; 
    int i1 = i <= 76 ? 0 : (abyte0[76] != 13 ? 0 : l/78) << 1; 
    int j1 = ((l - i1) * 6 >> 3) - byte0; 
    byte abyte1[] = new byte[j1]; 
    int k1 = 0; 
    int l1 = 0; 
    int j2 = (j1/3) * 3; 
    do 
    { 
     if(k1 >= j2) 
      break; 
     int i3 = IA[abyte0[j++]] << 18 | IA[abyte0[j++]] << 12 | IA[abyte0[j++]] << 6 | IA[abyte0[j++]]; 
     abyte1[k1++] = (byte)(i3 >> 16); 
     abyte1[k1++] = (byte)(i3 >> 8); 
     abyte1[k1++] = (byte)i3; 
     if(i1 > 0 && ++l1 == 19) 
     { 
      j += 2; 
      l1 = 0; 
     } 
    } while(true); 
    if(k1 < j1) 
    { 
     int i2 = 0; 
     for(int k2 = 0; j <= k - byte0; k2++) 
      i2 |= IA[abyte0[j++]] << 18 - k2 * 6; 

     for(int l2 = 16; k1 < j1; l2 -= 8) 
      abyte1[k1++] = (byte)(i2 >> l2); 

    } 
    return abyte1; 
} 

public static final String encodeToString(byte abyte0[], boolean flag) 
{ 
    return new String(encodeToChar(abyte0, flag)); 
} 

public static final byte[] decode(String s) 
{ 
    int i = s == null ? 0 : s.length(); 
    if(i == 0) 
     return new byte[0]; 
    int j = 0; 
    for(int k = 0; k < i; k++) 
     if(IA[s.charAt(k)] < 0) 
      j++; 

    if((i - j) % 4 != 0) 
     return null; 
    int l = 0; 
    int i1 = i; 
    do 
    { 
     if(i1 <= 1 || IA[s.charAt(--i1)] > 0) 
      break; 
     if(s.charAt(i1) == '=') 
      l++; 
    } while(true); 
    i1 = ((i - j) * 6 >> 3) - l; 
    byte abyte0[] = new byte[i1]; 
    int j1 = 0; 
    int k1 = 0; 
    do 
    { 
     if(k1 >= i1) 
      break; 
     int l1 = 0; 
     for(int i2 = 0; i2 < 4; i2++) 
     { 
      int j2 = IA[s.charAt(j1++)]; 
      if(j2 >= 0) 
       l1 |= j2 << 18 - i2 * 6; 
      else 
       i2--; 
     } 

     abyte0[k1++] = (byte)(l1 >> 16); 
     if(k1 < i1) 
     { 
      abyte0[k1++] = (byte)(l1 >> 8); 
      if(k1 < i1) 
       abyte0[k1++] = (byte)l1; 
     } 
    } while(true); 
    return abyte0; 
} 

public static final byte[] decodeFast(String s) 
{ 
    int i = s.length(); 
    if(i == 0) 
     return new byte[0]; 
    int j = 0; 
    int k; 
    for(k = i - 1; j < k && IA[s.charAt(j) & 0xff] < 0; j++); 
    for(; k > 0 && IA[s.charAt(k) & 0xff] < 0; k--); 
    byte byte0 = s.charAt(k) != '=' ? 0 : ((byte)(s.charAt(k - 1) != '=' ? 1 : 2)); 
    int l = (k - j) + 1; 
    int i1 = i <= 76 ? 0 : (s.charAt(76) != '\r' ? 0 : l/78) << 1; 
    int j1 = ((l - i1) * 6 >> 3) - byte0; 
    byte abyte0[] = new byte[j1]; 
    int k1 = 0; 
    int l1 = 0; 
    int j2 = (j1/3) * 3; 
    do 
    { 
     if(k1 >= j2) 
      break; 
     int i3 = IA[s.charAt(j++)] << 18 | IA[s.charAt(j++)] << 12 | IA[s.charAt(j++)] << 6 | IA[s.charAt(j++)]; 
     abyte0[k1++] = (byte)(i3 >> 16); 
     abyte0[k1++] = (byte)(i3 >> 8); 
     abyte0[k1++] = (byte)i3; 
     if(i1 > 0 && ++l1 == 19) 
     { 
      j += 2; 
      l1 = 0; 
     } 
    } while(true); 
    if(k1 < j1) 
    { 
     int i2 = 0; 
     for(int k2 = 0; j <= k - byte0; k2++) 
      i2 |= IA[s.charAt(j++)] << 18 - k2 * 6; 

     for(int l2 = 16; k1 < j1; l2 -= 8) 
      abyte0[k1++] = (byte)(i2 >> l2); 

    } 
    return abyte0; 
} 

private static final char CA[]; 
private static final int IA[]; 

static 
{ 
    CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/".toCharArray(); 
    IA = new int[256]; 
    Arrays.fill(IA, -1); 
    int i = 0; 
    for(int j = CA.length; i < j; i++) 
     IA[CA[i]] = i; 

    IA[61] = 0; 
} 
} 

이제 우리는 인코딩 코드 및 디코드를 작성합니다 인코딩하고 우리는 하나 개의 기본 64 클래스 가진 코드를 만들 전직의 캐릭터에게

를 디코딩 base64로의 algorithim 또는 사용자 정의 너 한테을 사용할 수 있습니다 문자열

package com.encrypt; 
import java.util.*; 

    public class EnMain { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    long l = (new Date()).getTime(); 
    System.out.println(" timestamp "+l); 
    String userid ="hello------"+l; 
    Base64 base = new Base64(); 
    System.out.println(userid.getBytes()); 
    String encryptuserid = base.encodeToString(userid.getBytes(), false); 
    System.out.println("encrypt values "+encryptuserid); 
    byte[] b = base.decode("Y29nbm9zLS0tLS0xMzUxNDMzMjk2Mzk3"); 
    StringBuffer sb = new StringBuffer(); 
    for(int i =0;i<b.length;i++){ 
     byte bx = b[i]; 
     System.out.print((char)b[i]); 
     sb.append((char)b[i]); 
     //String s = ; 
    }System.out.println(); 
    System.out.println(sb); 


    String[] as = sb.toString().split("-----"); 
    long l1 = Long.parseLong(as[1]); 
    System.out.println("diff :"+(l1-l)); 


} 

    } 

여기서 "hello ........."+ l을 문자열로 전달하고 base64 algo를 사용하여 인코딩 한 다음 검색합니다.

+0

이미 시도했습니다 URLDecoder.decode (str, "UTF-8"); 은 모든 %를 생략합니다. 유일한 문제는 16 진수 문자 해독입니다. – Talha

+0

문제는 16 진수 문자가 아닙니다. 우르두 대신 다른 16 진수 문자열로 시도해 보셨습니까? 작동해야합니다. urdu에 대한 java 유니 코드는 \ u0622이며 디코딩하면?가 표시됩니다. – Pri

+0

System.out.println ("فیفا"); 나는 java와 servlet에서이 라인을 사용한다. 자바 출력 괜찮습니다. 서블릿에는 '????'이 표시됩니다. 나는 바람둥이 서버가 유니 코드 언어를 지원하지 않는다고 생각한다. – Talha

관련 문제