Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

URLEncoder

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
cHEo
Asked:
cHEo
1 Solution
 
riaancorneliusCommented:
>> System.out.println(URLEncoder.encode(string1,"UTF-8"));

What is string1? shouldn't it be:
System.out.println(URLEncoder.encode(string,"UTF-8"));
0
 
cHEoAuthor Commented:
Sorry, typo error,
it should be
    System.out.println(URLEncoder.encode(string,"UTF-8"));
0
 
CEHJCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now