Convert a String of Hex into ASCII in Java
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I hope this isn't too much of a stupid question, I have looked on 5 different pages of Google results but haven't been able to find anything on this.
What I need to do is convert a string that contains all Hex characters into ASCII for example
String fileName =
75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000
Every way I have seen makes it seems like you have to put it into an array first. Is there no way to loop through each two and convert them?
java string ascii hex
add a comment |
I hope this isn't too much of a stupid question, I have looked on 5 different pages of Google results but haven't been able to find anything on this.
What I need to do is convert a string that contains all Hex characters into ASCII for example
String fileName =
75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000
Every way I have seen makes it seems like you have to put it into an array first. Is there no way to loop through each two and convert them?
java string ascii hex
add a comment |
I hope this isn't too much of a stupid question, I have looked on 5 different pages of Google results but haven't been able to find anything on this.
What I need to do is convert a string that contains all Hex characters into ASCII for example
String fileName =
75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000
Every way I have seen makes it seems like you have to put it into an array first. Is there no way to loop through each two and convert them?
java string ascii hex
I hope this isn't too much of a stupid question, I have looked on 5 different pages of Google results but haven't been able to find anything on this.
What I need to do is convert a string that contains all Hex characters into ASCII for example
String fileName =
75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000
Every way I have seen makes it seems like you have to put it into an array first. Is there no way to loop through each two and convert them?
java string ascii hex
java string ascii hex
edited Jan 25 '15 at 4:07
Brayden
476
476
asked Jan 24 '11 at 18:30
JamesJames
178136
178136
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder:
public static void main(String args) {
String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
String str = hex.substring(i, i+2);
output.append((char)Integer.parseInt(str, 16));
}
System.out.println(output);
}
This gives a few lines starting with the following:
uTorrentCompletednfsuc_ost_by_mustangPendulum-9,000 Miles.mp3
Hmmm... :-)
87
This question wins the award of "Most Self-Incriminating Question of the Year".
– Cory Klein
Jan 24 '11 at 18:46
3
Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)
– James
Jan 24 '11 at 18:54
1
Thank you "berry120" that has helped me loads! :)
– James
Jan 24 '11 at 18:56
Given that the size of the string is known in advance, one might optimise theStringBuilder
to hold it all or even use just achar
. Only relevant if this will be run often, of course.
– entonio
Feb 22 '13 at 16:16
5
I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used likenew String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII)
- the BaseEncoding instance can be cached as it's immutable
– Matt
Mar 22 '13 at 11:27
add a comment |
Easiest way to do it with javax.xml.bind.DatatypeConverter
:
String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
byte s = DatatypeConverter.parseHexBinary(hex);
System.out.println(new String(s));
1
This solution gives the same results as the accepted answer.
– LeHill
Sep 8 '16 at 20:34
in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^
– Ted Delezene
Sep 24 '18 at 22:44
add a comment |
Check out Convert a string representation of a hex dump to a byte array using Java?
Disregarding encoding, etc. you can do new String (hexStringToByteArray("75546..."));
add a comment |
So as I understand it, you need to pull out successive pairs of hex digits, then decode that 2-digit hex number and take the corresponding char:
String s = "...";
StringBuilder sb = new StringBuilder(s.length() / 2);
for (int i = 0; i < s.length(); i+=2) {
String hex = "" + s.charAt(i) + s.charAt(i+1);
int ival = Integer.parseInt(hex, 16);
sb.append((char) ival);
}
String string = sb.toString();
add a comment |
String hexToAscii(String s) {
int n = s.length();
StringBuilder sb = new StringBuilder(n / 2);
for (int i = 0; i < n; i += 2) {
char a = s.charAt(i);
char b = s.charAt(i + 1);
sb.append((char) ((hexToInt(a) << 4) | hexToInt(b)));
}
return sb.toString();
}
private static int hexToInt(char ch) {
if ('a' <= ch && ch <= 'f') { return ch - 'a' + 10; }
if ('A' <= ch && ch <= 'F') { return ch - 'A' + 10; }
if ('0' <= ch && ch <= '9') { return ch - '0'; }
throw new IllegalArgumentException(String.valueOf(ch));
}
add a comment |
//%%%%%%%%%%%%%%%%%%%%%% HEX to ASCII %%%%%%%%%%%%%%%%%%%%%%
public String convertHexToString(String hex){
String ascii="";
String str;
// Convert hex string to "even" length
int rmd,length;
length=hex.length();
rmd =length % 2;
if(rmd==1)
hex = "0"+hex;
// split into two characters
for( int i=0; i<hex.length()-1; i+=2 ){
//split the hex into pairs
String pair = hex.substring(i, (i + 2));
//convert hex to decimal
int dec = Integer.parseInt(pair, 16);
str=CheckCode(dec);
ascii=ascii+" "+str;
}
return ascii;
}
public String CheckCode(int dec){
String str;
//convert the decimal to character
str = Character.toString((char) dec);
if(dec<32 || dec>126 && dec<161)
str="n/a";
return str;
}
add a comment |
To this case, I have a hexadecimal data format into an int array and I want to convert them on String.
int encodeHex = new int { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; // Hello encode
for (int i = 0; i < encodeHex.length; i++) {
System.out.print((char) (encodeHex[i]));
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4785654%2fconvert-a-string-of-hex-into-ascii-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder:
public static void main(String args) {
String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
String str = hex.substring(i, i+2);
output.append((char)Integer.parseInt(str, 16));
}
System.out.println(output);
}
This gives a few lines starting with the following:
uTorrentCompletednfsuc_ost_by_mustangPendulum-9,000 Miles.mp3
Hmmm... :-)
87
This question wins the award of "Most Self-Incriminating Question of the Year".
– Cory Klein
Jan 24 '11 at 18:46
3
Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)
– James
Jan 24 '11 at 18:54
1
Thank you "berry120" that has helped me loads! :)
– James
Jan 24 '11 at 18:56
Given that the size of the string is known in advance, one might optimise theStringBuilder
to hold it all or even use just achar
. Only relevant if this will be run often, of course.
– entonio
Feb 22 '13 at 16:16
5
I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used likenew String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII)
- the BaseEncoding instance can be cached as it's immutable
– Matt
Mar 22 '13 at 11:27
add a comment |
Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder:
public static void main(String args) {
String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
String str = hex.substring(i, i+2);
output.append((char)Integer.parseInt(str, 16));
}
System.out.println(output);
}
This gives a few lines starting with the following:
uTorrentCompletednfsuc_ost_by_mustangPendulum-9,000 Miles.mp3
Hmmm... :-)
87
This question wins the award of "Most Self-Incriminating Question of the Year".
– Cory Klein
Jan 24 '11 at 18:46
3
Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)
– James
Jan 24 '11 at 18:54
1
Thank you "berry120" that has helped me loads! :)
– James
Jan 24 '11 at 18:56
Given that the size of the string is known in advance, one might optimise theStringBuilder
to hold it all or even use just achar
. Only relevant if this will be run often, of course.
– entonio
Feb 22 '13 at 16:16
5
I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used likenew String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII)
- the BaseEncoding instance can be cached as it's immutable
– Matt
Mar 22 '13 at 11:27
add a comment |
Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder:
public static void main(String args) {
String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
String str = hex.substring(i, i+2);
output.append((char)Integer.parseInt(str, 16));
}
System.out.println(output);
}
This gives a few lines starting with the following:
uTorrentCompletednfsuc_ost_by_mustangPendulum-9,000 Miles.mp3
Hmmm... :-)
Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder:
public static void main(String args) {
String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
String str = hex.substring(i, i+2);
output.append((char)Integer.parseInt(str, 16));
}
System.out.println(output);
}
This gives a few lines starting with the following:
uTorrentCompletednfsuc_ost_by_mustangPendulum-9,000 Miles.mp3
Hmmm... :-)
edited May 8 '13 at 9:32
answered Jan 24 '11 at 18:40
Michael BerryMichael Berry
44.1k16115170
44.1k16115170
87
This question wins the award of "Most Self-Incriminating Question of the Year".
– Cory Klein
Jan 24 '11 at 18:46
3
Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)
– James
Jan 24 '11 at 18:54
1
Thank you "berry120" that has helped me loads! :)
– James
Jan 24 '11 at 18:56
Given that the size of the string is known in advance, one might optimise theStringBuilder
to hold it all or even use just achar
. Only relevant if this will be run often, of course.
– entonio
Feb 22 '13 at 16:16
5
I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used likenew String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII)
- the BaseEncoding instance can be cached as it's immutable
– Matt
Mar 22 '13 at 11:27
add a comment |
87
This question wins the award of "Most Self-Incriminating Question of the Year".
– Cory Klein
Jan 24 '11 at 18:46
3
Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)
– James
Jan 24 '11 at 18:54
1
Thank you "berry120" that has helped me loads! :)
– James
Jan 24 '11 at 18:56
Given that the size of the string is known in advance, one might optimise theStringBuilder
to hold it all or even use just achar
. Only relevant if this will be run often, of course.
– entonio
Feb 22 '13 at 16:16
5
I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used likenew String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII)
- the BaseEncoding instance can be cached as it's immutable
– Matt
Mar 22 '13 at 11:27
87
87
This question wins the award of "Most Self-Incriminating Question of the Year".
– Cory Klein
Jan 24 '11 at 18:46
This question wins the award of "Most Self-Incriminating Question of the Year".
– Cory Klein
Jan 24 '11 at 18:46
3
3
Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)
– James
Jan 24 '11 at 18:54
Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)
– James
Jan 24 '11 at 18:54
1
1
Thank you "berry120" that has helped me loads! :)
– James
Jan 24 '11 at 18:56
Thank you "berry120" that has helped me loads! :)
– James
Jan 24 '11 at 18:56
Given that the size of the string is known in advance, one might optimise the
StringBuilder
to hold it all or even use just a char
. Only relevant if this will be run often, of course.– entonio
Feb 22 '13 at 16:16
Given that the size of the string is known in advance, one might optimise the
StringBuilder
to hold it all or even use just a char
. Only relevant if this will be run often, of course.– entonio
Feb 22 '13 at 16:16
5
5
I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used like
new String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII)
- the BaseEncoding instance can be cached as it's immutable– Matt
Mar 22 '13 at 11:27
I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used like
new String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII)
- the BaseEncoding instance can be cached as it's immutable– Matt
Mar 22 '13 at 11:27
add a comment |
Easiest way to do it with javax.xml.bind.DatatypeConverter
:
String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
byte s = DatatypeConverter.parseHexBinary(hex);
System.out.println(new String(s));
1
This solution gives the same results as the accepted answer.
– LeHill
Sep 8 '16 at 20:34
in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^
– Ted Delezene
Sep 24 '18 at 22:44
add a comment |
Easiest way to do it with javax.xml.bind.DatatypeConverter
:
String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
byte s = DatatypeConverter.parseHexBinary(hex);
System.out.println(new String(s));
1
This solution gives the same results as the accepted answer.
– LeHill
Sep 8 '16 at 20:34
in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^
– Ted Delezene
Sep 24 '18 at 22:44
add a comment |
Easiest way to do it with javax.xml.bind.DatatypeConverter
:
String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
byte s = DatatypeConverter.parseHexBinary(hex);
System.out.println(new String(s));
Easiest way to do it with javax.xml.bind.DatatypeConverter
:
String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
byte s = DatatypeConverter.parseHexBinary(hex);
System.out.println(new String(s));
answered May 18 '15 at 8:55
Nikita KoksharovNikita Koksharov
6,2883754
6,2883754
1
This solution gives the same results as the accepted answer.
– LeHill
Sep 8 '16 at 20:34
in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^
– Ted Delezene
Sep 24 '18 at 22:44
add a comment |
1
This solution gives the same results as the accepted answer.
– LeHill
Sep 8 '16 at 20:34
in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^
– Ted Delezene
Sep 24 '18 at 22:44
1
1
This solution gives the same results as the accepted answer.
– LeHill
Sep 8 '16 at 20:34
This solution gives the same results as the accepted answer.
– LeHill
Sep 8 '16 at 20:34
in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^
– Ted Delezene
Sep 24 '18 at 22:44
in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^
– Ted Delezene
Sep 24 '18 at 22:44
add a comment |
Check out Convert a string representation of a hex dump to a byte array using Java?
Disregarding encoding, etc. you can do new String (hexStringToByteArray("75546..."));
add a comment |
Check out Convert a string representation of a hex dump to a byte array using Java?
Disregarding encoding, etc. you can do new String (hexStringToByteArray("75546..."));
add a comment |
Check out Convert a string representation of a hex dump to a byte array using Java?
Disregarding encoding, etc. you can do new String (hexStringToByteArray("75546..."));
Check out Convert a string representation of a hex dump to a byte array using Java?
Disregarding encoding, etc. you can do new String (hexStringToByteArray("75546..."));
edited May 23 '17 at 12:32
Community♦
11
11
answered Jan 24 '11 at 18:40
Andrew T FinnellAndrew T Finnell
12k22642
12k22642
add a comment |
add a comment |
So as I understand it, you need to pull out successive pairs of hex digits, then decode that 2-digit hex number and take the corresponding char:
String s = "...";
StringBuilder sb = new StringBuilder(s.length() / 2);
for (int i = 0; i < s.length(); i+=2) {
String hex = "" + s.charAt(i) + s.charAt(i+1);
int ival = Integer.parseInt(hex, 16);
sb.append((char) ival);
}
String string = sb.toString();
add a comment |
So as I understand it, you need to pull out successive pairs of hex digits, then decode that 2-digit hex number and take the corresponding char:
String s = "...";
StringBuilder sb = new StringBuilder(s.length() / 2);
for (int i = 0; i < s.length(); i+=2) {
String hex = "" + s.charAt(i) + s.charAt(i+1);
int ival = Integer.parseInt(hex, 16);
sb.append((char) ival);
}
String string = sb.toString();
add a comment |
So as I understand it, you need to pull out successive pairs of hex digits, then decode that 2-digit hex number and take the corresponding char:
String s = "...";
StringBuilder sb = new StringBuilder(s.length() / 2);
for (int i = 0; i < s.length(); i+=2) {
String hex = "" + s.charAt(i) + s.charAt(i+1);
int ival = Integer.parseInt(hex, 16);
sb.append((char) ival);
}
String string = sb.toString();
So as I understand it, you need to pull out successive pairs of hex digits, then decode that 2-digit hex number and take the corresponding char:
String s = "...";
StringBuilder sb = new StringBuilder(s.length() / 2);
for (int i = 0; i < s.length(); i+=2) {
String hex = "" + s.charAt(i) + s.charAt(i+1);
int ival = Integer.parseInt(hex, 16);
sb.append((char) ival);
}
String string = sb.toString();
answered Jan 24 '11 at 18:41
Neil CoffeyNeil Coffey
18.7k65476
18.7k65476
add a comment |
add a comment |
String hexToAscii(String s) {
int n = s.length();
StringBuilder sb = new StringBuilder(n / 2);
for (int i = 0; i < n; i += 2) {
char a = s.charAt(i);
char b = s.charAt(i + 1);
sb.append((char) ((hexToInt(a) << 4) | hexToInt(b)));
}
return sb.toString();
}
private static int hexToInt(char ch) {
if ('a' <= ch && ch <= 'f') { return ch - 'a' + 10; }
if ('A' <= ch && ch <= 'F') { return ch - 'A' + 10; }
if ('0' <= ch && ch <= '9') { return ch - '0'; }
throw new IllegalArgumentException(String.valueOf(ch));
}
add a comment |
String hexToAscii(String s) {
int n = s.length();
StringBuilder sb = new StringBuilder(n / 2);
for (int i = 0; i < n; i += 2) {
char a = s.charAt(i);
char b = s.charAt(i + 1);
sb.append((char) ((hexToInt(a) << 4) | hexToInt(b)));
}
return sb.toString();
}
private static int hexToInt(char ch) {
if ('a' <= ch && ch <= 'f') { return ch - 'a' + 10; }
if ('A' <= ch && ch <= 'F') { return ch - 'A' + 10; }
if ('0' <= ch && ch <= '9') { return ch - '0'; }
throw new IllegalArgumentException(String.valueOf(ch));
}
add a comment |
String hexToAscii(String s) {
int n = s.length();
StringBuilder sb = new StringBuilder(n / 2);
for (int i = 0; i < n; i += 2) {
char a = s.charAt(i);
char b = s.charAt(i + 1);
sb.append((char) ((hexToInt(a) << 4) | hexToInt(b)));
}
return sb.toString();
}
private static int hexToInt(char ch) {
if ('a' <= ch && ch <= 'f') { return ch - 'a' + 10; }
if ('A' <= ch && ch <= 'F') { return ch - 'A' + 10; }
if ('0' <= ch && ch <= '9') { return ch - '0'; }
throw new IllegalArgumentException(String.valueOf(ch));
}
String hexToAscii(String s) {
int n = s.length();
StringBuilder sb = new StringBuilder(n / 2);
for (int i = 0; i < n; i += 2) {
char a = s.charAt(i);
char b = s.charAt(i + 1);
sb.append((char) ((hexToInt(a) << 4) | hexToInt(b)));
}
return sb.toString();
}
private static int hexToInt(char ch) {
if ('a' <= ch && ch <= 'f') { return ch - 'a' + 10; }
if ('A' <= ch && ch <= 'F') { return ch - 'A' + 10; }
if ('0' <= ch && ch <= '9') { return ch - '0'; }
throw new IllegalArgumentException(String.valueOf(ch));
}
answered Jan 24 '11 at 18:42
Mike SamuelMike Samuel
94.5k23174215
94.5k23174215
add a comment |
add a comment |
//%%%%%%%%%%%%%%%%%%%%%% HEX to ASCII %%%%%%%%%%%%%%%%%%%%%%
public String convertHexToString(String hex){
String ascii="";
String str;
// Convert hex string to "even" length
int rmd,length;
length=hex.length();
rmd =length % 2;
if(rmd==1)
hex = "0"+hex;
// split into two characters
for( int i=0; i<hex.length()-1; i+=2 ){
//split the hex into pairs
String pair = hex.substring(i, (i + 2));
//convert hex to decimal
int dec = Integer.parseInt(pair, 16);
str=CheckCode(dec);
ascii=ascii+" "+str;
}
return ascii;
}
public String CheckCode(int dec){
String str;
//convert the decimal to character
str = Character.toString((char) dec);
if(dec<32 || dec>126 && dec<161)
str="n/a";
return str;
}
add a comment |
//%%%%%%%%%%%%%%%%%%%%%% HEX to ASCII %%%%%%%%%%%%%%%%%%%%%%
public String convertHexToString(String hex){
String ascii="";
String str;
// Convert hex string to "even" length
int rmd,length;
length=hex.length();
rmd =length % 2;
if(rmd==1)
hex = "0"+hex;
// split into two characters
for( int i=0; i<hex.length()-1; i+=2 ){
//split the hex into pairs
String pair = hex.substring(i, (i + 2));
//convert hex to decimal
int dec = Integer.parseInt(pair, 16);
str=CheckCode(dec);
ascii=ascii+" "+str;
}
return ascii;
}
public String CheckCode(int dec){
String str;
//convert the decimal to character
str = Character.toString((char) dec);
if(dec<32 || dec>126 && dec<161)
str="n/a";
return str;
}
add a comment |
//%%%%%%%%%%%%%%%%%%%%%% HEX to ASCII %%%%%%%%%%%%%%%%%%%%%%
public String convertHexToString(String hex){
String ascii="";
String str;
// Convert hex string to "even" length
int rmd,length;
length=hex.length();
rmd =length % 2;
if(rmd==1)
hex = "0"+hex;
// split into two characters
for( int i=0; i<hex.length()-1; i+=2 ){
//split the hex into pairs
String pair = hex.substring(i, (i + 2));
//convert hex to decimal
int dec = Integer.parseInt(pair, 16);
str=CheckCode(dec);
ascii=ascii+" "+str;
}
return ascii;
}
public String CheckCode(int dec){
String str;
//convert the decimal to character
str = Character.toString((char) dec);
if(dec<32 || dec>126 && dec<161)
str="n/a";
return str;
}
//%%%%%%%%%%%%%%%%%%%%%% HEX to ASCII %%%%%%%%%%%%%%%%%%%%%%
public String convertHexToString(String hex){
String ascii="";
String str;
// Convert hex string to "even" length
int rmd,length;
length=hex.length();
rmd =length % 2;
if(rmd==1)
hex = "0"+hex;
// split into two characters
for( int i=0; i<hex.length()-1; i+=2 ){
//split the hex into pairs
String pair = hex.substring(i, (i + 2));
//convert hex to decimal
int dec = Integer.parseInt(pair, 16);
str=CheckCode(dec);
ascii=ascii+" "+str;
}
return ascii;
}
public String CheckCode(int dec){
String str;
//convert the decimal to character
str = Character.toString((char) dec);
if(dec<32 || dec>126 && dec<161)
str="n/a";
return str;
}
edited Jul 14 '13 at 9:04
kleopatra
45.2k1676163
45.2k1676163
answered Jul 14 '13 at 8:46
maryanmaryan
211
211
add a comment |
add a comment |
To this case, I have a hexadecimal data format into an int array and I want to convert them on String.
int encodeHex = new int { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; // Hello encode
for (int i = 0; i < encodeHex.length; i++) {
System.out.print((char) (encodeHex[i]));
}
add a comment |
To this case, I have a hexadecimal data format into an int array and I want to convert them on String.
int encodeHex = new int { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; // Hello encode
for (int i = 0; i < encodeHex.length; i++) {
System.out.print((char) (encodeHex[i]));
}
add a comment |
To this case, I have a hexadecimal data format into an int array and I want to convert them on String.
int encodeHex = new int { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; // Hello encode
for (int i = 0; i < encodeHex.length; i++) {
System.out.print((char) (encodeHex[i]));
}
To this case, I have a hexadecimal data format into an int array and I want to convert them on String.
int encodeHex = new int { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; // Hello encode
for (int i = 0; i < encodeHex.length; i++) {
System.out.print((char) (encodeHex[i]));
}
answered Sep 3 '15 at 15:22
Pablo TisseraPablo Tissera
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4785654%2fconvert-a-string-of-hex-into-ascii-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown