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?










0















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










share|improve this question
























  • 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





    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















0















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










share|improve this question
























  • 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





    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













0












0








0








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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





    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

















  • 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





    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
















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












2 Answers
2






active

oldest

votes


















2














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.






share|improve this answer

























  • 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 a String.

    – erickson
    Mar 23 at 1:33


















0














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





share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    2














    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.






    share|improve this answer

























    • 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 a String.

      – erickson
      Mar 23 at 1:33















    2














    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.






    share|improve this answer

























    • 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 a String.

      – erickson
      Mar 23 at 1:33













    2












    2








    2







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 a String.

      – 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












    • @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
















    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













    0














    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





    share|improve this answer



























      0














      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





      share|improve this answer

























        0












        0








        0







        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 23 at 20:00









        Elliott FrischElliott Frisch

        156k1396191




        156k1396191



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현