Home > Net >  C# byte array to base10 string
C# byte array to base10 string

Time:01-17

I need to take a byte array and convert it to a string. I basically need to do the opposite of this:

BigInteger.Parse(num).ToByteArray();

CodePudding user response:

If you have:

string num = "1234567890123456789012345678901234567890";
byte[] bytes = BigInteger.Parse(num).ToByteArray();

and you want to get from the byte[] bytes array back to the string "1234567890123456789012345678901234567890", this is the code that reverses that operation:

string numDecoded = new BigInteger(bytes).ToString();

.NET Fiddle

CodePudding user response:

The proper way to do this is as described in the previous answer!

But for further information please look through this Stackoverflow as well as the official Microsoft documentation

BigInteger to Hex/Decimal/Octal/Binary strings?

I think it contains more than enough information about everything you will need to know about this topic

If you need more information

Look through this documentation from Microsoft

https://docs.microsoft.com/en-us/dotnet/api/system.numerics.biginteger.tobytearray?view=net-6.0

var bigint = BigInteger.Parse("123456789012345678901234567890");

// Convert to base 10 (decimal):
string base10 = bigint.ToString();
// This returns 123456789012345678901234567890

// Convert to base 16 (hexadecimal):
string base16 = bigint.ToString("X");
// This returns the same number in hexadecimal format
  •  Tags:  
  • Related