0

How do i convert a value given as a hexadecimal string to an Octal format, when the number is too large to fit in a 64 bits number?

I currently convert a byte array to hex like this

Byte[] data = { 116, 4, 228, 18, 189, 145, 31, 7, 123, 74, 174, 151, 54, 144, 224, 49, 210, 169, 43, 213 };
hex = BitConverter.ToString(data).Replace("-", string.Empty);

Output in Hex:

7404E412BD911F077B4AAE973690E031D2A92BD5

How do I get the Octal representation?

So i tried this earlier, but it doesn't work.

string binaryval = "";


  binaryval = Convert.ToString(Convert.ToInt32(hexValue,16), 8);

    foreach (char ch in hexValue)
    {

           binaryval += Convert.ToString(Convert.ToInt32(ch.ToString(), 16), 8);

    }
4

5 回答 5

4

Have a look at the following link on how to convert hex to decimal, octal or binary. It comes with complete code examples:

/// <summary>
/// Hex2s the octal.
/// </summary>
/// <param name="hexvalue">The hexvalue.</param>
/// <returns></returns>
public static string hex2Octal(string hexvalue)
{
   string binaryval = "";
   binaryval = Convert.ToString(Convert.ToInt32(hexvalue, 16), 8);
   return binaryval;
}
于 2012-06-22T14:27:58.207 回答
3

This is not that hard. You should have googled for it before :) Try this method:

public static string hex2Octal(string hexvalue)
   {
      string binaryval = "";
      binaryval = Convert.ToString(Convert.ToInt32(hexvalue, 16), 8);
      return binaryval;
   }
于 2012-06-22T14:28:57.827 回答
3

Assuming you start off with a hexadecimal string of an arbitrary length, I think the best you can do is take each three bytes and convert them, because 0xFFFFFF is exactly 77777777 octal (with one or two bytes you have to do more calculation, so this is simply the easiest approach).

string hex = "7404E412BD911F077B4AAE973690E031D2A92BD5";
string octal = "";

for (int i = hex.Length; i > 0; i -= 6)
{
    string threebyte;
    if (i < 6)
        threebyte = hex.Substring(0, hex.Length % 6);
    else
        threebyte = hex.Substring(i - 6, 6);

    octal = Convert.ToString(Convert.ToInt32(threebyte, 16), 8) + octal;
}

This will give you the following correct octal result:

72004710112754421740736645256456332207003072252225725

You can verify this result easily by taking a smaller number and comparing the least-significant part of the outcome when you type it in Windows Calculator (which can convert from Hex to Oct when you select View > Programmer, or Alt-3). I.e., when you take A92BD5 Windows Calculator shows 52225725 as outcome, equal to the above.

The reason that you cannot simply concatenate the results of converting each byte, or each four bytes (int), is that only one and a half byte fits in two octals (FFF equals 7777, but the range 00-FF fits in 000-377), or three bytes fit in four octals (FFFFFF equals 77777777). Which is why I chose to split on six digits in my approach.

于 2012-06-22T15:09:51.367 回答
1
Byte[] data = { 116, 4, 228, 18, 189, 145, 31, 7, 123, 74, 174, 151, 54, 144, 224, 49, 210, 169, 43, 213};
var str = String.Join("", data.Select(b => Convert.ToString(b,8).PadLeft(3,'0')));

-

//First hex string to byte array
string hex = "7404E412BD911F077B4AAE973690E031D2A92BD5";
List<byte> buf = new List<byte>();
for (int i = 0; i< hex.Length / 2; i++) 
     buf.Add(Convert.ToByte(hex.Substring(i * 2, 2), 16));

//Then to octal as above
var str = String.Join("", buf.Select(b => Convert.ToString(b,8).PadLeft(3,'0')));
于 2012-06-22T14:53:14.463 回答
0

I think I get your problem. You are sending a hexvalue to the aforesaid method,something like 0A45D and when Convert.ToInt32 method is getting that value it's failing to parse. And again your values are larger for an Int32 structure. So simply take your number(the hex representation), split it into 2 smaller string using any technique and then pass them separately to the above method.Get the strings and add them up. Something like following:

public static string BigHexToOct(string number)
{
    string a= "first splitted part from number";
    string b= "second splitted part from number";
    string result= this.hex2Octal(a)+this.hex2Octal(b);
    return result;

}

Try it and let us know.

You can try this way ; take a small part of your given string at first , see if it's converted successfully. If it is, then increase the length of string that the hex2octal method can convert successfully and this way you will know how you can divide your big number string.

于 2012-06-22T15:03:42.840 回答