如何将字符串转换为十六进制? [英] how to convert string to hexadecimal?

查看:84
本文介绍了如何将字符串转换为十六进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串 0xFF的,有没有像任何函数atoi 它读取该字符串,并保存在 uint32_t的格式?

I have a string 0xFF, is there any function like atoi which reads that string and save in a uint32_t format?

推荐答案

您也可以用这样的功能做到这一点。

you can also do it with a function like this.

unsigned int foo(const char * s) {
 unsigned int result = 0;
 int c ;
 if ('0' == *s && 'x' == *(s+1)) { s+=2;
  while (*s) {
   result = result << 4;
   if (c=(*s-'0'),(c>=0 && c <=9)) result|=c;
   else if (c=(*s-'A'),(c>=0 && c <=5)) result|=(c+10);
   else if (c=(*s-'a'),(c>=0 && c <=5)) result|=(c+10);
   else break;
   ++s;
  }
 }
 return result;
}

例如:

 printf("%08x\n",foo("0xff"));

这篇关于如何将字符串转换为十六进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