Home > Back-end >  36 hexadecimal turn out of 10 mechanism problem
36 hexadecimal turn out of 10 mechanism problem

Time:09-29

Pseudo code is as follows:

 string key (" ZZZZZZZZZZZZZ ");//the string to save 36 hexadecimal 

Long long int TMP=0;
Int I=0, k=0;

For (I=key length () - 1; i>=0; I -) {
If (key [I] <- '9') {
K=key [I] - '0';
} else {
K=key [I] - 'A' + 10;
}
TMP TMP *=36 + k;
}


Code above there is a problem is 36 hexadecimal string key (" ZZZZZZZZZZZZZ "); For the decimal number is 170581728179578200000, more than the long long int
Range of storage, TMP will overflow, have good method to solve this problem, thank you very much???

CodePudding user response:

Decimal strings can also be used to save o

CodePudding user response:

reference 1st floor akari10032 response:
decimal strings can also be used to save o




If TMP does not overflow, with TMP deposit first, and then converted to strings, but don't know how to skip the TMP, save string directly

CodePudding user response:

There is a way of thinking, is to find 36 corresponding relation between binary and decimal, like three hexadecimal can completely into four 8 hexadecimal, then a period of transformation, joining together

CodePudding user response:

reference akari10032 reply: 3/f
there is a train of thought, is to find 36 corresponding relation between binary and decimal, like three hexadecimal can completely into four 8 hexadecimal, then a period of transformation, splicing



For (I=key length () - 1; i>=0; I -) {
If (key [I] <- '9') {
K=key [I] - '0';
} else {
K=key [I] - 'A' + 10;
}
TMP TMP *=36 + k;
}

Now look at a single character conversion relationship is such, but the overall conversion is recursive take 36 years plus tail, not a period of the transformation of the law

CodePudding user response:

If (key [I] <- '9') {
What is - '9'?

CodePudding user response:

reference 5 floor lin5161678 reply:
if (key [I] <- '9') {
What is - '9'?



Oh, writing is wrong, the if (key [I] <='9')

CodePudding user response:

 # include & lt; stdio.h> 
#include
#include
#include
#include

# define _max (a, b) ((a) & gt; (b)? (a) : (b))
# define _min (a, b) ((a) & gt; (b)? (b) : (a))

# define _swap (l, r) \
Do {\
Typeof __tmp=(l) (l); \
(l)=(r); \
(r)=__tmp; \
} while (0)

# define assert_condition (cond, code) \
{\
if (! (cond)) {\
Return (code); \
} \
}

The static inline int _convert (int) c {
Return isdigit (c)? C - '0' : tolower (c) - 'a' + 10;
}

The static char * reverse_str (char * STR, int len) {
If (len & lt; 2)
Return the STR;
_swap (STR [0], STR [] len - 1);
Reverse_str (STR + 1, len - 2);
Return the STR;
}

/* an additive alone, whether return to carry */
The static inline bool _one_bit_add (char char l, r, Boolean flag, char * res) {
* res=l + r + (flag? 1:0);/* flag: carry flag */
If (* res & lt; '5' + '5') {
* res -='0';
return false;
} else {
='0' * res - + 10;
return true;
}
}

The static char * _add (const char * l, const char * r) {
Int l_len=strlen (l);
Int r_len=strlen (r);
Int shorter=_min (l_len r_len);
Int longer=_max (l_len r_len);
Bool flag=false;
Const char * p=NULL;
Char * res=(char *) calloc (1, longer + 2);
Assert_condition (res!=NULL, NULL);

/* start bits together, will first short list, to add to a long list of all the rest of the long list of add 0 */
for (int i=0; i Flag=_one_bit_add (l [l_len - I - 1), r (r_len - I - 1), flag, & amp; Res [I]);
}
P=(l (shorter)? L: r) + longer - shorter - 1;
For (int I=shorter; i Flag=_one_bit_add (* p, '0', flag, & amp; Res [I]);
}

/* highest carry */
For (int I=longer; Flag; + + I) {
Flag=_one_bit_add (' 0 ', '0', flag, & amp; Res [I]);
}

/* because began writing bits, so need to reverse the results, the following multiplication similarly */
Return reverse_str (res, strlen (res));
}

The static char * _mul (const char * l, const char * r) {
Int l_len=strlen (l);
Int r_len=strlen (r);
/* two number multiplication, the length is the sum of two number length */
Int res_len=l_len + r_len;
Char * res=(char *) calloc (1, res_len + 1);
Char * p=res;/* * for mobile assignment/

Int x=0;/* every time the result of the multiplication a */
for (int i=0; i For (int j=0; J & lt;=I; + + j) {
Int bit1=l_len - I - 1 + j;
Int bit2=r_len - j - 1;
If (bit1 & gt;=0 & amp; & Bit2 & gt;=0) {
X +=[bit1] - '0' (l) * (r [bit2] - '0');
}
}
* p=10 + x % '0'.
X/=10;
}

/* if the result is 0, only show a 0 */
If (p & gt; Res & amp; & P [1]=='0') {
Res [1]='\ 0';
}

Return reverse_str (res, strlen (res));
}

The static char * _pow (int base, int the ex) {
Char * res=(char *) calloc (1, 2);
Res [0]='1'.
Char base_str [64]={0};
Sprintf (base_str, "% d", base);
for (int i=0; i Char * TMP=_mul (res, base_str);
Free (res);
Res=TMP;
}

return res;
}

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related