Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 969 | runge | 1 | |
| 2 | #include <string> |
||
| 3 | |||
| 4 | /*************************************************************************** |
||
| 5 | * Copyright (C) 2004 by Mattias Runge * |
||
| 6 | * mattias@mrunge.se * |
||
| 7 | * * |
||
| 8 | * This program is free software; you can redistribute it and/or modify * |
||
| 9 | * it under the terms of the GNU General Public License as published by * |
||
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
||
| 11 | * (at your option) any later version. * |
||
| 12 | * * |
||
| 13 | * This program is distributed in the hope that it will be useful, * |
||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
| 16 | * GNU General Public License for more details. * |
||
| 17 | * * |
||
| 18 | * You should have received a copy of the GNU General Public License * |
||
| 19 | * along with this program; if not, write to the * |
||
| 20 | * Free Software Foundation, Inc., * |
||
| 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
| 22 | ***************************************************************************/ |
||
| 23 | #include "tools.h" |
||
| 24 | |||
| 981 | runge | 25 | string niceTime() |
| 26 | { |
||
| 27 | string result; |
||
| 28 | struct tm tmStruct; |
||
| 29 | time_t t = time(NULL); |
||
| 990 | runge | 30 | localtime_r(&t, &tmStruct); |
| 981 | runge | 31 | |
| 32 | result += lpad(itos(tmStruct.tm_hour), 2, '0'); |
||
| 33 | result += ":"; |
||
| 34 | result += lpad(itos(tmStruct.tm_min), 2, '0'); |
||
| 35 | result += ":"; |
||
| 36 | result += lpad(itos(tmStruct.tm_sec), 2, '0'); |
||
| 37 | |||
| 38 | return result; |
||
| 39 | } |
||
| 40 | |||
| 969 | runge | 41 | unsigned int bin2uint(string bin) |
| 42 | { |
||
| 43 | unsigned int num = 0; |
||
| 44 | |||
| 45 | for (int n = bin.length()-1; n >= 0; n--) |
||
| 46 | { |
||
| 47 | if (bin[n] == '1') |
||
| 48 | num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n)); |
||
| 49 | } |
||
| 50 | |||
| 51 | return num; |
||
| 52 | } |
||
| 53 | |||
| 54 | string invert(string bin) |
||
| 55 | { |
||
| 56 | string inverted; |
||
| 57 | |||
| 58 | for (int n = 0; n < bin.length(); n++) |
||
| 59 | inverted += (bin[n] == '1' ? '0' : '1'); |
||
| 60 | |||
| 61 | return inverted; |
||
| 62 | } |
||
| 63 | |||
| 64 | float bin2float(string bin) |
||
| 65 | { |
||
| 66 | float num = 0; |
||
| 67 | |||
| 68 | if (bin.length() == 0) |
||
| 69 | return 0.0f; |
||
| 70 | |||
| 71 | bool isNegative = false;; |
||
| 72 | |||
| 73 | if (bin[0] == '1') |
||
| 74 | { |
||
| 75 | bin = invert(bin); |
||
| 76 | isNegative = true; |
||
| 77 | } |
||
| 78 | |||
| 79 | for (int n = bin.length()-1; n > 0; n--) |
||
| 80 | { |
||
| 81 | if (bin[n] == '1') |
||
| 82 | num += pow(2.0f, (int)(bin.length()-1-n)); |
||
| 83 | } |
||
| 84 | |||
| 85 | num /= 64.0f; |
||
| 86 | |||
| 87 | if (isNegative) |
||
| 88 | num = -num; |
||
| 89 | |||
| 90 | return num; |
||
| 91 | } |
||
| 92 | |||
| 93 | string bin2hex(string bin) |
||
| 94 | { |
||
| 95 | string hex; |
||
| 96 | |||
| 97 | while (bin.size() > 0) |
||
| 98 | { |
||
| 99 | string part = bin.substr(0, 4); |
||
| 100 | bin.erase(0, part.size()); |
||
| 101 | |||
| 102 | while (part.size() < 4) |
||
| 103 | part = "0" + part; |
||
| 104 | |||
| 105 | if (part == "0000") |
||
| 106 | hex += '0'; |
||
| 107 | else if (part == "0001") |
||
| 108 | hex += '1'; |
||
| 109 | else if (part == "0010") |
||
| 110 | hex += '2'; |
||
| 111 | else if (part == "0011") |
||
| 112 | hex += '3'; |
||
| 113 | else if (part == "0100") |
||
| 114 | hex += '4'; |
||
| 115 | else if (part == "0101") |
||
| 116 | hex += '5'; |
||
| 117 | else if (part == "0110") |
||
| 118 | hex += '6'; |
||
| 119 | else if (part == "0111") |
||
| 120 | hex += '7'; |
||
| 121 | else if (part == "1000") |
||
| 122 | hex += '8'; |
||
| 123 | else if (part == "1001") |
||
| 124 | hex += '9'; |
||
| 125 | else if (part == "1010") |
||
| 126 | hex += 'a'; |
||
| 127 | else if (part == "1011") |
||
| 128 | hex += 'b'; |
||
| 129 | else if (part == "1100") |
||
| 130 | hex += 'c'; |
||
| 131 | else if (part == "1101") |
||
| 132 | hex += 'd'; |
||
| 133 | else if (part == "1110") |
||
| 134 | hex += 'e'; |
||
| 135 | else if (part == "1111") |
||
| 136 | hex += 'f'; |
||
| 137 | } |
||
| 138 | |||
| 139 | return hex; |
||
| 140 | } |
||
| 141 | |||
| 142 | string float2bin(float num, int length) |
||
| 143 | { |
||
| 144 | string bin; |
||
| 145 | |||
| 146 | ///FIXME |
||
| 147 | |||
| 148 | while (bin.size() < length) |
||
| 149 | bin = "0" + bin; |
||
| 150 | |||
| 151 | return bin; |
||
| 152 | } |
||
| 153 | |||
| 154 | string hex2bin(string hex) |
||
| 155 | { |
||
| 156 | hex = strtolower(hex); |
||
| 157 | |||
| 158 | string bin; |
||
| 159 | |||
| 160 | for (int n = 0; n < hex.length(); n++) |
||
| 161 | { |
||
| 162 | switch (hex[n]) |
||
| 163 | { |
||
| 164 | case '0': bin += "0000"; break; |
||
| 165 | case '1': bin += "0001"; break; |
||
| 166 | case '2': bin += "0010"; break; |
||
| 167 | case '3': bin += "0011"; break; |
||
| 168 | case '4': bin += "0100"; break; |
||
| 169 | case '5': bin += "0101"; break; |
||
| 170 | case '6': bin += "0110"; break; |
||
| 171 | case '7': bin += "0111"; break; |
||
| 172 | case '8': bin += "1000"; break; |
||
| 173 | case '9': bin += "1001"; break; |
||
| 174 | case 'a': bin += "1010"; break; |
||
| 175 | case 'b': bin += "1011"; break; |
||
| 176 | case 'c': bin += "1100"; break; |
||
| 177 | case 'd': bin += "1101"; break; |
||
| 178 | case 'e': bin += "1110"; break; |
||
| 179 | case 'f': bin += "1111"; break; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | return bin; |
||
| 184 | } |
||
| 185 | |||
| 186 | string uint2bin(unsigned int num, int length) |
||
| 187 | { |
||
| 188 | string bin; |
||
| 189 | |||
| 190 | while (num) |
||
| 191 | { |
||
| 192 | bin = char((num&1)+'0') + bin; |
||
| 193 | num >>= 1; |
||
| 194 | } |
||
| 195 | |||
| 196 | while (bin.size() < length) |
||
| 197 | bin = "0" + bin; |
||
| 198 | |||
| 199 | return bin; |
||
| 200 | } |
||
| 201 | |||
| 981 | runge | 202 | string uint2hex(unsigned int num, int length) |
| 203 | { |
||
| 204 | return bin2hex(uint2bin(num, length)); |
||
| 205 | } |
||
| 969 | runge | 206 | |
| 207 | vector<string> explode(string delimiter, string str) |
||
| 208 | { |
||
| 209 | vector<string> parts; |
||
| 210 | string::size_type startPos = 0; |
||
| 211 | string::size_type endPos = 0; |
||
| 212 | |||
| 213 | while ((endPos = str.find(delimiter, startPos)) != string::npos) |
||
| 214 | { |
||
| 215 | parts.push_back(str.substr(startPos, endPos-startPos)); |
||
| 216 | startPos = endPos+delimiter.length(); |
||
| 217 | } |
||
| 218 | |||
| 219 | if (startPos < str.length()) |
||
| 220 | { |
||
| 221 | parts.push_back(str.substr(startPos, str.length()-startPos)); |
||
| 222 | } |
||
| 223 | |||
| 224 | return parts; |
||
| 225 | } |
||
| 226 | |||
| 227 | string strtoupper(string s) |
||
| 228 | { |
||
| 229 | for (unsigned int n = 0; n < s.size(); n++) |
||
| 230 | s[n] = (char)toupper(s[n]); |
||
| 231 | |||
| 232 | return s; |
||
| 233 | } |
||
| 234 | |||
| 235 | string strtolower(string s) |
||
| 236 | { |
||
| 237 | for (unsigned int n = 0; n < s.size(); n++) |
||
| 238 | s[n] = (char)tolower(s[n]); |
||
| 239 | |||
| 240 | return s; |
||
| 241 | } |
||
| 242 | |||
| 243 | string trim(string s) |
||
| 244 | { |
||
| 245 | return trim(s, ' '); |
||
| 246 | } |
||
| 247 | |||
| 248 | string trim(string s, char c) |
||
| 249 | { |
||
| 250 | while (s[0] == c) |
||
| 251 | s.erase(0, 1); |
||
| 252 | |||
| 253 | while (s[s.length()-1] == c) |
||
| 254 | s.erase(s.length()-1, 1); |
||
| 255 | |||
| 256 | return s; |
||
| 257 | } |
||
| 258 | |||
| 259 | int stoi(const string s) |
||
| 260 | { |
||
| 261 | istringstream in(s); |
||
| 262 | int i; |
||
| 263 | in >> i; |
||
| 264 | return i; |
||
| 265 | } |
||
| 266 | |||
| 267 | string itos(int i) |
||
| 268 | { |
||
| 269 | ostringstream out; |
||
| 270 | out << i; |
||
| 271 | return out.str(); |
||
| 272 | } |
||
| 273 | |||
| 981 | runge | 274 | unsigned int stou(const string s) |
| 275 | { |
||
| 276 | istringstream in(s); |
||
| 277 | unsigned int i; |
||
| 278 | in >> i; |
||
| 279 | return i; |
||
| 280 | } |
||
| 281 | |||
| 282 | string utos(unsigned int i) |
||
| 283 | { |
||
| 284 | ostringstream out; |
||
| 285 | out << i; |
||
| 286 | return out.str(); |
||
| 287 | } |
||
| 288 | |||
| 969 | runge | 289 | float stof(const string s) |
| 290 | { |
||
| 291 | istringstream in(s); |
||
| 292 | float f; |
||
| 293 | in >> f; |
||
| 294 | return f; |
||
| 295 | } |
||
| 296 | |||
| 297 | string ftos(float f) |
||
| 298 | { |
||
| 299 | ostringstream out; |
||
| 300 | out << f; |
||
| 301 | return out.str(); |
||
| 302 | } |
||
| 303 | |||
| 304 | string str_replace(string search, string replace, string str) |
||
| 305 | { |
||
| 306 | vector<string> parts = explode(search, str); |
||
| 307 | |||
| 308 | string result; |
||
| 309 | for (int n = 0; n < parts.size(); n++) |
||
| 310 | { |
||
| 311 | result += parts[n]; |
||
| 312 | |||
| 313 | if (n < parts.size()-1) |
||
| 314 | result += replace; |
||
| 315 | } |
||
| 316 | |||
| 317 | return result; |
||
| 318 | } |
||
| 319 | |||
| 320 | string file_get_contents(string filename) |
||
| 321 | { |
||
| 322 | ifstream srcfile(filename.c_str()); |
||
| 323 | |||
| 324 | if (!srcfile.is_open()) |
||
| 325 | { |
||
| 326 | srcfile.close(); |
||
| 327 | return ""; |
||
| 328 | } |
||
| 329 | |||
| 330 | string buffer = ""; |
||
| 331 | string line; |
||
| 332 | |||
| 333 | while (!srcfile.eof()) |
||
| 334 | { |
||
| 335 | getline(srcfile, line); |
||
| 336 | |||
| 337 | if (srcfile.eof()) |
||
| 338 | break; |
||
| 339 | |||
| 340 | buffer += line + "\n"; |
||
| 341 | }; |
||
| 342 | |||
| 343 | srcfile.close(); |
||
| 344 | |||
| 345 | return buffer; |
||
| 346 | } |
||
| 347 | |||
| 348 | bool file_exists(string filename) |
||
| 349 | { |
||
| 350 | ifstream file(filename.c_str()); |
||
| 351 | if (file.is_open()) |
||
| 352 | { |
||
| 353 | file.close(); |
||
| 354 | return true; |
||
| 355 | } |
||
| 356 | |||
| 357 | return false; |
||
| 358 | } |
||
| 359 | |||
| 360 | string escape(string in) |
||
| 361 | { |
||
| 362 | string out; |
||
| 363 | |||
| 364 | for (int n = 0; n < in.length(); n++) |
||
| 365 | { |
||
| 366 | switch (in[n]) |
||
| 367 | { |
||
| 976 | runge | 368 | case '\r': |
| 369 | break; |
||
| 370 | |||
| 969 | runge | 371 | case '\n': |
| 372 | out += "\\n"; |
||
| 373 | break; |
||
| 374 | |||
| 976 | runge | 375 | case '\'': |
| 376 | out += "\\'"; |
||
| 377 | break; |
||
| 378 | |||
| 379 | case '"': |
||
| 380 | out += "\\\""; |
||
| 381 | break; |
||
| 382 | |||
| 969 | runge | 383 | default: |
| 384 | out += in[n]; |
||
| 385 | break; |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | return out; |
||
| 390 | } |
||
| 391 | |||
| 976 | runge | 392 | string rpad(string in, int length, char c) |
| 393 | { |
||
| 394 | while (in.length() < length) |
||
| 395 | { |
||
| 396 | in += c; |
||
| 397 | } |
||
| 398 | |||
| 399 | return in; |
||
| 400 | } |
||
| 401 | |||
| 402 | string lpad(string in, int length, char c) |
||
| 403 | { |
||
| 404 | while (in.length() < length) |
||
| 405 | { |
||
| 406 | in = c + in; |
||
| 407 | } |
||
| 408 | |||
| 409 | return in; |
||
| 410 | } |
||
| 411 | |||
| 412 | |||
| 969 | runge | 413 | // FileTools |
| 414 | |||
| 415 | vector<string> FileTools::GetDirList(string path) |
||
| 416 | { |
||
| 417 | DIR *dir = opendir(path.c_str()); |
||
| 418 | vector<string> list; |
||
| 419 | |||
| 420 | if (!dir) |
||
| 421 | return list; |
||
| 422 | |||
| 423 | dirent *d; |
||
| 424 | while ((d = readdir(dir))) |
||
| 425 | { |
||
| 426 | if (d->d_name[0] == '.') |
||
| 427 | continue; |
||
| 428 | |||
| 429 | list.push_back(d->d_name); |
||
| 430 | } |
||
| 431 | closedir(dir); |
||
| 432 | |||
| 433 | return list; |
||
| 434 | } |
||
| 435 | |||
| 436 | string FileTools::GetUniqeFilename(string path, string prefix, string postfix) |
||
| 437 | { |
||
| 438 | time_t now; |
||
| 439 | time(&now); |
||
| 440 | |||
| 441 | string filename = path + prefix + itos(now); |
||
| 442 | |||
| 443 | int index = 0; |
||
| 444 | while (1) |
||
| 445 | { |
||
| 446 | string file = filename + itos(index) + postfix; |
||
| 447 | if (!Exist(file)) |
||
| 448 | break; |
||
| 449 | index++; |
||
| 450 | } |
||
| 451 | |||
| 452 | filename += itos(index) + postfix; |
||
| 453 | |||
| 454 | return filename; |
||
| 455 | } |
||
| 456 | |||
| 457 | bool FileTools::SaveFile(string filepath, string data) |
||
| 458 | { |
||
| 459 | ofstream destfile(filepath.c_str()); |
||
| 460 | if (!destfile.is_open()) |
||
| 461 | { |
||
| 462 | destfile.close(); |
||
| 463 | return false; |
||
| 464 | } |
||
| 465 | |||
| 466 | destfile << data; |
||
| 467 | |||
| 468 | destfile.close(); |
||
| 469 | return true; |
||
| 470 | } |
||
| 471 | |||
| 472 | string FileTools::Get(string src) |
||
| 473 | { |
||
| 474 | ifstream srcfile(src.c_str()); |
||
| 475 | if (!srcfile.is_open()) |
||
| 476 | { |
||
| 477 | srcfile.close(); |
||
| 478 | return ""; |
||
| 479 | } |
||
| 480 | |||
| 481 | string buffer = ""; |
||
| 482 | string line; |
||
| 483 | while (!srcfile.eof()) |
||
| 484 | { |
||
| 485 | getline(srcfile, line); |
||
| 486 | if (srcfile.eof()) |
||
| 487 | break; |
||
| 488 | buffer += line + "\n"; |
||
| 489 | }; |
||
| 490 | |||
| 491 | srcfile.close(); |
||
| 492 | |||
| 493 | return buffer; |
||
| 494 | } |
||
| 495 | |||
| 496 | bool FileTools::Copy(string src, string dest) |
||
| 497 | { |
||
| 498 | ifstream srcfile(src.c_str()); |
||
| 499 | if (!srcfile.is_open()) |
||
| 500 | { |
||
| 501 | srcfile.close(); |
||
| 502 | return false; |
||
| 503 | } |
||
| 504 | |||
| 505 | ofstream destfile(dest.c_str()); |
||
| 506 | if (!destfile.is_open()) |
||
| 507 | { |
||
| 508 | destfile.close(); |
||
| 509 | srcfile.close(); |
||
| 510 | return false; |
||
| 511 | } |
||
| 512 | |||
| 513 | string line; |
||
| 514 | while (!srcfile.eof()) |
||
| 515 | { |
||
| 516 | getline(srcfile, line); |
||
| 517 | if (srcfile.eof()) |
||
| 518 | break; |
||
| 519 | destfile << line << endl; |
||
| 520 | }; |
||
| 521 | |||
| 522 | destfile.close(); |
||
| 523 | srcfile.close(); |
||
| 524 | |||
| 525 | return true; |
||
| 526 | } |
||
| 527 | |||
| 528 | bool FileTools::Exist(string filename) |
||
| 529 | { |
||
| 530 | ifstream file(filename.c_str()); |
||
| 531 | if (file.is_open()) |
||
| 532 | { |
||
| 533 | file.close(); |
||
| 534 | return true; |
||
| 535 | } |
||
| 536 | |||
| 537 | return false; |
||
| 538 | } |
||
| 539 | |||
| 540 | |||
| 541 | |||
| 542 | |||
| 543 | |||
| 544 | /* |
||
| 545 | void bd() |
||
| 546 | { |
||
| 547 | int n,b=0,a[6],i=0,t=0; |
||
| 548 | clrscr(); |
||
| 549 | printf("Conversion from Binary to Decimal\n"); |
||
| 550 | printf("Enter Binary Number\n"); |
||
| 551 | scanf("%d",&n); |
||
| 552 | while(n!=0) |
||
| 553 | { |
||
| 554 | a[i]=n%10; |
||
| 555 | n=n/10; |
||
| 556 | if(a[i]!=1 && a[i]!=0) |
||
| 557 | t=1; |
||
| 558 | i++; |
||
| 559 | } |
||
| 560 | a[i]=2; |
||
| 561 | n=1; |
||
| 562 | for(i=0;a[i]!=2;i++) |
||
| 563 | { |
||
| 564 | b=b+a[i]*n; |
||
| 565 | n=n*2; |
||
| 566 | } |
||
| 567 | if(t==0) |
||
| 568 | printf("Decimal Equivalent=%d",B); |
||
| 569 | else if(t==1) |
||
| 570 | printf("Entered number is not binary."); |
||
| 571 | |||
| 572 | } |
||
| 573 | |||
| 574 | void db() |
||
| 575 | { |
||
| 576 | int dec,bin,n,i=0,a[10]; |
||
| 577 | clrscr(); |
||
| 578 | printf("Conversion from Decimal to Binary\n"); |
||
| 579 | printf("Input decimal no."); |
||
| 580 | scanf("%d",&dec); |
||
| 581 | do |
||
| 582 | { |
||
| 583 | a[i]=dec%2; |
||
| 584 | dec=dec/2; |
||
| 585 | i++; |
||
| 586 | }while(dec!=0); |
||
| 587 | for(n=i-1;n>=0;n--) |
||
| 588 | printf("%d",a[n]); |
||
| 589 | } |
||
| 590 | |||
| 591 | void doc() |
||
| 592 | { |
||
| 593 | int n,i,a[10]; |
||
| 594 | clrscr(); |
||
| 595 | printf("Conversion from Decimal to Octal\n"); |
||
| 596 | printf("Enter a Decimal number\n"); |
||
| 597 | scanf("%d",&n); |
||
| 598 | i=0; |
||
| 599 | printf("Octal equavalent of %d is ",n); |
||
| 600 | while(n!=0) |
||
| 601 | { |
||
| 602 | a[i]=n%8; |
||
| 603 | n=n/8; |
||
| 604 | i++; |
||
| 605 | } |
||
| 606 | i--; |
||
| 607 | for(;i>=0;i--) |
||
| 608 | printf("%d",a[i]); |
||
| 609 | } |
||
| 610 | |||
| 611 | void dh(long n) |
||
| 612 | { |
||
| 613 | long i; |
||
| 614 | if(n>0) |
||
| 615 | { |
||
| 616 | i=n%16; |
||
| 617 | n=n/16; |
||
| 618 | dh(n); |
||
| 619 | if(i>=10) |
||
| 620 | { |
||
| 621 | switch(i) |
||
| 622 | { |
||
| 623 | case 10: |
||
| 624 | printf("A"); |
||
| 625 | break; |
||
| 626 | case 11: |
||
| 627 | printf("B"); |
||
| 628 | break; |
||
| 629 | case 12: |
||
| 630 | printf("C"); |
||
| 631 | break; |
||
| 632 | case 13: |
||
| 633 | printf("D"); |
||
| 634 | break; |
||
| 635 | case 14: |
||
| 636 | printf("E"); |
||
| 637 | break; |
||
| 638 | case 15: |
||
| 639 | printf("F"); |
||
| 640 | break; |
||
| 641 | } |
||
| 642 | } |
||
| 643 | else |
||
| 644 | printf("%ld",i); |
||
| 645 | } |
||
| 646 | } |
||
| 647 | |||
| 648 | void od() |
||
| 649 | { |
||
| 650 | unsigned long n,i=0,a,p=1,t=0; |
||
| 651 | clrscr(); |
||
| 652 | printf("Conversion from Octal to Decimal\n"); |
||
| 653 | printf("Enter a Octal number\n"); |
||
| 654 | scanf("%ld",&n); |
||
| 655 | i=0; |
||
| 656 | printf("Decimal equavalent of %ld",n); |
||
| 657 | while(n!=0) |
||
| 658 | { |
||
| 659 | a=n%10; |
||
| 660 | if(a>7) |
||
| 661 | t=1; |
||
| 662 | n=n/10; |
||
| 663 | i=i+a*p; |
||
| 664 | p=p*8; |
||
| 665 | } |
||
| 666 | if(t==0) |
||
| 667 | printf("= %ld",i); |
||
| 668 | else if(t==1) |
||
| 669 | printf(" can't be calculated because it is not an Octal Number.\n"); |
||
| 670 | } |
||
| 671 | |||
| 672 | void ob() |
||
| 673 | { |
||
| 674 | int n,a[6],i=0,t=0; |
||
| 675 | clrscr(); |
||
| 676 | printf("Convertion from Octal to Binary.\n"); |
||
| 677 | printf("Enter an Octal Number.\n"); |
||
| 678 | scanf("%d",&n); |
||
| 679 | while(n!=0) |
||
| 680 | { |
||
| 681 | a[i]=n%10; |
||
| 682 | n=n/10; |
||
| 683 | if(a[i]>7) |
||
| 684 | t=1; |
||
| 685 | i++; |
||
| 686 | } |
||
| 687 | i--; |
||
| 688 | if(t==0) |
||
| 689 | for(;i>=0;i--) |
||
| 690 | { |
||
| 691 | switch(a[i]) |
||
| 692 | { |
||
| 693 | case 0: |
||
| 694 | printf("000"); |
||
| 695 | break; |
||
| 696 | case 1: |
||
| 697 | printf("001"); |
||
| 698 | break; |
||
| 699 | case 2: |
||
| 700 | printf("010"); |
||
| 701 | break; |
||
| 702 | case 3: |
||
| 703 | printf("011"); |
||
| 704 | break; |
||
| 705 | case 4: |
||
| 706 | printf("100"); |
||
| 707 | break; |
||
| 708 | case 5: |
||
| 709 | printf("101"); |
||
| 710 | break; |
||
| 711 | case 6: |
||
| 712 | printf("110"); |
||
| 713 | break; |
||
| 714 | case 7: |
||
| 715 | printf("111"); |
||
| 716 | break; |
||
| 717 | } |
||
| 718 | } |
||
| 719 | if(t==1) |
||
| 720 | printf("Not a Octal number\n"); |
||
| 721 | } |
||
| 722 | |||
| 723 | void bo() |
||
| 724 | { |
||
| 725 | int i=0,a[5],t=0; |
||
| 726 | long int n; |
||
| 727 | clrscr(); |
||
| 728 | printf("Convertion From Binary to Octal\n"); |
||
| 729 | printf("Enter a Binary number\n"); |
||
| 730 | scanf("%ld",&n); |
||
| 731 | while(n!=0) |
||
| 732 | { |
||
| 733 | a[i]=n%1000; |
||
| 734 | n=n/1000; |
||
| 735 | if(a[i]>111) |
||
| 736 | t=1; |
||
| 737 | i++; |
||
| 738 | } |
||
| 739 | i--; |
||
| 740 | if(t==0) |
||
| 741 | for(;i>=0;i--) |
||
| 742 | { |
||
| 743 | switch(a[i]) |
||
| 744 | { |
||
| 745 | case 0: |
||
| 746 | printf("0"); |
||
| 747 | break; |
||
| 748 | case 1: |
||
| 749 | printf("1"); |
||
| 750 | break; |
||
| 751 | case 10: |
||
| 752 | printf("2"); |
||
| 753 | break; |
||
| 754 | case 11: |
||
| 755 | printf("3"); |
||
| 756 | break; |
||
| 757 | case 100: |
||
| 758 | printf("4"); |
||
| 759 | break; |
||
| 760 | case 101: |
||
| 761 | printf("5"); |
||
| 762 | break; |
||
| 763 | case 110: |
||
| 764 | printf("6"); |
||
| 765 | break; |
||
| 766 | case 111: |
||
| 767 | printf("7"); |
||
| 768 | break; |
||
| 769 | default: |
||
| 770 | printf("\nEntered number is not binary.\nPrinted value is not correct.\n"); |
||
| 771 | break; |
||
| 772 | } |
||
| 773 | } |
||
| 774 | if(t==1) |
||
| 775 | printf("Number is not Binary\n"); |
||
| 776 | } |
||
| 777 | |||
| 778 | void bh() |
||
| 779 | { |
||
| 780 | int i=0,a[5],t=0; |
||
| 781 | long int n; |
||
| 782 | clrscr(); |
||
| 783 | printf("Convertion from Binary to Hexadecimal\n"); |
||
| 784 | printf("Enter a Binary number\n"); |
||
| 785 | scanf("%ld",&n); |
||
| 786 | while(n!=0) |
||
| 787 | { |
||
| 788 | a[i]=n%10000; |
||
| 789 | n=n/10000; |
||
| 790 | if(a[i]>1111) |
||
| 791 | t=1; |
||
| 792 | i++; |
||
| 793 | } |
||
| 794 | i--; |
||
| 795 | if(t==0) |
||
| 796 | for(;i>=0;i--) |
||
| 797 | { |
||
| 798 | switch(a[i]) |
||
| 799 | { |
||
| 800 | case 0: |
||
| 801 | printf("0"); |
||
| 802 | break; |
||
| 803 | case 1: |
||
| 804 | printf("1"); |
||
| 805 | break; |
||
| 806 | case 10: |
||
| 807 | printf("2"); |
||
| 808 | break; |
||
| 809 | case 11: |
||
| 810 | printf("3"); |
||
| 811 | break; |
||
| 812 | case 100: |
||
| 813 | printf("4"); |
||
| 814 | break; |
||
| 815 | case 101: |
||
| 816 | printf("5"); |
||
| 817 | break; |
||
| 818 | case 110: |
||
| 819 | printf("6"); |
||
| 820 | break; |
||
| 821 | case 111: |
||
| 822 | printf("7"); |
||
| 823 | break; |
||
| 824 | case 1000: |
||
| 825 | printf("8"); |
||
| 826 | break; |
||
| 827 | case 1001: |
||
| 828 | printf("9"); |
||
| 829 | break; |
||
| 830 | case 1010: |
||
| 831 | printf("A"); |
||
| 832 | break; |
||
| 833 | case 1011: |
||
| 834 | printf("B"); |
||
| 835 | break; |
||
| 836 | case 1100: |
||
| 837 | printf("C"); |
||
| 838 | break; |
||
| 839 | case 1101: |
||
| 840 | printf("D"); |
||
| 841 | break; |
||
| 842 | case 1110: |
||
| 843 | printf("E"); |
||
| 844 | break; |
||
| 845 | case 1111: |
||
| 846 | printf("F"); |
||
| 847 | break; |
||
| 848 | default: |
||
| 849 | printf("\nEntered number is not binary.\nPrinted value is not correct.\n"); |
||
| 850 | break; |
||
| 851 | } |
||
| 852 | } |
||
| 853 | if(t==1) |
||
| 854 | printf("Number is not Binary\n"); |
||
| 855 | } |
||
| 856 | |||
| 857 | void hb() |
||
| 858 | { |
||
| 859 | int i; |
||
| 860 | char s[20]; |
||
| 861 | clrscr(); |
||
| 862 | printf("Convertion from Hexadecimal to Binary\n"); |
||
| 863 | printf("Enter a Hexadecimal number\n"); |
||
| 864 | scanf("%s",s); |
||
| 865 | //gets(s); |
||
| 866 | printf("Binary Equivalent="); |
||
| 867 | for(i=0;s[i]!=NULL;i++) |
||
| 868 | { |
||
| 869 | switch(s[i]) |
||
| 870 | { |
||
| 871 | case '0': |
||
| 872 | printf("0000"); |
||
| 873 | break; |
||
| 874 | case '1': |
||
| 875 | printf("0001"); |
||
| 876 | break; |
||
| 877 | case '2': |
||
| 878 | printf("0010"); |
||
| 879 | break; |
||
| 880 | case '3': |
||
| 881 | printf("0011"); |
||
| 882 | break; |
||
| 883 | case '4': |
||
| 884 | printf("0100"); |
||
| 885 | break; |
||
| 886 | case '5': |
||
| 887 | printf("0101"); |
||
| 888 | break; |
||
| 889 | case '6': |
||
| 890 | printf("0110"); |
||
| 891 | break; |
||
| 892 | case '7': |
||
| 893 | printf("0111"); |
||
| 894 | break; |
||
| 895 | case '8': |
||
| 896 | printf("1000"); |
||
| 897 | break; |
||
| 898 | case '9': |
||
| 899 | printf("1001"); |
||
| 900 | break; |
||
| 901 | case 'a': |
||
| 902 | case 'A': |
||
| 903 | printf("1010"); |
||
| 904 | break; |
||
| 905 | case 'b': |
||
| 906 | case 'B': |
||
| 907 | printf("1011"); |
||
| 908 | break; |
||
| 909 | case 'c': |
||
| 910 | case 'C': |
||
| 911 | printf("1100"); |
||
| 912 | break; |
||
| 913 | case 'd': |
||
| 914 | case 'D': |
||
| 915 | printf("1101"); |
||
| 916 | break; |
||
| 917 | case 'e': |
||
| 918 | case 'E': |
||
| 919 | printf("1110"); |
||
| 920 | break; |
||
| 921 | case 'f': |
||
| 922 | case 'F': |
||
| 923 | printf("1111"); |
||
| 924 | break; |
||
| 925 | default: |
||
| 926 | printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n"); |
||
| 927 | break; |
||
| 928 | } |
||
| 929 | } |
||
| 930 | } |
||
| 931 | |||
| 932 | void hd() |
||
| 933 | { |
||
| 934 | int i,a[20]; |
||
| 935 | unsigned long int h=0,m=1; |
||
| 936 | char s[20]; |
||
| 937 | clrscr(); |
||
| 938 | printf("Convertion from Hexadecimal to Decimal\n"); |
||
| 939 | printf("Enter a Hexadecimal number\n"); |
||
| 940 | scanf("%s",s); |
||
| 941 | printf("Decimal Equivalent="); |
||
| 942 | for(i=0;s[i]!=NULL;i++) |
||
| 943 | { |
||
| 944 | switch(s[i]) |
||
| 945 | { |
||
| 946 | case '0': |
||
| 947 | a[i]=0; |
||
| 948 | break; |
||
| 949 | case '1': |
||
| 950 | a[i]=1; |
||
| 951 | break; |
||
| 952 | case '2': |
||
| 953 | a[i]=2; |
||
| 954 | break; |
||
| 955 | case '3': |
||
| 956 | a[i]=3; |
||
| 957 | break; |
||
| 958 | case '4': |
||
| 959 | a[i]=4; |
||
| 960 | break; |
||
| 961 | case '5': |
||
| 962 | a[i]=5; |
||
| 963 | break; |
||
| 964 | case '6': |
||
| 965 | a[i]=6; |
||
| 966 | break; |
||
| 967 | case '7': |
||
| 968 | a[i]=7; |
||
| 969 | break; |
||
| 970 | case '8': |
||
| 971 | a[i]=8; |
||
| 972 | break; |
||
| 973 | case '9': |
||
| 974 | a[i]=9; |
||
| 975 | break; |
||
| 976 | case 'a': |
||
| 977 | case 'A': |
||
| 978 | a[i]=10; |
||
| 979 | break; |
||
| 980 | case 'b': |
||
| 981 | case 'B': |
||
| 982 | a[i]=11; |
||
| 983 | break; |
||
| 984 | case 'c': |
||
| 985 | case 'C': |
||
| 986 | a[i]=12; |
||
| 987 | break; |
||
| 988 | case 'd': |
||
| 989 | case 'D': |
||
| 990 | a[i]=13; |
||
| 991 | break; |
||
| 992 | case 'e': |
||
| 993 | case 'E': |
||
| 994 | a[i]=14; |
||
| 995 | break; |
||
| 996 | case 'f': |
||
| 997 | case 'F': |
||
| 998 | a[i]=15; |
||
| 999 | break; |
||
| 1000 | default: |
||
| 1001 | printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n"); |
||
| 1002 | break; |
||
| 1003 | } |
||
| 1004 | } |
||
| 1005 | i--; |
||
| 1006 | for(;i>=0;i--) |
||
| 1007 | { |
||
| 1008 | h=h+a[i]*m; |
||
| 1009 | m=m*16; |
||
| 1010 | } |
||
| 1011 | printf("%ld ",h); |
||
| 1012 | } |
||
| 1013 | |||
| 1014 | void oh(long n) |
||
| 1015 | { |
||
| 1016 | long i; |
||
| 1017 | if(n>0) |
||
| 1018 | { |
||
| 1019 | i=n%16; |
||
| 1020 | n=n/16; |
||
| 1021 | oh(n); |
||
| 1022 | if(i>=10) |
||
| 1023 | { |
||
| 1024 | switch(i) |
||
| 1025 | { |
||
| 1026 | case 10: |
||
| 1027 | printf("A"); |
||
| 1028 | break; |
||
| 1029 | case 11: |
||
| 1030 | printf("B"); |
||
| 1031 | break; |
||
| 1032 | case 12: |
||
| 1033 | printf("C"); |
||
| 1034 | break; |
||
| 1035 | case 13: |
||
| 1036 | printf("D"); |
||
| 1037 | break; |
||
| 1038 | case 14: |
||
| 1039 | printf("E"); |
||
| 1040 | break; |
||
| 1041 | case 15: |
||
| 1042 | printf("F"); |
||
| 1043 | break; |
||
| 1044 | } |
||
| 1045 | } |
||
| 1046 | else |
||
| 1047 | printf("%ld",i); |
||
| 1048 | } |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | void ho() |
||
| 1052 | { |
||
| 1053 | int i,a[20]; |
||
| 1054 | unsigned long int h=0,m=1; |
||
| 1055 | char s[20]; |
||
| 1056 | clrscr(); |
||
| 1057 | printf("Convertion from Hexadecimal to Octal\n"); |
||
| 1058 | printf("Enter a Hexadecimal number\n"); |
||
| 1059 | scanf("%s",s); |
||
| 1060 | // Converting hex to dec first |
||
| 1061 | for(i=0;s[i]!=NULL;i++) |
||
| 1062 | { |
||
| 1063 | switch(s[i]) |
||
| 1064 | { |
||
| 1065 | case '0': |
||
| 1066 | a[i]=0; |
||
| 1067 | break; |
||
| 1068 | case '1': |
||
| 1069 | a[i]=1; |
||
| 1070 | break; |
||
| 1071 | case '2': |
||
| 1072 | a[i]=2; |
||
| 1073 | break; |
||
| 1074 | case '3': |
||
| 1075 | a[i]=3; |
||
| 1076 | break; |
||
| 1077 | case '4': |
||
| 1078 | a[i]=4; |
||
| 1079 | break; |
||
| 1080 | case '5': |
||
| 1081 | a[i]=5; |
||
| 1082 | break; |
||
| 1083 | case '6': |
||
| 1084 | a[i]=6; |
||
| 1085 | break; |
||
| 1086 | case '7': |
||
| 1087 | a[i]=7; |
||
| 1088 | break; |
||
| 1089 | case '8': |
||
| 1090 | a[i]=8; |
||
| 1091 | break; |
||
| 1092 | case '9': |
||
| 1093 | a[i]=9; |
||
| 1094 | break; |
||
| 1095 | case 'a': |
||
| 1096 | case 'A': |
||
| 1097 | a[i]=10; |
||
| 1098 | break; |
||
| 1099 | case 'b': |
||
| 1100 | case 'B': |
||
| 1101 | a[i]=11; |
||
| 1102 | break; |
||
| 1103 | case 'c': |
||
| 1104 | case 'C': |
||
| 1105 | a[i]=12; |
||
| 1106 | break; |
||
| 1107 | case 'd': |
||
| 1108 | case 'D': |
||
| 1109 | a[i]=13; |
||
| 1110 | break; |
||
| 1111 | case 'e': |
||
| 1112 | case 'E': |
||
| 1113 | a[i]=14; |
||
| 1114 | break; |
||
| 1115 | case 'f': |
||
| 1116 | case 'F': |
||
| 1117 | a[i]=15; |
||
| 1118 | break; |
||
| 1119 | default: |
||
| 1120 | printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n"); |
||
| 1121 | break; |
||
| 1122 | } |
||
| 1123 | } |
||
| 1124 | i--; |
||
| 1125 | for(;i>=0;i--) |
||
| 1126 | { |
||
| 1127 | h=h+a[i]*m; |
||
| 1128 | m=m*16; |
||
| 1129 | } |
||
| 1130 | // Now convering from decimal to octal (h) |
||
| 1131 | i=0; |
||
| 1132 | printf("Octal equavalent="); |
||
| 1133 | while(h!=0) |
||
| 1134 | { |
||
| 1135 | a[i]=h%8; |
||
| 1136 | h=h/8; |
||
| 1137 | i++; |
||
| 1138 | } |
||
| 1139 | i--; |
||
| 1140 | for(;i>=0;i--) |
||
| 1141 | printf("%d",a[i]); |
||
| 1142 | }*/ |