Convert any binary into decimal

Source: Internet
Author: User

# Include <algorithm> <br/> # include <iostream> <br/> # include <sstream> <br/> # include <string> <br/> # include <iomanip> <br/> # include <cmath> <br/> # include <ctime> <br/> using namespace STD; </P> <p> string bintodec (unsigned char * a, int N) <br/>{< br/> int size = (INT) (8 * n * log10 (2.0) + 3)/2, Count = 1; <br/> unsigned char * TB = new unsigned char [size], * t = Tb, * s = a + N; <br/> const unsigned int q = 100; <br/> unsigned int g, V, D; <br/> for (INT I = 0; I <size; ++ I) T [I] = 0; <br/> * t = * -- S; <br/> for (int w = 0; W <n-1; ++ W) <br/>{< br/> T = Tb; <br/> G = 0; <br/> for (INT I = 0; I <count; ++ I) <br/>{< br/> V = * t; <br/> * t = (unsigned char) g; <br/> G = V x 0x100; <br/> d = g % Q; <br/> G/= Q; <br/> D + = * t; <br/> * t = (unsigned char) (D % Q ); <br/> G + = D/Q; <br/> + + T; <br/>}< br/> while (G> 0) <br/>{< br/> + + count; <br/> * t = (unsigned char) (G % Q); <br/> G/= Q; <br/> + + T; <br/>}< br/> T = Tb; <br/> G = * -- S; <br/> while (G> 0) <br/> {<br/> G + = * t; <br/> * t = (unsigned char) (g % Q); <br/> G/= Q; <br/> + + T; <br/>}< br/> If (t-TB> count) count = T-TB; <br/>}< br/> T = Tb; <br/> ostringstream ostream; <br/> ostream <(INT) T [-- count]; <br/> for (INT I = -- count; I> = 0; -- I) <br/> {<br/> ostream <setfill ('0') <SETW (2) <(INT) T [I]; <br/>}< br/> Delete [] TB; <br/> return string (ostream. STR (); <br/>}</P> <p> string bintodec (unsigned int * a, int N) <br/> {<br/> int size = (INT) (32 * n * log10 (2.0) + 10)/9, Count = 1; <br/> unsigned int * TB = new unsigned int [size], * t = Tb, * s = a + N; <br/> const unsigned _ int64 q = 1000000000; <br/> unsigned _ int64 g, V, D; <br/> for (INT I = 0; I <size; ++ I) T [I] = 0; <br/> * t = * -- S; <br/> for (INT W = 0; W <n-1; ++ W) <br/>{< br/> T = Tb; <br/> G = 0; <br/> for (INT I = 0; I <count; ++ I) <br/>{< br/> V = * t; <br/> * t = (unsigned INT) g; <br/> G = V x 0x100000000; <br/> d = g % Q; <br/> G/= Q; <br/> D + = * t; <br/> * t = (unsigned INT) (D % Q); <br/> G + = D/Q; <br/> + + T; <br/>}< br/> while (G> 0) <br/>{< br/> + count; <br/> * t = (unsigned INT) (G % Q); <br/> G/= Q; <br/> ++ T; <br/>}< br/> T = Tb; <br/> G = * -- S; <br/> while (G> 0) <br/>{< br/> G + = * t; <br/> * t = (unsigned INT) (G % Q ); <br/> G/= Q; <br/> + + T; <br/>}< br/> If (t-TB> count) Count = T-TB; <br/>}< br/> T = Tb; <br/> ostringstream ostream; <br/> ostream <t [-- count]; <br/> for (INT I = -- count; I> = 0; -- I) <br/>{< br/> ostream <setfill ('0 ') <SETW (9) <t [I]; <br/>}< br/> Delete [] TB; <br/> return string (ostream. STR (); <br/>}</P> <p> string bintohex (unsigned int A [], int N) <br/>{ <br/> ostringstream ostream; <br/> ostream <setfill ('0') <pex <uppercase; <br/> for (INT I = n-1; I> = 0; -- I) <br/>{</P> <p> ostream <SETW (8) <A [I] <"; <br/>}< br/> return string (ostream. STR (); <br/>}</P> <p> unsigned int getrand () <br/>{< br/> unsigned int r = 0; <br/> for (INT I = 0; I <4; ++ I) <br/>{< br/> r <= 8; <br/> r + = rand () & 0xff; <br/>}< br/> return R; <br/>}</P> <p> int main () <br/> {<br/> unsigned int C [8]; <br/> cout <rand_max <Endl; <br/> srand (unsigned) Time (null); <br/> for (INT I = 1; I <= 20; ++ I) <br/>{< br/> int W = rand () % 8 + 1; <br/> for (Int J = 0; j <W; ++ J) C [J] = getrand (); <br/> cout <"[0x" <bintohex (C, W) <"]" <Endl; <br/> cout <bintodec (C, W) <Endl; <br/> cout <bintodec (unsigned char *) c, w * 4) <Endl; <br/> cout <"-------------------------------------------/N"; <br/>}< br/> cin. get (); <br/>}< br/>

 

