You are using a version without Ads of this website. Please, consider donating:

Hi
my program is converter to hexadecimal number and it works fine but there is one mistake. I need code to make the output of convert function to print backwards. so it would be indeed hexadecimal number. please help.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````#include using namespace std; void convert(int num); int main() { int num; cout <<"insert the number you want to convert"<> num; cout << "you inserted number "<

and yes i know that i could use
 `cout <
but i wanted to do it on my own. and i am stuck now. and also so many if statements don t look so good. any idea how to make it shorter??
Here's one way to do it.

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include using namespace std; void convert(int num); int main() { int num; cout <<"insert the number you want to convert"<> num; cout << "you inserted number "<

<almost duplicate post, deleted>

@mirec: TODO: modify cire's code to
a. take care of `num` being negative
b. take care of `num` being originally zero.
Last edited on
ok i am confused now. when it get to convert function and to line 29 it should start convert function all over again and it never gets to cout<< hexChar[num%16] ... so why it works?? and why it print out the last num%16 first?? in cire s code ..not in this below.

and my second question is if the negative hexadecimal numbers are the same as positives just with negetive sign infront of it

and this is what i have done:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include #include using namespace std; void convert(int num); void convert2(int num); int main() { int num; cout <<"insert the number you want to convert"<> num; cout << "you inserted number "<0 ) convert(num); if (num == 0) cout << "0"; if (num < 0) convert2(num); cout << "\n" ; system ("pause"); return 0; } void convert (int num) { static char const hexChar[] = "0123456789ABCDEF" ; if ( num !=0 ) { convert( num/16 ) ; cout << hexChar[num%16] ; } } void convert2(int num) { // I don t know what value is negetive hexadecimal. }``````

and yeah thank you guys.. both of you...
Last edited on
> it should start convert function all over again
> and it never gets to cout<< hexChar[num%16] ... so why it works??

It would get to `cout<< hexChar[num%16]` when the called function returns.
See: http://www.cprogramming.com/tutorial/lesson16.html

> if the negative hexadecimal numbers are the same as positives just with negetive sign infront of it

Typically, when a hexadecimal number is printed, each hex digit is a representation of the value of four bits. We make no distinction betwen a sign bit and other value bits. For instance, a 32-bit signed integer with one sign bit and 31 digit bits is treated as eight four-bit values. In other words, the signed int is treated as an unsigned int.

In printing out the hexadecimal literal, `hexChar[num%16]` is a technical error when `num` is a signed integer. It leads to undefined behaviour when num is negative. What would `-17%16` yield?
 ``12345678910`` ``````#include int main() { // what does this print out? std::cout << std::hex << -17 << '\n' ; // and what would this print out? std::cout << (-17/10) << (-17%16) << '\n' ; }``````

The simplest solution is to just make the function interpret the number as an unsigned value:
 ``123456789101112131415161718192021222324252627`` ``````#include void print_hex( unsigned int num ) ; int main() { int num ; while( std::cout << "number? " && std::cin >> num ) { std::cout << "decimal: " << num << " hex: 0x" ; if( num == 0 ) std::cout << '0' ; print_hex(num) ; std::cout << '\n' ; } } void print_hex( unsigned int num ) { static constexpr char digits[] = "0123456789abcdef" ; if ( num != 0 ) { print_hex( num/16 ) ; std::cout << digits[ num%16 ] ; } }``````
Last edited on
thanks JLBorges
so ok.` i tryed cout<< hex <<-17;` and it gave me some fff crap and the other one some letters i never even set before.

but I finished it of like this.. what do you think?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include #include using namespace std; void convert(int num); void convert2(int num2); int main() { int num; cout <<"insert the number you want to convert"<> num; cout << "you inserted number "<0 ) convert(num); if (num == 0) cout << "0"; if (num < 0) { num-=2*num; unsigned int num2 = num; cout <<"-"; convert2(num2); } cout << "\n" ; system ("pause"); return 0; } void convert (int num) { char hexChar[] = "0123456789ABCDEF" ; if ( num !=0 ) { convert( num/16 ) ; cout << hexChar[num%16] ; } } void convert2(int num2) { char hexChar[] = "0123456789ABCDEF" ; if ( num2 !=0 ) { convert( num2/16 ) ; cout << hexChar[num2%16] ; } }``````

whenever i didn t use my line 21 and 22 it didn t work it changes it to some huge number every time. but like this it works.. :)

and if i may ask can you guys give me some assignement to do for this work or something totaly else so i can keep improving myself?? thx
> i tryed `cout<< hex <<-17; ` and it gave me
some fff crap and the other one some letters i never even set before.

`std::cout << std::hex << -17 << '\n' ;` will print out something like ffffffef.
The same as what `std::cout << std::hex << unsigned(-17) << '\n' ;`
or `std::cout << std::hex << -17U << '\n' ;` would print out.

In general, avoid using the modulo operator on numbers that could possibly be negative. The rules are different in different programming languages, and what you learn in one would have to be unlearned in another.
See http://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation

If you want to print out negative values with a minus sign prefixing the positive value:

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include void print_hex( unsigned int num ) ; int main() { int num ; while( std::cout << "number? " && std::cin >> num ) { std::cout << "decimal: " << num << " hex: " ; if( num == 0 ) std::cout << '0' ; else if( num < 0 ) // negative { std::cout << '-' ; // prefix a minus sign num = -num ; // drop the minus sign } print_hex(num) ; // invariant: num is non-negative std::cout << '\n' ; } } void print_hex( unsigned int num ) { static constexpr char digits[] = "0123456789abcdef" ; if ( num != 0 ) { print_hex( num/16 ) ; std::cout << digits[ num%16 ] ; } }``````
ok so basicaly is the same thing as i did. I just didn t know what u can drop the minus sign like that.. and i did it different way... but it works.. :)

ok thanks JLBorgers
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: