?
Solved

URLEncoder

Posted on 2005-03-09
5
Medium Priority
?
312 Views
Last Modified: 2012-05-05
Hi All,
     I want to convert a hex string to byte and then URLEncode before sending via HTTP Parameter.
     This is snippet of my code,  
             
                String hex = "06050415810000";
              byte[] bts = new byte[hex.length() / 2];
              for (int i = 0; i < bts.length; i++) {
                      bts[i] = (byte) Integer.parseInt(hex.substring(2*i, 2*i+2), 16);
              }
              String string = new String(bts);

                System.out.println(hex );
                System.out.println(URLEncoder.encode(string1,"UTF-8"));

This is my result, which clear seem like the URLEncoding part doesn't match. I have very sure the hex to byte conversion works as expected.

06050415810000
%06%05%04%15%EF%BF%BD%00%00

Please help.


                             

0
Comment
Question by:cHEo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 9

Expert Comment

by:riaancornelius
ID: 13503292
>> System.out.println(URLEncoder.encode(string1,"UTF-8"));

What is string1? shouldn't it be:
System.out.println(URLEncoder.encode(string,"UTF-8"));
0
 

Author Comment

by:cHEo
ID: 13503547
Sorry, typo error,
it should be
    System.out.println(URLEncoder.encode(string,"UTF-8"));
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 600 total points
ID: 13505567
This is a somewhat dangerous strategy as changing between various character encodings can have unexpected effects. It would be much safer (and easier) to send the bytes as hex codes in their original format:

String hex = "06050415810000";

String param1 =  "param1=" + URLEncoder.encode(hex,"UTF-8");

0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question