Skip to content

feat: Binary to Decimal Conversion #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 23, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: Better naming convention
  • Loading branch information
motasimmakki committed Oct 23, 2021
commit 520450e7cf9ae1d397953254e9c07c6380e8e840
12 changes: 6 additions & 6 deletions conversion/binarytodecimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ import (
"regexp"
)

var IsValid = regexp.MustCompile("^[0-1]{1,}$").MatchString
var isValid = regexp.MustCompile("^[0-1]{1,}$").MatchString

// BinaryToDecimal() function that will take Binary number as string,
// and return it's Decimal equivalent as integer.
func BinaryToDecimal(num string) (int, error) {
if !IsValid(num) {
func BinaryToDecimal(binary string) (int, error) {
if !isValid(binary) {
return -1, errors.New("not a valid binary string")
}
if len(num) > 32 {
if len(binary) > 32 {
return -1, errors.New("binary number must be in range 0 to 2^(31-1)")
}
var result, base int = 0, 1
for i := len(num) - 1; i >= 0; i-- {
if num[i] == '1' {
for i := len(binary) - 1; i >= 0; i-- {
if binary[i] == '1' {
result += base
}
base *= 2
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy