0

im writing a code for converting a number to hexadecimal and im getting a random numbers as result. at first i succeed to convert the number but it was in a reversed order (the first mod needs to be the last number or letter). the code is a part (for cases of %x) from a big project that is kind of implementation of sprintf (so sprintf or printf are not allowed obviously). so the buffer is for composing a string without any placeholders. thank u in advance.

here's my code:

int num = *(int*)ptrs[counter];
int tempnum=num;
int mod=0;
int length =0;
for(int i=0;tempnum !=0;i++)
{
    length++;
    tempnum /= 16;
}
int array[length];
for(int i= length; i>0;i--)
{
    mod = num%16;
    num = num/16;
    array[i] = mod;
}
for(int i=0;i<length;i++)
{
    if(array[i]<10)
        *buffer = array[i]+ '0';
    else
        *buffer = array[i] -10 + 'a';
    buffer++;
}

12
  • I guess you mean array[i] + 'a' Commented May 23, 2020 at 17:39
  • 1
    What is *buffer = array[i] + '87'; doing? I believe it should be *buffer = array[i] - 10 + 'a'; or *buffer = array[i] - 10 + 'A'; Commented May 23, 2020 at 17:42
  • 1
    This for is wrong: for(int i= length; i>0;i--), it should be for(int i= length-1; i>=0;i--) Commented May 23, 2020 at 17:51
  • 1
    Many problems with this code. Sample code to form a string of any base TO_BASE(some_unsigned, base). Commented May 23, 2020 at 18:34
  • 1
    marksman123 Note that "%x" is for unsigned, not int. Commented May 23, 2020 at 19:33

3 Answers 3

2

The for loop of calculating array[i] should change to:

    for(int i = length-1; i>=0;i--) // i from (length - 1) to 0 instead of from length to 1.
    {
        mod = num%16;
        num = num/16;
        array[i] = mod;
    }

You do not need to change the buffer pointer. You can use:

    for(int i=0;i<length;i++)
    {
        if(array[i]<10)
            buffer[i] = array[i]+ '0';
        else
            buffer[i] = array[i] + 55;
    }

then do not forget at the null character at the end of buffer:

buffer[length] = '\0';

I do not see the declaration of buffer in your code, so i propose the solution above for the declaration:

 char buffer[length+1];
Sign up to request clarification or add additional context in comments.

8 Comments

If this is an implementation of printf it may be convenient to increment buffer, as the string may contain more than just the hexadecimal integer, and without incrementing the buffer it would need a counter variable to keep track of the characters in the string. Also it may not be the end of the string right after the number, as more text may be added.
@isrnick I mentioned in my answer that i propose the solution for the declaration of buffer as char buffer[length+1]; because i do not see any declaration of buffer in the OP's code.
Note code will fail to provide correct results when num < 0.
When num == INT_MIN, what value do you think num has after if(num<0) {num = -num;}? IAC, -INT_MIN is UB.
@chux-ReinstateMonica i got it. it seems that unsigned is better. Thank for your comment
|
1
for(int i= length; i>0;i--)
                {
                    mod = num%16;
                    num = num/16;
                    array[i] = mod;
                }

mistakes at array[i] to array[i-1].

buffer = array[i] + '87';

I suggest you use:

buffer = array[i]-10 + 'a';

2 Comments

It is a start, yet OP's code with these fixes fails when num <= 0.
I he can't check for it , he can ask for help again :)
0
printf("%i", (int)[any hexadecimal number]);

would print the integer form of this '[any hexadecimal number]' number

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.