美文网首页
Sscanf函数

Sscanf函数

作者: 不要重不要重 | 来源:发表于2019-06-20 12:29 被阅读0次

函数原型

//Sscanf scans the argument string, storing successive space-separated values
//into successive arguments as determined by the format.
//It returns the number of items successfully parsed.
//Newlines in the input must match newlines in the format.
func Sscanf(str string, format string, a ...interface{}) (n int, err error)

在做hex 转变 rgb 的时候,看了一段 go-colorful 的 代码, 遇到了一个格式化符号,%02x,以前没遇到过。

// Hex parses a "html" hex color-string, either in the 3 "#f0c" or 6 "#ff1034" digits form.
func Hex(scol string) (Color, error) {
        format := "#%02x%02x%02x"
        factor := 1.0 / 255.0
        if len(scol) == 4 {
                format = "#%1x%1x%1x"
                factor = 1.0 / 15.0
        }

        var r, g, b uint8
        n, err := fmt.Sscanf(scol, format, &r, &g, &b)
        if err != nil {
                return Color{}, err
        }
        if n != 3 {
                return Color{}, fmt.Errorf("color: %v is not a hex-color", scol)
        }

        return Color{float64(r) * factor, float64(g) * factor, float64(b) * factor}, nil
}

其实是把 #ff00ff 之类的转变为 r,g,b。 是个很简单的操作,在我做来,可能会用字符串分割,没想过这么用,记录一下
另外,记录一下bool类型的格式化符号是 %t.
最后,说一下 go 的优势,就是没有动态库,各种官方的库都可以随便看,随便学。我一点都不狂。。。哈哈哈哈哈哈。心情好。

相关文章

  • Sscanf函数

    函数原型 在做hex 转变 rgb 的时候,看了一段 go-colorful 的 代码, 遇到了一个格式化符号,%...

  • sscanf()函数使用

    搬运,详情看下面博客:C++sscanf()的初级理解C++sscanf()的用法

  • 格式输入输出

    int sscanf( constchar*, const char *, ...);int sscanf(con...

  • All for PAT秋考 | 1132 - 1135

    涉及知识1132 sscanf(),浮点错误1133 链表重排(cmp函数、假装重排= =)1134 图的点覆盖(...

  • 关于sscanf函数的使用

    sscanf函数简介 从一个字符串中读进与指定格式相符的数据的函数 相关函数:swscanf() - 用于处理宽字...

  • sscanf

    sscanf 是C函数库 中的一个函数, 主要用来从字符串读取格式化输入, 参考: https://www.r...

  • 可变参数在宏定义中的应用

    在C语言的标准库中,printf、scanf、sscanf、sprintf这些标准库的输入输出函数,参数都是可变的...

  • 日至排序

    这道题主要是sscanf的用法,反正我还没太明白,以后再看看。sscanf用法

  • 白书字符串数组

    数字与字符串转换 使用itoa函数,注意要指定转换的进制.. 用sprintf,sscanf来写入写出.. 1.蛇...

  • C语言sscanf函数的总结

    https://blog.csdn.net/gzshun/article/details/7081736

网友评论

      本文标题:Sscanf函数

      本文链接:https://www.haomeiwen.com/subject/dwudqctx.html