1

I have to do a cryptography project for my school and I choose Go for this project !

I read the doc but I only C, so it's kinda hard for me right now.

First , I needed to collect the program arguments, I did it. I stockd all arguments in a string variable like :

var text, base string = os.Args[1], os. Args[6]

Now , i need to store the ASCII number in a array of int , for exemple , in C I would done something like that :

int     arr[18];
char    str[18] = "Hi Stack OverFlow";

arr[i] = str[i] - 96;

So how could I do that in Go?

Thanks !

6
  • you're probably better off starting here: golang.org/doc, and specifically: golang.org/ref/spec#Conversions Commented Nov 25, 2014 at 18:23
  • 2
    I don't understand anything in this page , english isn't my first language and I started programation two months ago , there are too much new things... Commented Nov 25, 2014 at 18:50
  • 1
    Use Google translate, select English to Russian, drop the url from the website you want to translate. And done! a := "Hello World!" for key, _ := range a { fmt.Println(a[key] - 96) } Commented Nov 25, 2014 at 19:00
  • 1
    @Dippo: a := "Hello World!" for key, _ := range a { fmt.Println(a[key] - 96) } is not a good example. Commented Nov 25, 2014 at 22:10
  • @PeterSO That is perhaps true, but the question from the OP is not clear. Also, he is new and i thought, i give him something instead of nothing. Commented Nov 25, 2014 at 22:16

2 Answers 2

2

Here's an example that is similar to the other answer but avoids importing additional packages.

Create a slice of int with the length equal to the string's length. Then iterate over the string to extract each character as int and assign it to the corresponding index in the int slice. Here's code (also on the Go Playground):

package main

import "fmt"

func main() {
    s := "Hi Stack OverFlow"
    fmt.Println(StringToInts(s))
}

// makes a slice of int and stores each char from string
// as int in the slice
func StringToInts(s string) (intSlice []int) {
    intSlice = make([]int, len(s))
    for i, _ := range s {
        intSlice[i] = int(s[i])
    }
    return
}

Output of the above program is:

[72 105 32 83 116 97 99 107 32 79 118 101 114 70 108 111 119]

The StringToInts function in the above should do what you want. Though it returns a slice (not an array) of int, it should satisfy your usecase.

Sign up to request clarification or add additional context in comments.

2 Comments

You fail to address this part of the question: arr[i] = str[i] - 96;. You make improper use of the range clause: "For a string value, the "range" clause iterates over the Unicode code points in the string starting at byte index 0."
This solution is reusable and the str[i] - 96 can be done by the caller. Also, since it uses the primitive constructs of the language, much easier to read. However, thanks for the feedback.
2

My guess is that you want something like this:

package main

import (
    "fmt"
    "strings"
)

// transform transforms ASCII letters to numbers.
// Letters in the English (basic Latin) alphabet, both upper and lower case,
// are represented by a number between one and twenty-six. All other characters,
// including space, are represented by the number zero.
func transform(s string) []int {
    n := make([]int, 0, len(s))
    other := 'a' - 1
    for _, r := range strings.ToLower(s) {
        if 'a' > r || r > 'z' {
            r = other
        }
        n = append(n, int(r-other))
    }
    return n
}

func main() {
    s := "Hi Stack OverFlow"
    fmt.Println(s)
    n := transform(s)
    fmt.Println(n)
}

Output:

Hi Stack OverFlow
[8 9 0 19 20 1 3 11 0 15 22 5 18 6 12 15 23]

Take A Tour of Go and see if you can understand what the program does.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.