Convert binary String to hexadecimal String2019 Community Moderator ElectionHow to convert decimal to hex in JavaScript?Converting an integer to a hexadecimal string in RubyConvert hex string to int in PythonHow do you convert a byte array to a hexadecimal string, and vice versa?Decimal/Hexadecimal/Binary ConversionHow to convert a byte array to a hex string in Java?Bitwise Operators and Binary string evaluationsMoving between hexadecimal and binary notationHexadecimal to binary to decimal GUIWithout format specifiers, converting binary int number to hexadecimal in C

How well should I expect Adam to work?

What is the significance behind "40 days" that often appears in the Bible?

Why does a Star of David appear at a rally with Francisco Franco?

What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?

How could a scammer know the apps on my phone / iTunes account?

Fastest way to pop N items from a large dict

Are Roman Catholic priests ever addressed as pastor

Why is the President allowed to veto a cancellation of emergency powers?

Welcoming 2019 Pi day: How to draw the letter π?

ERC721: How to get the owned tokens of an address

How are passwords stolen from companies if they only store hashes?

Employee lack of ownership

"of which" is correct here?

My adviser wants to be the first author

How do I hide Chekhov's Gun?

Is it insecure to send a password in a `curl` command?

Shortcut for setting origin to vertex

Official degrees of earth’s rotation per day

A single argument pattern definition applies to multiple-argument patterns?

How do you talk to someone whose loved one is dying?

Equivalents to the present tense

Planetary tidal locking causing asymetrical water distribution

Brexit - No Deal Rejection

What is a ^ b and (a & b) << 1?



Convert binary String to hexadecimal String



2019 Community Moderator ElectionHow to convert decimal to hex in JavaScript?Converting an integer to a hexadecimal string in RubyConvert hex string to int in PythonHow do you convert a byte array to a hexadecimal string, and vice versa?Decimal/Hexadecimal/Binary ConversionHow to convert a byte array to a hex string in Java?Bitwise Operators and Binary string evaluationsMoving between hexadecimal and binary notationHexadecimal to binary to decimal GUIWithout format specifiers, converting binary int number to hexadecimal in C










1















I have this conversion in my Kotlin class for Android:



val binary = "01000100000111001011011011100010111000110011010111010110"
val hexadecimal = BigInteger(binary, 2).toString(16)


Which is producing the expected value of 441CB6E2E335D6.



Now I want to reproduce this in Visual Basic and I am doing something like this:



Dim binary = "01000100000111001011011011100010111000110011010111010110"
Dim hexadecimal = BigInteger.Parse(binary, 2).ToString("X")


Which is producing 0A7108304A751AFEC876F740BC1F2CB59772FB7C6C753E.



I am not an expert in Visual Basic, but from what I researched, I think this is the right way to convert a binary to hexadecimal. What I am doing wrong?










share|improve this question



















  • 1





    Just a note: the second parameter in Parse is not the base, but the enum NumberStyles where 2 is AllowTrailingWhite. You are simply parsing it as a decimal.

    – ZorgoZ
    Mar 6 at 17:01











  • @ZorgoZ alright. So doing only BigInteger.Parse(binary) should do the work, right?

    – Ravers
    Mar 6 at 17:12











  • No. Have you seen anywhere in the documentation, that it can parse binary?

    – ZorgoZ
    Mar 6 at 17:14















1















I have this conversion in my Kotlin class for Android:



val binary = "01000100000111001011011011100010111000110011010111010110"
val hexadecimal = BigInteger(binary, 2).toString(16)


Which is producing the expected value of 441CB6E2E335D6.



Now I want to reproduce this in Visual Basic and I am doing something like this:



Dim binary = "01000100000111001011011011100010111000110011010111010110"
Dim hexadecimal = BigInteger.Parse(binary, 2).ToString("X")


Which is producing 0A7108304A751AFEC876F740BC1F2CB59772FB7C6C753E.



I am not an expert in Visual Basic, but from what I researched, I think this is the right way to convert a binary to hexadecimal. What I am doing wrong?










share|improve this question



















  • 1





    Just a note: the second parameter in Parse is not the base, but the enum NumberStyles where 2 is AllowTrailingWhite. You are simply parsing it as a decimal.

    – ZorgoZ
    Mar 6 at 17:01











  • @ZorgoZ alright. So doing only BigInteger.Parse(binary) should do the work, right?

    – Ravers
    Mar 6 at 17:12











  • No. Have you seen anywhere in the documentation, that it can parse binary?

    – ZorgoZ
    Mar 6 at 17:14













