NumberFormatException - with String of an Hex Value
I got a stream of byte this stream is a bytearray ( byte stream ) now i want parse this byte first to an hex value, e.g. 80 = "0x50" then transform this to an int
But for some values like 0xB8 i got an `java.lang.NumberFormatException: For input string: "0xB8"
How could i manage this exception? Maybe with an other type of parse or different type data?
byte k = stream[i];
int b = 0;
try {
b = Integer.parseInt(String.format("0x%02X",k),16);
} catch (NumberFormatException e) {
e.printStackTrace();
}
java exception
add a comment |
I got a stream of byte this stream is a bytearray ( byte stream ) now i want parse this byte first to an hex value, e.g. 80 = "0x50" then transform this to an int
But for some values like 0xB8 i got an `java.lang.NumberFormatException: For input string: "0xB8"
How could i manage this exception? Maybe with an other type of parse or different type data?
byte k = stream[i];
int b = 0;
try {
b = Integer.parseInt(String.format("0x%02X",k),16);
} catch (NumberFormatException e) {
e.printStackTrace();
}
java exception
Check this stackoverflow.com/questions/11377944/…
– mettleap
Nov 15 '18 at 17:31
add a comment |
I got a stream of byte this stream is a bytearray ( byte stream ) now i want parse this byte first to an hex value, e.g. 80 = "0x50" then transform this to an int
But for some values like 0xB8 i got an `java.lang.NumberFormatException: For input string: "0xB8"
How could i manage this exception? Maybe with an other type of parse or different type data?
byte k = stream[i];
int b = 0;
try {
b = Integer.parseInt(String.format("0x%02X",k),16);
} catch (NumberFormatException e) {
e.printStackTrace();
}
java exception
I got a stream of byte this stream is a bytearray ( byte stream ) now i want parse this byte first to an hex value, e.g. 80 = "0x50" then transform this to an int
But for some values like 0xB8 i got an `java.lang.NumberFormatException: For input string: "0xB8"
How could i manage this exception? Maybe with an other type of parse or different type data?
byte k = stream[i];
int b = 0;
try {
b = Integer.parseInt(String.format("0x%02X",k),16);
} catch (NumberFormatException e) {
e.printStackTrace();
}
java exception
java exception
edited Nov 16 '18 at 7:45
Gianluca Benucci
asked Nov 15 '18 at 17:21
Gianluca BenucciGianluca Benucci
779
779
Check this stackoverflow.com/questions/11377944/…
– mettleap
Nov 15 '18 at 17:31
add a comment |
Check this stackoverflow.com/questions/11377944/…
– mettleap
Nov 15 '18 at 17:31
Check this stackoverflow.com/questions/11377944/…
– mettleap
Nov 15 '18 at 17:31
Check this stackoverflow.com/questions/11377944/…
– mettleap
Nov 15 '18 at 17:31
add a comment |
3 Answers
3
active
oldest
votes
You must use overloaded parseInt method with radix.
For hex value an example:
Integer.parseInt("0x84B",16);
Always get same error. I've updated my code.
– Gianluca Benucci
Nov 16 '18 at 7:45
@GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.
– Tix
Nov 16 '18 at 7:58
1
You'll need to remove the leading0x
for this to work.Integer.parseInt("84B",16)
returns 2123, but with0x84B
you get aNumberFormatException
.
– jsheeran
Nov 16 '18 at 8:03
add a comment |
Try this:
Integer.parseInt(String.format("0b%02X",k),16);
Binary values are written using "0bxx" syntax
add a comment |
I've resolved my problem with this question
byte k = stream[i];
int b = 0;
try {
String hexStringNumber = String.format("0x%02X",k);
b = Integer.decode(hexStringNumber);
} catch (NumberFormatException e) {
e.printStackTrace();
}
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%2f53324825%2fnumberformatexception-with-string-of-an-hex-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You must use overloaded parseInt method with radix.
For hex value an example:
Integer.parseInt("0x84B",16);
Always get same error. I've updated my code.
– Gianluca Benucci
Nov 16 '18 at 7:45
@GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.
– Tix
Nov 16 '18 at 7:58
1
You'll need to remove the leading0x
for this to work.Integer.parseInt("84B",16)
returns 2123, but with0x84B
you get aNumberFormatException
.
– jsheeran
Nov 16 '18 at 8:03
add a comment |
You must use overloaded parseInt method with radix.
For hex value an example:
Integer.parseInt("0x84B",16);
Always get same error. I've updated my code.
– Gianluca Benucci
Nov 16 '18 at 7:45
@GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.
– Tix
Nov 16 '18 at 7:58
1
You'll need to remove the leading0x
for this to work.Integer.parseInt("84B",16)
returns 2123, but with0x84B
you get aNumberFormatException
.
– jsheeran
Nov 16 '18 at 8:03
add a comment |
You must use overloaded parseInt method with radix.
For hex value an example:
Integer.parseInt("0x84B",16);
You must use overloaded parseInt method with radix.
For hex value an example:
Integer.parseInt("0x84B",16);
answered Nov 15 '18 at 17:42
TixTix
616
616
Always get same error. I've updated my code.
– Gianluca Benucci
Nov 16 '18 at 7:45
@GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.
– Tix
Nov 16 '18 at 7:58
1
You'll need to remove the leading0x
for this to work.Integer.parseInt("84B",16)
returns 2123, but with0x84B
you get aNumberFormatException
.
– jsheeran
Nov 16 '18 at 8:03
add a comment |
Always get same error. I've updated my code.
– Gianluca Benucci
Nov 16 '18 at 7:45
@GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.
– Tix
Nov 16 '18 at 7:58
1
You'll need to remove the leading0x
for this to work.Integer.parseInt("84B",16)
returns 2123, but with0x84B
you get aNumberFormatException
.
– jsheeran
Nov 16 '18 at 8:03
Always get same error. I've updated my code.
– Gianluca Benucci
Nov 16 '18 at 7:45
Always get same error. I've updated my code.
– Gianluca Benucci
Nov 16 '18 at 7:45
@GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.
– Tix
Nov 16 '18 at 7:58
@GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.
– Tix
Nov 16 '18 at 7:58
1
1
You'll need to remove the leading
0x
for this to work. Integer.parseInt("84B",16)
returns 2123, but with 0x84B
you get a NumberFormatException
.– jsheeran
Nov 16 '18 at 8:03
You'll need to remove the leading
0x
for this to work. Integer.parseInt("84B",16)
returns 2123, but with 0x84B
you get a NumberFormatException
.– jsheeran
Nov 16 '18 at 8:03
add a comment |
Try this:
Integer.parseInt(String.format("0b%02X",k),16);
Binary values are written using "0bxx" syntax
add a comment |
Try this:
Integer.parseInt(String.format("0b%02X",k),16);
Binary values are written using "0bxx" syntax
add a comment |
Try this:
Integer.parseInt(String.format("0b%02X",k),16);
Binary values are written using "0bxx" syntax
Try this:
Integer.parseInt(String.format("0b%02X",k),16);
Binary values are written using "0bxx" syntax
answered Nov 16 '18 at 7:59
NickNick
5118
5118
add a comment |
add a comment |
I've resolved my problem with this question
byte k = stream[i];
int b = 0;
try {
String hexStringNumber = String.format("0x%02X",k);
b = Integer.decode(hexStringNumber);
} catch (NumberFormatException e) {
e.printStackTrace();
}
add a comment |
I've resolved my problem with this question
byte k = stream[i];
int b = 0;
try {
String hexStringNumber = String.format("0x%02X",k);
b = Integer.decode(hexStringNumber);
} catch (NumberFormatException e) {
e.printStackTrace();
}
add a comment |
I've resolved my problem with this question
byte k = stream[i];
int b = 0;
try {
String hexStringNumber = String.format("0x%02X",k);
b = Integer.decode(hexStringNumber);
} catch (NumberFormatException e) {
e.printStackTrace();
}
I've resolved my problem with this question
byte k = stream[i];
int b = 0;
try {
String hexStringNumber = String.format("0x%02X",k);
b = Integer.decode(hexStringNumber);
} catch (NumberFormatException e) {
e.printStackTrace();
}
answered Nov 16 '18 at 8:06
Gianluca BenucciGianluca Benucci
779
779
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%2f53324825%2fnumberformatexception-with-string-of-an-hex-value%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
Check this stackoverflow.com/questions/11377944/…
– mettleap
Nov 15 '18 at 17:31