如何将字符串转换为类似于C语言中的ASCII字符串?

huangapple go评论81阅读模式
英文:

How to transform a string into an ASCII string like in C?

问题

我必须为我的学校做一个密码学项目,我选择用Go语言来完成这个项目!

我已经阅读了文档,但我只会C语言,所以现在对我来说有点困难。

首先,我需要收集程序的参数,我已经做到了。我将所有的参数都存储在一个字符串变量中,像这样:

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

现在,我需要将ASCII码存储在一个整数数组中,例如,在C语言中,我会这样做:

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

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

那么在Go语言中我该如何做呢?

谢谢!

英文:

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 !

答案1

得分: 2

我猜你想要这样的代码:

package main

import (
	"fmt"
	"strings"
)

// transform将ASCII字母转换为数字。
// 英语(基本拉丁字母)字母,包括大写和小写字母,
// 用介于1到26之间的数字表示。所有其他字符,
// 包括空格,用数字0表示。
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)
}

输出结果:

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

参考A Tour of Go,看看你是否能理解这个程序的功能。

英文:

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:

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

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

答案2

得分: 2

这是一个类似于其他答案但避免导入额外包的示例。

创建一个长度等于字符串长度的int切片。然后遍历字符串,将每个字符作为int提取出来,并将其赋值给int切片中相应的索引位置。以下是代码(也可以在Go Playground上查看):

package main

import "fmt"

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

// 将字符串中的每个字符作为int存储在切片中
func StringToInts(s string) (intSlice []int) {
    intSlice = make([]int, len(s))
    for i, _ := range s {
        intSlice[i] = int(s[i])
    }
    return
}

上述程序的输出是:

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

上述代码中的StringToInts函数应该符合你的需求。虽然它返回的是一个int切片(而不是数组),但它应该满足你的用例。

英文:

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 &quot;fmt&quot;

func main() {
	s := &quot;Hi Stack OverFlow&quot;
	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.

huangapple
  • 本文由 发表于 2014年11月26日 02:18:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/27133888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定