Getting a byte out of a given String of 1s and 0s The Next CEO of Stack OverflowDoes a finally block always get executed in Java?Convert a string representation of a hex dump to a byte array using Java?Fastest way to determine if an integer's square root is an integerHow do I read / convert an InputStream into a String in Java?How to get an enum value from a string value in Java?How do I break out of nested loops in Java?Simple way to repeat a String in javaHow to split a string in JavaHow do I convert a String to an int in Java?Why is char[] preferred over String for passwords?
Car headlights in a world without electricity
Avoiding the "not like other girls" trope?
How to show a landlord what we have in savings?
Free fall ellipse or parabola?
Is a distribution that is normal, but highly skewed, considered Gaussian?
Read/write a pipe-delimited file line by line with some simple text manipulation
pgfplots: How to draw a tangent graph below two others?
Raspberry pi 3 B with Ubuntu 18.04 server arm64: what pi version
Is it possible to make a 9x9 table fit within the default margins?
Masking layers by a vector polygon layer in QGIS
Can a PhD from a non-TU9 German university become a professor in a TU9 university?
Is it OK to decorate a log book cover?
Creating a script with console commands
Is it "common practice in Fourier transform spectroscopy to multiply the measured interferogram by an apodizing function"? If so, why?
Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact
How badly should I try to prevent a user from XSSing themselves?
Does int main() need a declaration on C++?
Direct Implications Between USA and UK in Event of No-Deal Brexit
Is it okay to majorly distort historical facts while writing a fiction story?
How do I secure a TV wall mount?
Why did early computer designers eschew integers?
How should I connect my cat5 cable to connectors having an orange-green line?
How to unfasten electrical subpanel attached with ramset
Is this a new Fibonacci Identity?
Getting a byte out of a given String of 1s and 0s
The Next CEO of Stack OverflowDoes a finally block always get executed in Java?Convert a string representation of a hex dump to a byte array using Java?Fastest way to determine if an integer's square root is an integerHow do I read / convert an InputStream into a String in Java?How to get an enum value from a string value in Java?How do I break out of nested loops in Java?Simple way to repeat a String in javaHow to split a string in JavaHow do I convert a String to an int in Java?Why is char[] preferred over String for passwords?
I am looking for a fast way to get a byte out of a String, if the string consists of 1s and 0s. For example: String a="101010" . How do I get the Byte b ="101010"?
I looked at some other posts but they usually consist of how to get the byte value of a String. I am interested in basically switching the format.
Edit:
to make it more clear, I am looking for this
String a = "101010"
byte b = //What to do?
System.out.print(b)
--> 0b101010
java
add a comment |
I am looking for a fast way to get a byte out of a String, if the string consists of 1s and 0s. For example: String a="101010" . How do I get the Byte b ="101010"?
I looked at some other posts but they usually consist of how to get the byte value of a String. I am interested in basically switching the format.
Edit:
to make it more clear, I am looking for this
String a = "101010"
byte b = //What to do?
System.out.print(b)
--> 0b101010
java
Some more context would be helpful here. Using your examples, why can't you treatString a
as an array and search if it containsByte b
in it. Are you looking for a function that does that?
– Jerry M.
Mar 21 at 20:03
12
Are you looking forbyte b = Byte.parseByte(a, 2);
– Elliott Frisch
Mar 21 at 20:03
Nope, I have put an extra example to explain what I am looking for
– Tricker Macedonia
Mar 22 at 6:59
add a comment |
I am looking for a fast way to get a byte out of a String, if the string consists of 1s and 0s. For example: String a="101010" . How do I get the Byte b ="101010"?
I looked at some other posts but they usually consist of how to get the byte value of a String. I am interested in basically switching the format.
Edit:
to make it more clear, I am looking for this
String a = "101010"
byte b = //What to do?
System.out.print(b)
--> 0b101010
java
I am looking for a fast way to get a byte out of a String, if the string consists of 1s and 0s. For example: String a="101010" . How do I get the Byte b ="101010"?
I looked at some other posts but they usually consist of how to get the byte value of a String. I am interested in basically switching the format.
Edit:
to make it more clear, I am looking for this
String a = "101010"
byte b = //What to do?
System.out.print(b)
--> 0b101010
java
java
edited Mar 22 at 6:53
Tricker Macedonia
asked Mar 21 at 19:58
Tricker MacedoniaTricker Macedonia
286
286
Some more context would be helpful here. Using your examples, why can't you treatString a
as an array and search if it containsByte b
in it. Are you looking for a function that does that?
– Jerry M.
Mar 21 at 20:03
12
Are you looking forbyte b = Byte.parseByte(a, 2);
– Elliott Frisch
Mar 21 at 20:03
Nope, I have put an extra example to explain what I am looking for
– Tricker Macedonia
Mar 22 at 6:59
add a comment |
Some more context would be helpful here. Using your examples, why can't you treatString a
as an array and search if it containsByte b
in it. Are you looking for a function that does that?
– Jerry M.
Mar 21 at 20:03
12
Are you looking forbyte b = Byte.parseByte(a, 2);
– Elliott Frisch
Mar 21 at 20:03
Nope, I have put an extra example to explain what I am looking for
– Tricker Macedonia
Mar 22 at 6:59
Some more context would be helpful here. Using your examples, why can't you treat
String a
as an array and search if it contains Byte b
in it. Are you looking for a function that does that?– Jerry M.
Mar 21 at 20:03
Some more context would be helpful here. Using your examples, why can't you treat
String a
as an array and search if it contains Byte b
in it. Are you looking for a function that does that?– Jerry M.
Mar 21 at 20:03
12
12
Are you looking for
byte b = Byte.parseByte(a, 2);
– Elliott Frisch
Mar 21 at 20:03
Are you looking for
byte b = Byte.parseByte(a, 2);
– Elliott Frisch
Mar 21 at 20:03
Nope, I have put an extra example to explain what I am looking for
– Tricker Macedonia
Mar 22 at 6:59
Nope, I have put an extra example to explain what I am looking for
– Tricker Macedonia
Mar 22 at 6:59
add a comment |
2 Answers
2
active
oldest
votes
The Byte
class and other integral types like Integer
can parse numbers in a range of radices from 2 to 36 (encoded using digits 0-9 and letters A-Z).
String a = "101010";
byte b = Byte.parseByte(a, 2);
String c = Integer.toBinaryString(b & 0xFF);
String d = "0b" + c;
Remember, a Java byte
is signed, so we need to mask out any high-order bits that would be introduced when used in a way that promotes the value to a signed int
.
String a= "1010"; byte b = Byte.parseByte(a,2); System.out.println(b); printed the decimal value of a. I want it to print the same as "a" 1010... I am not sure if I am clear enough in my post, I just want to get a byte with the same form.
– Tricker Macedonia
Mar 22 at 6:49
@TrickerMacedonia it is not the decimal value that you getting
– paradox
Mar 22 at 7:14
@TrickerMacedonia I added some detail about conversion back to aString
.
– erickson
Mar 23 at 1:33
add a comment |
A byte
is a signed primitive value in Java, when you print it you will get a decimal representation of that value (by default). You need to perform two steps; first, parse the String
into a byte
; second, display the byte
with your desired formatting (as a binary String
with leading 0b
). That can be done simply enough, something like
String a = "101010";
byte b = Byte.parseByte(a, 2);
System.out.printf("0b%s%n", Integer.toBinaryString(b));
Which outputs (as requested)
0b101010
add a comment |
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%2f55288393%2fgetting-a-byte-out-of-a-given-string-of-1s-and-0s%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The Byte
class and other integral types like Integer
can parse numbers in a range of radices from 2 to 36 (encoded using digits 0-9 and letters A-Z).
String a = "101010";
byte b = Byte.parseByte(a, 2);
String c = Integer.toBinaryString(b & 0xFF);
String d = "0b" + c;
Remember, a Java byte
is signed, so we need to mask out any high-order bits that would be introduced when used in a way that promotes the value to a signed int
.
String a= "1010"; byte b = Byte.parseByte(a,2); System.out.println(b); printed the decimal value of a. I want it to print the same as "a" 1010... I am not sure if I am clear enough in my post, I just want to get a byte with the same form.
– Tricker Macedonia
Mar 22 at 6:49
@TrickerMacedonia it is not the decimal value that you getting
– paradox
Mar 22 at 7:14
@TrickerMacedonia I added some detail about conversion back to aString
.
– erickson
Mar 23 at 1:33
add a comment |
The Byte
class and other integral types like Integer
can parse numbers in a range of radices from 2 to 36 (encoded using digits 0-9 and letters A-Z).
String a = "101010";
byte b = Byte.parseByte(a, 2);
String c = Integer.toBinaryString(b & 0xFF);
String d = "0b" + c;
Remember, a Java byte
is signed, so we need to mask out any high-order bits that would be introduced when used in a way that promotes the value to a signed int
.
String a= "1010"; byte b = Byte.parseByte(a,2); System.out.println(b); printed the decimal value of a. I want it to print the same as "a" 1010... I am not sure if I am clear enough in my post, I just want to get a byte with the same form.
– Tricker Macedonia
Mar 22 at 6:49
@TrickerMacedonia it is not the decimal value that you getting
– paradox
Mar 22 at 7:14
@TrickerMacedonia I added some detail about conversion back to aString
.
– erickson
Mar 23 at 1:33
add a comment |
The Byte
class and other integral types like Integer
can parse numbers in a range of radices from 2 to 36 (encoded using digits 0-9 and letters A-Z).
String a = "101010";
byte b = Byte.parseByte(a, 2);
String c = Integer.toBinaryString(b & 0xFF);
String d = "0b" + c;
Remember, a Java byte
is signed, so we need to mask out any high-order bits that would be introduced when used in a way that promotes the value to a signed int
.
The Byte
class and other integral types like Integer
can parse numbers in a range of radices from 2 to 36 (encoded using digits 0-9 and letters A-Z).
String a = "101010";
byte b = Byte.parseByte(a, 2);
String c = Integer.toBinaryString(b & 0xFF);
String d = "0b" + c;
Remember, a Java byte
is signed, so we need to mask out any high-order bits that would be introduced when used in a way that promotes the value to a signed int
.
edited Mar 23 at 1:33
answered Mar 21 at 20:44
ericksonerickson
224k42334431
224k42334431
String a= "1010"; byte b = Byte.parseByte(a,2); System.out.println(b); printed the decimal value of a. I want it to print the same as "a" 1010... I am not sure if I am clear enough in my post, I just want to get a byte with the same form.
– Tricker Macedonia
Mar 22 at 6:49
@TrickerMacedonia it is not the decimal value that you getting
– paradox
Mar 22 at 7:14
@TrickerMacedonia I added some detail about conversion back to aString
.
– erickson
Mar 23 at 1:33
add a comment |
String a= "1010"; byte b = Byte.parseByte(a,2); System.out.println(b); printed the decimal value of a. I want it to print the same as "a" 1010... I am not sure if I am clear enough in my post, I just want to get a byte with the same form.
– Tricker Macedonia
Mar 22 at 6:49
@TrickerMacedonia it is not the decimal value that you getting
– paradox
Mar 22 at 7:14
@TrickerMacedonia I added some detail about conversion back to aString
.
– erickson
Mar 23 at 1:33
String a= "1010"; byte b = Byte.parseByte(a,2); System.out.println(b); printed the decimal value of a. I want it to print the same as "a" 1010... I am not sure if I am clear enough in my post, I just want to get a byte with the same form.
– Tricker Macedonia
Mar 22 at 6:49
String a= "1010"; byte b = Byte.parseByte(a,2); System.out.println(b); printed the decimal value of a. I want it to print the same as "a" 1010... I am not sure if I am clear enough in my post, I just want to get a byte with the same form.
– Tricker Macedonia
Mar 22 at 6:49
@TrickerMacedonia it is not the decimal value that you getting
– paradox
Mar 22 at 7:14
@TrickerMacedonia it is not the decimal value that you getting
– paradox
Mar 22 at 7:14
@TrickerMacedonia I added some detail about conversion back to a
String
.– erickson
Mar 23 at 1:33
@TrickerMacedonia I added some detail about conversion back to a
String
.– erickson
Mar 23 at 1:33
add a comment |
A byte
is a signed primitive value in Java, when you print it you will get a decimal representation of that value (by default). You need to perform two steps; first, parse the String
into a byte
; second, display the byte
with your desired formatting (as a binary String
with leading 0b
). That can be done simply enough, something like
String a = "101010";
byte b = Byte.parseByte(a, 2);
System.out.printf("0b%s%n", Integer.toBinaryString(b));
Which outputs (as requested)
0b101010
add a comment |
A byte
is a signed primitive value in Java, when you print it you will get a decimal representation of that value (by default). You need to perform two steps; first, parse the String
into a byte
; second, display the byte
with your desired formatting (as a binary String
with leading 0b
). That can be done simply enough, something like
String a = "101010";
byte b = Byte.parseByte(a, 2);
System.out.printf("0b%s%n", Integer.toBinaryString(b));
Which outputs (as requested)
0b101010
add a comment |
A byte
is a signed primitive value in Java, when you print it you will get a decimal representation of that value (by default). You need to perform two steps; first, parse the String
into a byte
; second, display the byte
with your desired formatting (as a binary String
with leading 0b
). That can be done simply enough, something like
String a = "101010";
byte b = Byte.parseByte(a, 2);
System.out.printf("0b%s%n", Integer.toBinaryString(b));
Which outputs (as requested)
0b101010
A byte
is a signed primitive value in Java, when you print it you will get a decimal representation of that value (by default). You need to perform two steps; first, parse the String
into a byte
; second, display the byte
with your desired formatting (as a binary String
with leading 0b
). That can be done simply enough, something like
String a = "101010";
byte b = Byte.parseByte(a, 2);
System.out.printf("0b%s%n", Integer.toBinaryString(b));
Which outputs (as requested)
0b101010
answered Mar 23 at 20:00
Elliott FrischElliott Frisch
156k1396191
156k1396191
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%2f55288393%2fgetting-a-byte-out-of-a-given-string-of-1s-and-0s%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
Some more context would be helpful here. Using your examples, why can't you treat
String a
as an array and search if it containsByte b
in it. Are you looking for a function that does that?– Jerry M.
Mar 21 at 20:03
12
Are you looking for
byte b = Byte.parseByte(a, 2);
– Elliott Frisch
Mar 21 at 20:03
Nope, I have put an extra example to explain what I am looking for
– Tricker Macedonia
Mar 22 at 6:59