Skip to content
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

adding binary to int conversion #1475

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
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
28 changes: 14 additions & 14 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,22 @@ func (pkg *PackageDefinitions) evaluateConstValue(file *ast.File, iota int, expr
if strings.ContainsRune(valueExpr.Value, '_') {
valueExpr.Value = strings.Replace(valueExpr.Value, "_", "", -1)
}
// hexadecimal
if len(valueExpr.Value) > 2 && valueExpr.Value[0] == '0' && valueExpr.Value[1] == 'x' {
if x, err := strconv.ParseInt(valueExpr.Value[2:], 16, 64); err == nil {
return int(x), nil
} else if x, err := strconv.ParseUint(valueExpr.Value[2:], 16, 64); err == nil {
return x, nil
} else {
panic(err)
if len(valueExpr.Value) >= 2 && valueExpr.Value[0] == '0' {
var start, base = 2, 8
switch valueExpr.Value[1] {
case 'x', 'X':
//hex
base = 16
case 'b', 'B':
//binary
base = 2
default:
//octet
start = 1
}
}

//octet
if len(valueExpr.Value) > 1 && valueExpr.Value[0] == '0' {
if x, err := strconv.ParseInt(valueExpr.Value[1:], 8, 64); err == nil {
if x, err := strconv.ParseInt(valueExpr.Value[start:], base, 64); err == nil {
return int(x), nil
} else if x, err := strconv.ParseUint(valueExpr.Value[1:], 8, 64); err == nil {
} else if x, err := strconv.ParseUint(valueExpr.Value[start:], base, 64); err == nil {
return x, nil
} else {
panic(err)
Expand Down