1












1








1


1






I have this conversion in my Kotlin class for Android:



val binary = "01000100000111001011011011100010111000110011010111010110"
val hexadecimal = BigInteger(binary, 2).toString(16)


Which is producing the expected value of 441CB6E2E335D6.



Now I want to reproduce this in Visual Basic and I am doing something like this:



Dim binary = "01000100000111001011011011100010111000110011010111010110"
Dim hexadecimal = BigInteger.Parse(binary, 2).ToString("X")


Which is producing 0A7108304A751AFEC876F740BC1F2CB59772FB7C6C753E.



I am not an expert in Visual Basic, but from what I researched, I think this is the right way to convert a binary to hexadecimal. What I am doing wrong?










share|improve this question
















I have this conversion in my Kotlin class for Android:



val binary = "01000100000111001011011011100010111000110011010111010110"
val hexadecimal = BigInteger(binary, 2).toString(16)


Which is producing the expected value of 441CB6E2E335D6.



Now I want to reproduce this in Visual Basic and I am doing something like this:



Dim binary = "01000100000111001011011011100010111000110011010111010110"
Dim hexadecimal = BigInteger.Parse(binary, 2).ToString("X")


Which is producing 0A7108304A751AFEC876F740BC1F2CB59772FB7C6C753E.



I am not an expert in Visual Basic, but from what I researched, I think this is the right way to convert a binary to hexadecimal. What I am doing wrong?







vb.net kotlin binary hex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 8:08









Jimi

9,43242035




9,43242035










asked Mar 6 at 16:50









RaversRavers

283523




283523







  • 1





    Just a note: the second parameter in Parse is not the base, but the enum NumberStyles where 2 is AllowTrailingWhite. You are simply parsing it as a decimal.

    – ZorgoZ
    Mar 6 at 17:01











  • @ZorgoZ alright. So doing only BigInteger.Parse(binary) should do the work, right?

    – Ravers
    Mar 6 at 17:12











  • No. Have you seen anywhere in the documentation, that it can parse binary?

    – ZorgoZ
    Mar 6 at 17:14












  • 1





    Just a note: the second parameter in Parse is not the base, but the enum NumberStyles where 2 is AllowTrailingWhite. You are simply parsing it as a decimal.

    – ZorgoZ
    Mar 6 at 17:01











  • @ZorgoZ alright. So doing only BigInteger.Parse(binary) should do the work, right?

    – Ravers
    Mar 6 at 17:12











  • No. Have you seen anywhere in the documentation, that it can parse binary?

    – ZorgoZ
    Mar 6 at 17:14







1




1





Just a note: the second parameter in Parse is not the base, but the enum NumberStyles where 2 is AllowTrailingWhite. You are simply parsing it as a decimal.

– ZorgoZ
Mar 6 at 17:01





Just a note: the second parameter in Parse is not the base, but the enum NumberStyles where 2 is AllowTrailingWhite. You are simply parsing it as a decimal.

– ZorgoZ
Mar 6 at 17:01













@ZorgoZ alright. So doing only BigInteger.Parse(binary) should do the work, right?

– Ravers
Mar 6 at 17:12





@ZorgoZ alright. So doing only BigInteger.Parse(binary) should do the work, right?

– Ravers
Mar 6 at 17:12













No. Have you seen anywhere in the documentation, that it can parse binary?

– ZorgoZ
Mar 6 at 17:14





No. Have you seen anywhere in the documentation, that it can parse binary?

– ZorgoZ
Mar 6 at 17:14












2 Answers
2






active

oldest

votes


















2














You can write a simple parser for the string representing the bits:



Dim sb As StringBuilder = New StringBuilder()
For pos As Integer = 0 To binary.Length - 8 Step 8
sb.Append(Convert.ToByte(binary.Substring(pos, 8), 2).ToString("X2"))
Next




Console.WriteLine(sb) will print "441CB6E2E335D6"


Or use a Module to add an extension method to the string data type:



Imports System.Runtime.CompilerServices
Imports System.Text

Module modStringExtensions
<Extension()>
Public Function ToHexFromBits(ByVal Value As String) As String
If (Not (Value.Length Mod 8 = 0)) Then Throw New FormatException("Invalid string length")
Dim sb As StringBuilder = New StringBuilder()
For pos As Integer = 0 To Value.Length - 8 Step 8
sb.Append(Convert.ToByte(Value.Substring(pos, 8), 2).ToString("X2"))
Next
Return sb.ToString()
End Function
End Module


