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

    }
8
  • 1
    Note that your number is too long for an Int32 (max 0xFFFFFFFF), and even Int64 (max 0xFFFFFFFFFFFFFFFF). So, the below solutions won't work... Commented Jun 22, 2012 at 14:38
  • yes exactly so i was looking for the solution Commented Jun 22, 2012 at 14:39
  • 1
    @user175084 - If you have to use a value larger then the Int64 structure then you will have to create your own. The simplest solution would be to handle them as two seperate values that can fit within an Int64 and simply combine the two strings together. Commented Jun 22, 2012 at 14:42
  • 1
    @user175084 that won't be enough. Because 0xFF is 0377 (octal), you'll need to do more than taking two characters and converting them. Commented Jun 22, 2012 at 15:03
  • 1
    @user175084 it seems you have missed my post. The number you mention is incorrect. Check my post (or my comment here) to see how you can get the correct value. Try typing 2BD5 in Windows Calc, you'll see 25725. This is not equal to your 53325. Remember, three hex nibbles (FFF) equal four octal nibbles (7777). You tried it with two nibbles (FF) which leave you a rest value (377). Commented Jun 22, 2012 at 15:44

5 Answers 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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I had already looked at this.. but i keep getting errors for the hex value i have in the example.. please can you check.. thanks
@user175084 - The code supplied does exactly what it should do. The code is actually the answer to your question. You simple have an entirely seperate problem the value you are trying to convert is to large for an Int64 which is a problem.
@user175084 you get errors because your hexvalue is too large. There are currently a couple of solutions that should work with arbitrary length hex values, have a look (but they borrow from this solution as well).
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;
   }

9 Comments

hahaha....seems Gaz found it before me.... ;) But hope that your problem is solved. @user
Additional non-parsable characters are at the end of the string.
@user175084 - Please update your question to explain your "non-parsable" character comment.
look we can't figure out your problem unless we see your some kind of log or exception or error that's shown in your console.Show it here. At least show us the input you have given which caused error and the details of the error.
I wonder why they did not implement it for any base. The docs say the base must be 2, 8, 10 or 16
|
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.

2 Comments

i thought the answer was Octal value: 164434422275221377173112256227662203406132225153325 please can you verify this value?? thanks a lot man
@user175084: your answer is incorrect. See my response to your earlier comment under the question. Just type the last four hex digits in Windows Calc, that should be enough proof to you and also show you that my answer is the correct one. If you need more explanation as to why, let me know.
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')));

15 Comments

This converts the bytes to an octal string. But { 43, 213} is 0x2BD5, is octal 25725, but your solution gives 053325 (see calc.50x.eu).
hey guys please don't go this way.. I want to convert Hex to octal.. that byte array is just for my testing. Thanks @Able you are right.
@Abel It works just like BitConverter.ToString(Which takes bytes one by one and convert to hex). What you do is converting the byte array to int then getting octal form of it. This aproach wouldn't work if your byte array's length is say 100.
@user175084 you may try String.Join("", buf.Select(b => Convert.ToString(b, 8).PadLeft(3, '0')).ToArray()) . My guess String.Join excepted IEnumerable as argument is new with .Net 4.0
@user175084 if you remove PadLeft you will get 164434422275221377173112256227662203406132225153325 as you commented. But you won't be able to parse it back to byte array.
|
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.

1 Comment

This would only work if you first split the string into correct parts (you write "using any technique", but it should be, cut them up from the right side, in parts divisible by three or six), otherwise just concatenating will give you incorrect values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.