开发者

Convert Int to Hexdecimal without using 'printf' (C Programming)

开发者 https://www.devze.com 2023-03-30 04:24 出处:网络
Is it possible to Convert Int to Hexdecimal without using \'printf\'? Best if the all the value are placed in the variabl开发者_运维问答e itself and some sample code with explanation.The decimal and

Is it possible to Convert Int to Hexdecimal without using 'printf'?

Best if the all the value are placed in the variabl开发者_运维问答e itself and some sample code with explanation.


The decimal and hexadecimal systems are just ways of expressing the value of the int. In a way "it is already a hexadecimal".


I think you can use itoa in stdlib.h :

char * itoa ( int value, char * str, int base ); or sprintf(str,"%x",value);

The documentation : itoa documentation


Of course it is possible. Just think about how printf itself was originally implemented...

I won't give you a full solution, only hints, based on which you can experiment with the implementation in code:

An 8-bit number can be expressed as 2 hex digits, which contain the higher and lower 4 bits, respectively. To get these bits, you can use the bit-shift (>>) and mask (&) operators.

Once you have a 4-bit value, you can easily map it to the correct hex digit using a sequence of ifs, or (as a more elegant solution) by indexing into a character array.


Hexdecival vs Decimal vs Binary..etc.. are only different bases that represent the same number. printf doesn't convert your number, it generates an hexdecimal string representation of your number. If you want to implement your own study how to make a conversion between decimal and hexdecimal bases.


Yes, it is definitely possible to convert an integer to a hexadecimal string without using the "printf" family of formatting functions.

You can write such a function by converting the number from base-10 (as we think about it) to base-16 (hexadecimal) and printing the digits in that representation (0-9A-F). This will require you to think a lot about the bases we use to represent numbers.


If you are referring to displaying an int as a hexadecimal number in C, than you will have to write a function that does the same thing as printf.

If you are referring to casting or internal representation, it can't be done because hexadecimal is not a data type.


An int is stored in your computer as a binary number. In fact, since hex can be interpreted as a shorthand for writing binary, you might even say that when you print out a decimal number using printf, it has to be converted from hex to decimal.


it's an example for convert a char array to hex string format

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

unsigned char d=255;
char hex_array[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char *array_to_hex_string(uint8_t data[],int size);
char test_data[3] = {'0',188,255};
int main()
{

    printf("%s",array_to_hex_string(test_data,3));
    return 0;
 }

 char *array_to_hex_string(uint8_t data[],int size){
 int i,j;
 char *hex_string = (char *)malloc((2*size) * sizeof(data[0]));
for(i=0;i<size;i++){
    hex_string[j] = hex_array[(data[i]>>4)];
    hex_string[j+1] =  hex_array[(data[i] & 15)];

    j +=2;
        }
return (char *)hex_string;
}


cout << hex << intvar << endl;

But if you want an answer that gives you an A for your homework, you're not going to get lucky :)

0

精彩评论

暂无评论...
验证码 换一张
取 消