如何像C中那样将字符串转换为ASCII字符串?

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 !

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.

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.