Decimal and hexadecimal strings are transferred to each other

Source: Internet
Author: User

voidStrdec2bufdec (Conststrval& Strdec, byte*Bufdec) {     for(size_t i =0; I < strdec.length (); ++i) {Bufdec[i]=0; if(Strdec[i] >= L'0'&& Strdec[i] <= L'9') Bufdec[i]= Strdec[i]-L'0'; }}intStrdec2hexbuf (Conststrval& Strdec, byte*destbuf) {size_t NIndex=0;  while(NIndex < Strdec.length () && (Strdec[nindex] <='0'|| Strdec[nindex] >'9'))        ++NIndex; if(NIndex >=strdec.length ())return 0; size_t Ndeclen= Strdec.length ()-NIndex; BYTE* Decbuf =Newbyte[ndeclen+1]; memset (Decbuf,0, ndeclen+1);    Strdec2bufdec (Strval (Strdec, NIndex), decbuf); BYTE* Hexbuf =NewByte[ndeclen]; memset (Hexbuf,0, Ndeclen); BYTE ntemp=0;  for(size_t Ndecindex =0; Ndecindex < Ndeclen; ++Ndecindex) {Ntemp=0;  for(intNbitindex =0; Nbitindex <8; ++Nbitindex) {             for(size_t nstartindex = Ndecindex; nstartindex < Ndeclen; + +Nstartindex) {Decbuf[nstartindex+1] + = (decbuf[nstartindex]&0x01) ==1?Ten:0; Decbuf[nstartindex]>>=1; } ntemp|= Decbuf[ndeclen] >0? (1<< Nbitindex):0; Decbuf[ndeclen]=0; } Hexbuf[ndecindex]=ntemp; }     while(Ndeclen >0) {        if(Hexbuf[--ndeclen] >0)             Break; }    if(Destbuf! =NULL) memcpy (Destbuf, hexbuf, Ndeclen+1); Delete[] decbuf; Delete[] hexbuf; returnNdeclen +1;}
View Code

The above is a 10 binary to 16 binary code, you can call the int strdec2hexbuf (const strval& STRDEC, byte* destbuf), convert the Strdec decimal data into a 16-binary destbuf string, and returns the effective length of the Destbuf

About the 10 binary to 16 binary method:

1. I will first convert the Strdec to a method represented by a number represented by each byte. And then make the appropriate conversions. void Strdec2bufdec (const strval& STRDEC, byte* Bufdec)

2, about the decimal to 16, I will convert the decimal into 2 binary way. That is, each number in addition to 2 of the way to take the remainder. Specific about the decimal turn 16 binary method A lot, I used to convert to 2 binary is just one of the methods.

#include <string>
typedef std::wstring Strval;

intCalcstep (byte* cur,intNcurlen) {    intNtemp =0;  for(intNIndex =0; NIndex < Ncurlen; ++NIndex) {Ntemp+ = Cur[nindex] * the; Cur[nindex]= ntemp%Ten; Ntemp/=Ten; }     while(Ntemp >0) {Cur[ncurlen+ +] = ntemp%Ten; Ntemp/=Ten; }    returnNcurlen;}voidMulonebyte (byte* Dec,intDatabyte*Base,intNbaselen) {     for(intNbaseindex =0; Nbaseindex < Nbaselen; ++Nbaseindex) {        intntemp = Data *Base[Nbaseindex];  for(intNdecindex = Nbaseindex; Ntemp >0; ++Ndecindex) {Ntemp+=Dec[ndecindex]; Dec[ndecindex]= ntemp%Ten; Ntemp/=Ten; }    }}voidHex2dec (byte* Hexbuf,intNhexlen,byte*decbuf) {    byte*Base=New byte[nhexlen*4]; memset (Base,0, nhexlen*4); Base[0] =1; intNcurbaselen =1;  for(intNhexindex =0; Nhexindex < Nhexlen; ++Nhexindex) {Mulonebyte (Decbuf, Hexbuf[nhexindex],Base, Ncurbaselen); Ncurbaselen= Calcstep (Base, Ncurbaselen); }}//////////////////////////////////////////////////////////////////////////Strval Convert2decdata (byte* srcdata,intNlen)    {Strval deststr; intNdeclen = nlen*4; BYTE* Decdata =NewByte[ndeclen]; memset (Decdata,0, Ndeclen);    Hex2dec (Srcdata, Nlen, Decdata);  while(ndeclen-->0) {        if(Decdata[ndeclen] >0)             Break;    } wchar_t Midbuf[max_path];  while(Ndeclen >=0) {swprintf_s (Midbuf, MAX_PATH, L"%d", decdata[ndeclen--]);    Deststr.append (MIDBUF); }    Delete[] decdata; returndeststr;}
View Code

The above code is a string that turns 16 binary into a 10 binary

in void Hex2dec (byte* hexbuf, int nhexlen, byte* decbuf) Here, I am also the result with each byte of decbuf representing a 0~9 number, where I new a buffer that is 4 times times larger than the hexadecimal buffer to hold the data. (Calculated 3 times times should be enough)

First call to void Mulonebyte (byte* dec, int data, byte* base, int nbaselen)

My base[] Initial value is 1, and then multiplied by each byte of hexbuf. Calculation results added to dec[]

The calcstep is multiplied by 256 for the value of the base buffer, in 10 binary notation.

Because Hex2dec, HEXBUF is calculated every time a byte is entered from low to high, it is necessary to multiply base by 256 after each calculation, and then adjust base to a 10 binary representation of each byte.


Strval the display of Convert2decdata (byte* srcdata, int nlen) functions. Because my computer is a small-end mode, it is printed out from the rear.

Add additional self-increment functions. Specific self-increment method can be modified

voidIncdata (byte*Val, INT64 Nincstep) {BYTE* Pincstep = (byte*) &Nincstep; intNleft =0;  for(intNIndex =0; NIndex <sizeof(INT64); ++NIndex) {         for(intNinner =0; Ninner <8; ++Ninner) {Nleft= Nleft + (Val[nindex] & (1<<ninner) + (Pincstep[nindex] & (1<<Ninner)); Val[nindex]&= ~ (1<<Ninner); Val[nindex]^= (Nleft & (1<<Ninner)); Nleft&=1<< (ninner+1); } nleft>>=8; }     for(intNIndex =sizeof(INT64); Nleft >0; ++NIndex) {         for(intNinner =0; Ninner <8&& nleft >0; ++Ninner) {Nleft= Nleft + (Val[nindex] & (1<<Ninner)); Val[nindex]&= Nleft & (1<< Ninner) &0xFF; Nleft&=1<< (ninner+1); } nleft>>=8; }}
View Code

Decimal and hexadecimal strings are transferred to each other

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.