Then use the extension to convert the bits string to a HEX representation:



Dim result As String = binary.ToHexFromBits()





share|improve this answer






























    0














    Following code is c#, but might not be too hard to translate it to vb.net.



    string BinToHex(string value)

    var res = new char[(int)(value.Length / 4)];
    int j = res.Length-1;
    for (int i = value.Length - 1; i > 0; i -= 4)

    int x = ((int)value[i]-48)
    +((int)value[i-1]-48)*2
    +((int)value[i-2]-48)*4
    +((int)value[i-3]-48)*8;

    res[j--] = x.ToString("X")[0];


    return new string(res);



    Beware: it won't handle input that has not the proper number of bits (multiple of 4). Anyway, the idea is that you can translate between base 2 and base 16 without the use of base 10. You could even step from left to right.






    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%2f55028264%2fconvert-binary-string-to-hexadecimal-string%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














      You can write a simple parser for the string representing the bits:



      Dim sb As StringBuilder = New StringBuilder()
      For pos As Integer = 0 To binary.Length - 8 Step 8
      sb.Append(Convert.ToByte(binary.Substring(pos, 8), 2).ToString("X2"))
      Next




      Console.WriteLine(sb) will print "441CB6E2E335D6"


      Or use a Module to add an extension method to the string data type:



      Imports System.Runtime.CompilerServices
      Imports System.Text

      Module modStringExtensions
      <Extension()>
      Public Function ToHexFromBits(ByVal Value As String) As String
      If (Not (Value.Length Mod 8 = 0)) Then Throw New FormatException("Invalid string length")
      Dim sb As StringBuilder = New StringBuilder()
      For pos As Integer = 0 To Value.Length - 8 Step 8
      sb.Append(Convert.ToByte(Value.Substring(pos, 8), 2).ToString("X2"))
      Next
      Return sb.ToString()
      End Function
      End Module


      Then use the extension to convert the bits string to a HEX representation:



      Dim result As String = binary.ToHexFromBits()





      share|improve this answer



























        2














        You can write a simple parser for the string representing the bits:



        Dim sb As StringBuilder = New StringBuilder()
        For pos As Integer = 0 To binary.Length - 8 Step 8
        sb.Append(Convert.ToByte(binary.Substring(pos, 8), 2).ToString("X2"))
        Next




        Console.WriteLine(sb) will print "441CB6E2E335D6"


        Or use a Module to add an extension method to the string data type:



        Imports System.Runtime.CompilerServices
        Imports System.Text

        Module modStringExtensions
        <Extension()>
        Public Function ToHexFromBits(ByVal Value As String) As String
        If (Not (Value.Length Mod 8 = 0)) Then Throw New FormatException("Invalid string length")
        Dim sb As StringBuilder = New StringBuilder()
        For pos As Integer = 0 To Value.Length - 8 Step 8
        sb.Append(Convert.ToByte(Value.Substring(pos, 8), 2).ToString("X2"))
        Next
        Return sb.ToString()
        End Function
        End Module


        Then use the extension to convert the bits string to a HEX representation:



        Dim result As String = binary.ToHexFromBits()





        share|improve this answer

























          2












          2








          2







          You can write a simple parser for the string representing the bits:



          Dim sb As StringBuilder = New StringBuilder()
          For pos As Integer = 0 To binary.Length - 8 Step 8
          sb.Append(Convert.ToByte(binary.Substring(pos, 8), 2).ToString("X2"))
          Next




          Console.WriteLine(sb) will print "441CB6E2E335D6"


          Or use a Module to add an extension method to the string data type:



          Imports System.Runtime.CompilerServices
          Imports System.Text

          Module modStringExtensions
          <Extension()>
          Public Function ToHexFromBits(ByVal Value As String) As String
          If (Not (Value.Length Mod 8 = 0)) Then Throw New FormatException("Invalid string length")
          Dim sb As StringBuilder = New StringBuilder()
          For pos As Integer = 0 To Value.Length - 8 Step 8
          sb.Append(Convert.ToByte(Value.Substring(pos, 8), 2).ToString("X2"))
          Next
          Return sb.ToString()
          End Function
          End Module


          Then use the extension to convert the bits string to a HEX representation:



          Dim result As String = binary.ToHexFromBits()





          share|improve this answer













          You can write a simple parser for the string representing the bits:



          Dim sb As StringBuilder = New StringBuilder()
          For pos As Integer = 0 To binary.Length - 8 Step 8
          sb.Append(Convert.ToByte(binary.Substring(pos, 8), 2).ToString("X2"))
          Next




          Console.WriteLine(sb) will print "441CB6E2E335D6"


          Or use a Module to add an extension method to the string data type:



          Imports System.Runtime.CompilerServices
          Imports System.Text

          Module modStringExtensions
          <Extension()>
          Public Function ToHexFromBits(ByVal Value As String) As String
          If (Not (Value.Length Mod 8 = 0)) Then Throw New FormatException("Invalid string length")
          Dim sb As StringBuilder = New StringBuilder()
          For pos As Integer = 0 To Value.Length - 8 Step 8
          sb.Append(Convert.ToByte(Value.Substring(pos, 8), 2).ToString("X2"))
          Next
          Return sb.ToString()
          End Function
          End Module


          Then use the extension to convert the bits string to a HEX representation:



          Dim result As String = binary.ToHexFromBits()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 6 at 20:34









          JimiJimi

          9,43242035




          9,43242035























              0














              Following code is c#, but might not be too hard to translate it to vb.net.



              string BinToHex(string value)

              var res = new char[(int)(value.Length / 4)];
              int j = res.Length-1;
              for (int i = value.Length - 1; i > 0; i -= 4)

              int x = ((int)value[i]-48)
              +((int)value[i-1]-48)*2
              +((int)value[i-2]-48)*4
              +((int)value[i-3]-48)*8;

              res[j--] = x.ToString("X")[0];


              return new string(res);



              Beware: it won't handle input that has not the proper number of bits (multiple of 4). Anyway, the idea is that you can translate between base 2 and base 16 without the use of base 10. You could even step from left to right.






              share|improve this answer





























                0














                Following code is c#, but might not be too hard to translate it to vb.net.



                string BinToHex(string value)

                var res = new char[(int)(value.Length / 4)];
                int j = res.Length-1;
                for (int i = value.Length - 1; i > 0; i -= 4)

                int x = ((int)value[i]-48)
                +((int)value[i-1]-48)*2
                +((int)value[i-2]-48)*4
                +((int)value[i-3]-48)*8;

                res[j--] = x.ToString("X")[0];


                return new string(res);



                Beware: it won't handle input that has not the proper number of bits (multiple of 4). Anyway, the idea is that you can translate between base 2 and base 16 without the use of base 10. You could even step from left to right.






                share|improve this answer



























                  0












                  0








                  0







                  Following code is c#, but might not be too hard to translate it to vb.net.



                  string BinToHex(string value)

                  var res = new char[(int)(value.Length / 4)];
                  int j = res.Length-1;
                  for (int i = value.Length - 1; i > 0; i -= 4)

                  int x = ((int)value[i]-48)
                  +((int)value[i-1]-48)*2
                  +((int)value[i-2]-48)*4
                  +((int)value[i-3]-48)*8;

                  res[j--] = x.ToString("X")[0];


                  return new string(res);



                  Beware: it won't handle input that has not the proper number of bits (multiple of 4). Anyway, the idea is that you can translate between base 2 and base 16 without the use of base 10. You could even step from left to right.






                  share|improve this answer















                  Following code is c#, but might not be too hard to translate it to vb.net.



                  string BinToHex(string value)

                  var res = new char[(int)(value.Length / 4)];
                  int j = res.Length-1;
                  for (int i = value.Length - 1; i > 0; i -= 4)

                  int x = ((int)value[i]-48)
                  +((int)value[i-1]-48)*2
                  +((int)value[i-2]-48)*4
                  +((int)value[i-3]-48)*8;

                  res[j--] = x.ToString("X")[0];


                  return new string(res);



                  Beware: it won't handle input that has not the proper number of bits (multiple of 4). Anyway, the idea is that you can translate between base 2 and base 16 without the use of base 10. You could even step from left to right.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 6 at 17:28

























                  answered Mar 6 at 17:16









                  ZorgoZZorgoZ

                  1,2841417




                  1,2841417



























                      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%2f55028264%2fconvert-binary-string-to-hexadecimal-string%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

                      AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                      Алба-Юлія

                      Захаров Федір Захарович