Decimal to hexadecimal code in c language

C language decimal to hexadecimal conversion?

In C, you can convert a decimal number to hexadecimal using the formatting controller of the `printf()` function. Specifically, the `%x` formatting controller can be used. For example:

“`c

intdecimal_number=255;

printf(“%x”,decimal_number);

“`

The above code converts the decimal number 255 to hexadecimal form and outputs the result, ff.

< p>It should be noted that when using the `%x` format control character, the output hexadecimal number mother is automatically converted to lowercase form. If you need to output letters in uppercase, you can use the `%X` format control.

Programming in c to convert decimal to hex

Method 1:

#include<iostream>

#include<string>

usingnamespacestd;

stringDecIntToHexStr(longlongnum)

{ stringstr; longlongTemp=num/16; intleft=num%16; if(Temp>0) str+=

DecIntToHexStr(Temp ); if(left<10) str+=(left+’0′); else str+=(‘A’+left-10); returnstr;}

stringDecStrToHexStr(stringstr){ longlongDec=0; for(inti=0; i<str.size();++i) Dec=

Dec*10+str[i]-‘0’; returnDecIntToHexStr(Dec);}intmain()

{ stringDecStr; while(cin>>DecStr) { cout<< “0x “+DecStrToHexStr(DecStr); } return0;}

Method 2:

#include<iostream>

#include<string>

#include<string>

# include<vector>

#include<sstream>

#include<math.h>

usingnamespacestd;intmain()

{ stringdecStr; cin>>decStr; intnum=stoi(decStr); vector<int>v; while(num) { intbit=num%2; v.push_back(bit); num=num/2; }

reverse(v.begin(),v.end()); stringhexStr; if(v.size()%4==0) { for(inti=0;i<v.size()/4;++i) { inttemp=0,k=3; for(intj=i*4;j<(i+

< p>1)*4;++j) { if(v[j]==1) temp+=pow(2,k); –k; } if(temp<=9) hexStr+=temp; else hexStr+=(‘A’+temp-10); } }

else { intzeroNum=4-v.size( )%4; for(inti=0;i<zeroNum;++i) v.insert(v.begin(),0); for(inti=0;i<v.size()/4;++i) { inttemp=0,k=3; for

(intj=i*4;j<(i+1) *4;++j) { if(v[j]==1) temp+=pow(2,k); –k; }

if(temp<=9) { stringstreamss; stringstrTemp;

ss<<temp; ss>>strTemp. hexStr+=strTemp; } else hexStr+=(‘A’+temp-10); } } cout<<hexStr<<endl; return0;}

Extensions:

There are 3 other ways to implement this, two of which are using system functions, and one of which is to write it directly yourself.

The following code is the realization of the 3 ways, including 2-bit integer and any integer (2 to the 31st power) can be. You can choose your own implementation of the desired way.

Use of system functions are

1. char * itoa (intvalue, char * str, intbase); value is to be converted into a number, str is converted to a string stored in the location, base is the number of binary (but this function is not a standard C function, some compilers are not supported!) . So the code can be:

charbuffer[33];//used to store the converted hexadecimal string, you can define the length as needed char*inttohex(intaa){ itoa(aa,buffer,16); return(buffer);}

2.sprintf( str,”%x”,value);str is the location of the conversion store, %x means hexadecimal format, value is the number to be converted. So the code can be:

charbuffer[33];//used to store the converted hexadecimal string, you can define the length as needed char*inttohex(intaa){ sprintf(buffer,”%x”,aa); return(buffer);}

3. yourself Write

If the argument is just a two-digit integer, then it’s simple.

The code is as follows:

#include<stdio.h>#include<stdlib.h>charbuffer[33];//for storing converted hexadecimal strings, which can be defined to be as long as needed char*inttohex(intaa)

{ sprintf(buffer,”%x”,aa); return(buffer);}intmain()

{ intnum; char*hex_str; printf(“Enteranumber:”); scanf(“%d”,&num); hex_str=inttohex(num); printf(“Hexadecimalnumber:%sH\n”,hex_str); return0;}

C语言中十进制转化为十六进制的代码?

#include”stdio.h”#defineMAX50main(){intn,a[MAX],i=0,sign=0;clrscr();printf(“Pleaseinputthedecimalnum:”);scanf(“%d”,&n);if(n==0){printf(“HEX=%d”,n);printf(“\n\n\nPressanykeytoexit…”);getch();exit(0);}if(n<0){sign=1;n=-n;}while(n!=0){a[i++]=n%16;n=n/16;}printf(“HEX=%cx”,sign?’-‘:”);

for(;i>0;i–){switch(a[i-1]){case10:printf(“a”);break;case11:printf(“b”);break;case12:printf(“c”);break;case13:printf(“d”);break;case14:printf(“e”);break;case15:printf(“f”);break;default:printf(“%d”,a[i-1]);}}printf(“\n\nPressanykeytoexit…”);getch();}

Seek function to convert decimal number to hexadecimal in c

Function to convert decimal number to hexadecimal in c:

#include<stdio.h>

main()

{

intu10;

charu16[10];.

intw=0,a,b,i;

printf(“Please enter a number –>”);

scanf(“%d”,&u10);

if(u10==0)

{

u16[0]=’0′;

w ++;

}

else

{

a=u10;

while(a)

{

b=a%16;

if(b<10)

{

u16[w]=’0’+b;

}

else

{

u16[w]=’A’+b-10;

}

a=a/16;

w++;

}

}

}

printf(“\n”);

printf(” %d(10) converted to hexadecimal numbers is:”,u10);

for(i=w-1;i>=0;i–)

{

printf(“%c”,u16[i]);

}

printf(“\n”);

Programming in C to implement the conversion of decimal values to hexadecimal strings

Based on the rules of conversion from decimal to hexadecimal, write a C program to convert a decimal number to a hexadecimal number.

The specific implementation method can be referred to the following program:

#include<stdio.h>voidmain(){

inti,x;//x indicates the number of decimal numbers that want to be converted

intdata[10],index=0;//data array stores converted hexadecimal number, index indicates the subscript

scanf(“%d”,&x);//accepts the number of hexadecimal number to be converted

for(i=0;i<10;i++)//performs the initial assignment of data to the array data

data[i]=0;printf(“%d corresponding to hexadecimal is:”,x);//repeatedly divide to get the remainder and store the remainder in the data array

while(x>0){

data[index]=x%16;x=x/16;index++;

}/// Reverse order outputs the elements of the data array, that is, the decimal number corresponding to the hexadecimal number for(i=index-1;i>=0;i–)printf(“%d”,data[i]);printf(“\n”);

}