Running result:

32767
[0x16c2e381 5fda8a87 18ef4231 dbeb4601 7df900ea df2735cb f8427328 22ecbd29]
10295221183747278936921747630648458603400467941382508862457972601553739889961
10295221183747278936921747630648458603400467941382508862457972601553739889961
-------------------------------------------
[0xab58cea6]
2874724006
2874724006
-------------------------------------------
[0xd8e3ae78 749d9738 527a5b97 8e56d9ab]
288295437159170548130528295295705864619
288295437159170548130528295295705864619
-------------------------------------------
[0x7ed5d8ea 957af148 109c4492]
39253636795536994516232586386
39253636795536994516232586386
-------------------------------------------
[0x7e44acb9]
2118429881
2118429881
-------------------------------------------
[0xb3cf4437 2329f98d 16fda-a3 8735a78e 8f8f08df]
1026531544631277579698356444072288962943657117919
1026531544631277579698356444072288962943657117919
-------------------------------------------
[0xec10a6cf 15fc7090 765a961a 9ab5eaac 50d77fbb]
1347693164944741772319685567401488555807581110203
1347693164944741772319685567401488555807581110203
-------------------------------------------
[0x93409c91 1fb5b4a1 7cab6d68 1060b908]
195731997933124300396688728051259848968
195731997933124300396688728051259848968
-------------------------------------------
[0xffc2f74d 5dd8e3ba acbb86ab 4b010ec7 c9aa6c6b 757941e1 28950cde 51629340]
115684251536446752552975103355391847133603818796589198375366649571388940260160
115684251536446752552975103355391847133603818796589198375366649571388940260160
-------------------------------------------
[0xee17ffcd 0bdb21de 489c3758 2fac7313]
316480874084447775187743231313921733395
316480874084447775187743231313921733395
-------------------------------------------
[0xeb8d800e 69fc3d7b f310afdb bf55a476 5172958a e4e47ae5]
5775736262161366909404927190742068809748619099643863923429
5775736262161366909404927190742068809748619099643863923429
-------------------------------------------
[0xfbc690dc dbfb33f0 524fcb31 d8cde4ee]
334667239885263361753438158052404356334
334667239885263361753438158052404356334
-------------------------------------------
[0x554a800e 9076246b 772e71e2 1600add7]
113371206911581635703420990498392813015
113371206911581635703420990498392813015
-------------------------------------------
[0x806d563f f06d2df3 a84e2ada b3e4043f ed8cce59 dc9eb219 d416ee37]
13524951930786778559908590832244395085965417732637930789552908594743
13524951930786778559908590832244395085965417732637930789552908594743
-------------------------------------------
[0x04e64de3 fe9140e8 7c2806b6 4c74b4bb d373ba5a ba5beec0 dbccd805]
515990844093674373181415759063953427605276408747587940375251965957
515990844093674373181415759063953427605276408747587940375251965957
-------------------------------------------
[0x9511dfdf e05371e4 7441704e 26c8c359 1fdd3f77 19c5c100 d8a68c1c]
15698884605461649141017228684458001364515195638821207912644490988572
15698884605461649141017228684458001364515195638821207912644490988572
-------------------------------------------
[0x2a70da56 6b2132d1 2d4e10c4 f222eafe 88e5d178]
242294315721853140782625861798340006710457258360
242294315721853140782625861798340006710457258360
-------------------------------------------
[0xa9f8a38d 2dae7ec8 f513d2ff 11ed82f6 cf3bbf8b 9da6bf8b 2229bdb6 9a02330e]
76880178273454902187917754641693824869993386165973485184702729854214444561166
76880178273454902187917754641693824869993386165973485184702729854214444561166
-------------------------------------------
[0x402a2011]
1076502545
1076502545
-------------------------------------------
[0xcf032c90 ad09fe31 33da20ce ef3ee956 9295ea00]
1181831873967380537024460566483337911861652089344
1181831873967380537024460566483337911861652089344
-------------------------------------------

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: info-contact@alibabacloud.com 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.