Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9047901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:01:01+00:00 2026-06-16T12:01:01+00:00

The problem I have a byte[] that is converted to a hex string, and

  • 0

The problem

I have a byte[] that is converted to a hex string, and then that string is parsed like this BigInteger.Parse(thatString,NumberSyles.Hexnumber).

This seems wasteful since BigInteger is able to accept a byte[], as long as the two’s complement is accounted for.


An working (inefficient) example

According to MSDN the most significant bit of the last byte should be zero in order for the following hex number be a positive one. The following is an example of a hex number that has this issue:

byte[] ripeHashNetwork = GetByteHash();
foreach (var item in ripeHashNetwork)
{
   Console.Write(item + "," );
} 

// Output:  
//      0,1,9,102,119,96,6,149,61,85,103,67,158,94,57,248,106,13,39,59,238,214,25,103,246

// Convert to Hex string using this http://stackoverflow.com/a/624379/328397
// Output: 
//       00010966776006953D5567439E5E39F86A0D273BEED61967F6` 

Okay, let’s pass that string into the static method of BigInteger:

 BigInteger bi2 = BigInt.Parse(thatString,NumberSyles.Hexnumber);

// Output bi2.ToString() ==
//                {25420294593250030202636073700053352635053786165627414518}

Now that I have a baseline of data, and known conversions that work, I want to make it better/faster/etc.


A not working (efficient) example

Now my goal is to round-trip a byte[] into BigInt and make the result look like 25420294593250030202636073700053352635053786165627414518. Let’s get started:

So according to MSDN I need a zero in my last byte to avoid my number from being seen as a two’s compliment. I’ll add the zero and print it out to be sure:

foreach (var item in ripeHashNetwork)
{
   Console.Write(item + "," );
} 

// Output:                            
//    0,1,9,102,119,96,6,149,61,85,103,67,158,94,57,248,106,13,39,59,238,214,25,103,246,0 

Okay, let’s pass that byte[] into the constructor of BigInteger:

 BigInteger bi2 = new BigInteger(ripeHashNetwork);

// Output bi2.ToString() ==
//                {1546695054495833846267861247985902403343958296074401935327488}

What I skipped over is the sample of what bigInt does to my byte array if I don’t add the trailing zero. What happens is that I get a negative number which is wrong. I’ll post that if you want.

So what am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:01:02+00:00Added an answer on June 16, 2026 at 12:01 pm

    When you are going via the hex string, the first byte of your array is becoming the most significant byte of the resulting BigInteger.

    When you are adding a trailing zero, the last bye of your array is the most significant.

    I’m not sure which case is right for you, but that’s why you’re getting different answers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have a string that looks like this: [1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18] Question: How can you
Here's the problem. I ,for example,have a string 2500.Its converted from byte array into
I have a method that currently returns a string converted from a byte array:
I have a method that currently returns a string converted from a byte array:
I've seen that a few instances of this problem have been raised already. However,
Problem: I have a table that prints out vertical but I would like it
Problem: I have a value that is set in $record, for instance 1.69. Then
I have this program that is reading the test string from a textbox and
I am implementing a piece of software that works like this: I have a
I have a byte array that has hex values and I initially put those

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.