배열로 구성된 2진수를 10진수로 바꾸는 법

jjkyj의 이미지

[C언어로 512비트 이상의 큰 자리의 수를 나타내기 위해 배열을 사용했습니다.]

2진수가 배열로 구성되었을때 ( a[MAX] = "010001010...")

이것을 같은 형식으로 10진수로 나타낼 수 있는 좋은 방법이 없을까요?

10진수를 2진수로 바꾸는 건 일단 성공했는데[2로 계속 나누는 계산을 구현]
2진수에서 10진수로 바꾸는 건 어떻게 해야할지 방법이 잘 떠오르지 않네요.
2의 지수승으로 나타낼 수 있는 수의 범위도 한계가 있을테고..

htna의 이미지

2진수는 16진수로 바꾸는데 정말 쉽습니다.
(당연한 얘기겠지만..)
16진수로 바꾼다음에, "10진수->2진수"로 했듯이...
"16진수->10진수" 로 하면 될 듯 하네요...

WOW Wow!!!
Computer Science is no more about computers than astronomy is about telescopes.
-- E. W. Dijkstra

냐옹이의 이미지

이게 좋은 알고리즘인지는 알 수 없으나. 수학책에서 이진수를 십진수로 바꾸는 문제가 있잖아요.
그것을 착안해서 만들면 되지 않을까요??? 혹시 math.h에서 진법 변환 함수가 있을지 모르겠지만요.

1. 배열의 실제 갯수(n)를 센다. (배열이 마지막 MAX까지 값이 차지 않았을 수도 있으니까)
2. sigma a=n n->0 {2^(a-1) * v} (v:n의 값)(컴퓨터 상이라 수식이 :oops:)

제가 수학을 못해서 ㅠ.ㅠ 이렇게 하시면 될거 같은데요. 위의 시그마는 제귀함수로 구현하면 될거 같은데 어떨까요??

------------------------
냐옹~~

익명 사용자의 이미지

테이블을 미리 만드세요. 그래봐야 얼마 안될거 같은데요.

BinaryToDecimalTable[] = { 1, 2, 4, 8, 16, ... };

이런식으로 하신다음에 1이면 그에 해당하는 숫자를 더하면 끝!

메모리가 극단적으로 제한적인 시스템이라면 문제가 될수 있다고 생각할수도 있겠지만, 대부분의 경우에 최고의 선택일겁니다.

jjkyj의 이미지

의견들 감사합니다.
저도 나름대로 생각해보았는데
2진수에서 그대로 10씩 나누는 것을 반복하면 될 것 같습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.