非常久没有写关于string的博客了。由于写的差点儿相同了。可是近期又与string打交道,于是荷尔蒙上脑,小蝌蚪躁动。

在程序中,假设用到了颜色代码,一般都是十六进制的,即hex。

可是server给你返回一个颜色字符串。即hex string

你怎么把这个hex string 转为 hex,并在你的代码中使用?

更进一步,你怎么办把一个形如”#ffceed”的hex string 转为 RGB呢?

第一个问题在Java中是这样搞的:

public static int parseColor(@Size(min=1) String colorString) {
if (colorString.charAt(0) == '#') {
// Use a long to avoid rollovers on #ffXXXXXX
long color = Long.parseLong(colorString.substring(1), 16);
if (colorString.length() == 7) {
// Set the alpha value
color |= 0x00000000ff000000;
} else if (colorString.length() != 9) {
throw new IllegalArgumentException("Unknown color");
}
return (int)color;
} else {
Integer color = sColorNameMap.get(colorString.toLowerCase(Locale.ROOT));
if (color != null) {
return color;
}
}
throw new IllegalArgumentException("Unknown color");
}

可是在C++中,我们能够用流,这样更加简洁:

    auto color_string_iter = hex_string_color.begin();
hex_string_color.erase(color_string_iter);
hex_string_color= "ff" + hex_string_color;
DWORD color;
std::stringstream ss;
ss << std::hex << hex_string_color;
ss >> std::hex >> color; btn1->SetBkColor(color);
btn2->SetBkColor(0xff123456);

还有一种方法,能够使用:std::strtoul

主要是第三个參数:

Numerical base (radix) that determines the valid characters and their interpretation.

If this is 0, the base used is determined by the format in the sequence

#include <cstdlib>
#include <iostream> // for std::cout int main()
{
char hex_string[] = "0xbeef";
unsigned long hex_value
= std::strtoul(hex_string, 0, 16);
std::cout << "hex value: " << hex_value << std::endl;
return 0;
}

接下来看看怎样把hex string 转rgb:

#include <iostream>
#include <sstream> int main()
{
std::string hexCode;
std::cout << "Please enter the hex code: ";
std::cin >> hexCode; int r, g, b; if(hexCode.at(0) == '#') {
hexCode = hexCode.erase(0, 1);
} // ... and extract the rgb values.
std::istringstream(hexCode.substr(0,2)) >> std::hex >> r;
std::istringstream(hexCode.substr(2,2)) >> std::hex >> g;
std::istringstream(hexCode.substr(4,2)) >> std::hex >> b; // Finally dump the result.
std::cout << std::dec << "Parsing #" << hexCode
<< " as hex gives (" << r << ", " << g << ", " << b << ")" << '\n';
}

这里有一点忠告,也是忠告自己。

我们从学习编程開始,最熟悉的就是print或是cout看看结果。可是在实际工作中是非常少用到的。

比方有一个十进制数100。我们何以通过cout<

实战c++中的string系列--十六进制的字符串转为十六进制的整型(一般是颜色代码使用)的更多相关文章

  1. 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)

    string.vector 互转 string 转 vector vector  vcBuf;string        stBuf("Hello DaMao!!!");----- ...

  2. 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)

    今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ...

  3. 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())

    在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数 ...

  4. 实战c++中的string系列--不要使用memset初始化string(一定别这么干)

    參考链接: http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html 百度百科第一次这么给力: void *memset(voi ...

  5. 实战c++中的string系列--std::string与MFC中CString的转换

    搞过MFC的人都知道cstring,给我们提供了非常多便利的方法. CString 是一种非常实用的数据类型. 它们非常大程度上简化了MFC中的很多操作,使得MFC在做字符串操作的时候方便了非常多.无 ...

  6. 实战c++中的string系列--指定浮点数有效数字并转为string

    上一篇博客讲了好几种方法进行number到string的转换,这里再单独说一下float或是double到string的转换. 还是处于控件显示的原因.比方说要显示文件的大小,我们从server能够获 ...

  7. 实战c++中的string系列--CDuiString和string的转换(duilib中的cduistring)

    使用所duilib的人定会知道cduistring类型,先看看这个类是怎么定义的: class UILIB_API CDuiString { public: enum { MAX_LOCAL_STRI ...

  8. 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)

    使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element ...

  9. 在iOS开发中,我们会遇到十六进制和字符串之间相互转换,话不多说,直接上代码:

    //将十六进制的字符串转换成NSString则可使用如下方式: + (NSString *)convertHexStrToString:(NSString *)str { if (!str || [s ...

随机推荐

  1. FastText 介绍

    FastText 介绍 在面试百度的NLP工程师时,被问及常用的词向量表示学习方法有哪些,我说知道word2vec,然后大佬又问我知道FastText么... 这就很尴尬了,不会! 不同于word2v ...

  2. PHP 格式化显示时间 date()函数

    Y 4位数字年,y为2位数字,如99即1999年 m 数字月份,前面有前导0,如01.n 为无前导0数字月份 F 月份,完整的文本格式,例如 January 或者 March M 三个字母缩写表示的月 ...

  3. POJ 2021 Relative Relatives

    Relative Relatives Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3339   Accepted: 146 ...

  4. 【转】hibernate延迟加载和抓取策略

    一.延迟加载 1.简单查询get,load 针对对象本身延迟或即时 当使用load方法来得到一个对象时,此时hibernate会使用延迟加载的机制来加载这个对象,即:当我们使用session.load ...

  5. iOS学习笔记31-从图册获取图片和视频

    一.从图册中获取本地图片和视频 从图册中获取文件,我们使用的是UIImagePickerController,这个类我们在之前的摄像头中使用过,这里是链接:iOS学习笔记27-摄像头,这里我们使用的是 ...

  6. 【Luogu】P2445动物园(最大流)

    题目链接 题目本身还是比较水的吧……容易发现是最大流套上dinic跑一遍就好了,并不会超时. 比较不偷税的一点是关于某动物的所有目击报告都符合才能连边……qwqqwqqwq #include<c ...

  7. kb-07专题--线段树-01-单点修改,区间查和

    给定区间长度,然后给两个操作,单点增加值和单点减值,询问一个区间的人数和:(水) 代码如下: /* 写的第一个线段树,丑: */ #include<iostream> #include&l ...

  8. Rust 内存管理

    Rust 内存管理 Rust 与其他编程语言相比,最大的亮点就是引入了一套在编译期间,通过静态分析的方式,确定所有对象的作用域与生命周期,从而可以精确的在某个对象不再被使用时,将其销毁,并且不引入任何 ...

  9. 安装环境 :win64

    1.安装环境 :win64 1.1 下载mysql安装包地址: https://dev.mysql.com/downloads/file/?id=476233 2.安装 2.1 解压下载的ZIP压缩包 ...

  10. 加速和简化构建Docker(基于Google jib)

    赵安家 2019年02月11日阅读 1518 关注 加速和简化构建Docker(基于Google jib) 介绍 其实jib刚发布时就有关注,但是一直没有用于生产,原因有二 基于 spotify/do ...