How do I convert a string value in uint16 in Go lang?

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

How do I convert a string value in uint16 in Go lang?

问题

我正在研究一种将字符串(在我的情况下,字符串是"05f8")转换为uint16的解决方案。我进行了一些研究,但没有找到解决方案。有人知道如何做到这一点吗?

谢谢你的帮助!

英文:

I'm researching a solution in order to convert a string (in my case the string is "05f8") into a uint16.I made research but I don't find a solution.Does anyone know how to do this?

Thanks for your help!

答案1

得分: 14

使用strconv.ParseUint函数进行转换。

var s = "05f8"
var base = 16
var size = 16
value, err := strconv.ParseUint(s, base, size)
value2 := uint16(value) // 完成!

请注意,输出值是uint64类型,您在使用之前需要将其转换为您所需的类型。

另请注意,size参数控制要转换为的uint的最大大小,因此溢出检查是正确的。

英文:

Use strconv.ParseUint (doc).

var s = "05f8"
var base = 16
var size = 16
value, err := strconv.ParseUint(s, base, size)
value2 := uint16(value) // done!

Note that the output value is an uint64, you have to cast it to you type before using it.

Note (bis) that the size parameter control the maximum size of the uint to convert to, so the overflow check is done correctly.

答案2

得分: 2

如果你想将一个 string 转换为 []uint16,你可以这样做:

package main

import (
   "fmt"
   "golang.org/x/sys/windows"
)

func main() {
   a, e := windows.UTF16FromString("05f8")
   if e != nil {
      panic(e)
   }
   fmt.Printf("%q\n", a) // ['0' '5' 'f' '8' '\x00']
}

或者,如果你确定 string 不包含 NUL 字节,你可以这样做:

package main

import (
   "fmt"
   "golang.org/x/sys/windows"
)

func main() {
   a := windows.StringToUTF16("05f8")
   fmt.Printf("%q\n", a) // ['0' '5' 'f' '8' '\x00']
}
英文:

If you are interested in turning a string into []uint16, you can do:

package main

import (
   "fmt"
   "golang.org/x/sys/windows"
)

func main() {
   a, e := windows.UTF16FromString("05f8")
   if e != nil {
      panic(e)
   }
   fmt.Printf("%q\n", a) // ['0' '5' 'f' '8' '\x00']
}

or, if you are certain string contains no NUL bytes, you can do this:

package main

import (
   "fmt"
   "golang.org/x/sys/windows"
)

func main() {
   a := windows.StringToUTF16("05f8")
   fmt.Printf("%q\n", a) // ['0' '5' 'f' '8' '\x00']
}

答案3

得分: 2

我不是Go开发者,对于这个问题我不敢妄下结论,但我愿意听取反馈。你想将一个环境变量从字符串转换为uint16类型。你可以尝试以下代码:

文件: main.go

package main

import (
  "log"
  "os"
  "strconv"
)

var PORT = os.Getenv("PORT")

func main() {
  portAsInt, err := strconv.ParseInt(PORT, 10, 16)

  if err != nil {
    log.Fatal(err)
  }

  // 根据需要使用 uint16()
  log.Println("监听端口:", uint16(portAsInt))
}

运行应用程序:

PORT=3000 go run main.go;
# 预期输出:
# 监听端口:3000

请注意,我只是将代码翻译成中文,无法保证代码的正确性。如果有问题,请提供反馈。

英文:

By no means do I claim to be a go developer, and I welcome feedback on this, but I was trying to convert an env variable for a port from a string to uint16. I was able to get it working with:

File: main.go

package main

import (
  "log"
  "os"
  "strconv"
)

var PORT = os.Getenv("PORT")

func main() {
  portAsInt, err := strconv.ParseInt(PORT, 0, 16)

  if (err != nil) {
    log.Fatal(err)
  }

  // Use as needed with uint16()
  log.Println("Listening on port:", uint16(portAsInt))
}

Running application:

PORT=3000 go run main.go;
# Expected Output:
# Listening on port: 3000

huangapple
  • 本文由 发表于 2016年3月3日 01:15:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/35753904.html
匿名

发表评论

匿名网友

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

确定