eGospodarka.pl
eGospodarka.pl poleca

eGospodarka.plGrupypl.comp.programmingAlgorytm hex,dec<->liczba › Re: Algorytm hex,dec<->liczba
  • Data: 2017-07-04 15:59:43
    Temat: Re: Algorytm hex,dec<->liczba
    Od: Borneq <b...@a...hidden.pl> szukaj wiadomości tego autora
    [ pokaż wszystkie nagłówki ]

    W dniu 04.07.2017 o 15:05, Borneq pisze:
    > Tu znalazłem dobrze rozpisane:
    > http://www.cs.trincoll.edu/~ram/cpsc110/inclass/conv
    ersions.html
    > może się przydać

    Ostatecznie, trochę pozmieniałem kolejność w baseToNumber
    Gotowiec:
    #include <string>
    #include <boost/multiprecision/cpp_int.hpp>

    std::string numberToBase(boost::multiprecision::cpp_int n, int base)
    {
    const int max_buf = 128; //128 bit to binary
    char buf[max_buf+1];
    buf[max_buf] = 0;
    int pos = max_buf - 1;
    while (n != 0)
    {
    boost::multiprecision::cpp_int rb = n % base;
    int r = (int)rb;
    n = n / base;
    char letter;
    if (r < 10)
    letter = '0' + r;
    else
    letter = 'a' + (r - 10);
    buf[pos] = letter;
    pos--;
    }
    return buf + pos + 1;
    }

    boost::multiprecision::cpp_int baseToNumber(std::string str, int base)
    {
    boost::multiprecision::cpp_int n = 0;
    for (int i = 0; i< str.length(); i++)
    {
    char letter = tolower(str[i]);
    int r;
    if (letter >= '0' && letter <= '9')
    r = letter - '0';
    else
    r = letter - 'a' + 10;
    n = n * base + r;
    }
    return n;
    }

    int main()
    {
    std::string strnum = numberToBase(642452, 16);
    std::cout << strnum << std::endl;
    boost::multiprecision::cpp_int n = baseToNumber(strnum, 16);
    std::cout << n << std::endl;
    return 0;
    }



Podziel się

Poleć ten post znajomemu poleć

Wydrukuj ten post drukuj


Następne wpisy z tego wątku

Najnowsze wątki z tej grupy


Najnowsze wątki

Szukaj w grupach

Eksperci egospodarka.pl

1 1 1

Wpisz nazwę miasta, dla którego chcesz znaleźć jednostkę ZUS.

Wzory dokumentów

Bezpłatne wzory dokumentów i formularzy.
Wyszukaj i pobierz za darmo: