3

I have an int array that represents a very large number such as:

// ...
unsigned int n1[200];
// ...

n1 = {1,3,4,6,1,...} ==> means my number is 13461...

How can I convert that large number to its hex value?

3
  • Do you mean the hex value as a string? Commented Mar 29, 2011 at 23:18
  • Where is the hexadecimal involved... Commented Mar 29, 2011 at 23:19
  • for example, my number is 55. that means it will be located in my int array like this : n1[0]=5 , n2[1]=5. now i want to find the hex value of 55 which is 37 (that s what i need). my number may be very large. if i made some mistakes when i was explaning, sorry about my english or explanation :) Commented Mar 30, 2011 at 0:23

3 Answers 3

1

So here is my take on the problem:

  1. You have an array of digits.
  2. You want to build an unsigned int from this array of digits.
  3. The array of digits could be either HEX digits, or DECIMAL digits.

To build this unsigned long long, assuming an array of DECIMAL digits:

unsigned long long myNum = 0;
unsigned int n1[200];

for (int i=0; i < n1.length ; i++ ){
    myNum += pow(10,i) * n1[n1.length - i];
}

To build this unsigned long long, assuming an array of HEX digits:

for (int i=0; i < n1.length ; i++ ){
    myNum += pow(16,i)* n1[n1.length - i];
}

(Notice the base 16)

Disclaimer: limited to exactly 16 digits MAX stored in your array. After that you will overrun the buffer

If it is just a matter of DISLAYING the number in the correct format...

Well, an int is an int is an int... (in memory).

There are 10 fingers on my hands whether or not I call that number 10, or A.

If you want to format the number for DISPLAY in hex, then try something like:

unsigned int i = 10;
//OR
unsigned int i = 0xA;

printf("My number in hex: %x", i);
printf("My number in decimal: %d", i);
Sign up to request clarification or add additional context in comments.

7 Comments

The ^ operator is actually the bitwise exclusive-or operator. You need to use something like pow() to perform that type of operation.
@dreamlax: Thanks for the catch. It was sort-of pseudo coded, but good call nonetheless.
Your solution shows how to represent a large hex number using a similar scheme to the OP's. However, conversion between the two schemes is different. Remember that 9 * pow(16,3) is not equal to 9 * pow(10, 3).
@Thomas: I believe that is the point. The two schemes are supposed to be different. He wants to rebuild a unsigned int from an array of digits. But he wants to imagine that the array of digits represent hex digits, not decimal.
The question says "very large number". I don't think passing a single unsigned int to printf will handle that.
|
0

I'm unsure if you want the hexadecimal represented as a string. If that's the case, here's some code:

#include <iostream>
#include <stack>
using namespace std;

string toHexa(int num){
    string digit = "0123456789ABCDEF", numStr = "";
    stack<char> s;

    do {
        s.push(digit[num%16]);
        num /= 16;
    } while (num != 0);

    while (!s.empty()){
        numStr += s.top();
        s.pop();
    }
    return numStr;
}

int main(){
    int num = 235; // EB in hexa
    cout << num << " to hexadecimal: " << toHexa(num) << endl;
    return 0;
}

2 Comments

I doubt that will work for a "very large number" as described in the question.
0

You could use the GMP library to make this relatively straightforward.

  • Use basic_stringstream<unsigned int> to wrap your array.
  • Use operator << to read it into a mpz_t variable.
  • Create another basic_stringstream<unsigned int> for your result.
  • Use std::hex and operator >> to write the variable back out in hexadecimal.

That would work on ASCII digits, but yours aren't. You can still use GMP, but you'll want to use the mpn_get_str and mpn_set_str functions instead. You'll need to copy your digits into an unsigned char[] and then you can specify the base for conversion to mp_limb_t and back to a string of digits.

Comments

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.