ç十六进制转换为十进制格式 [英] C convert hex to decimal format

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

问题描述

使用gcc编译Linux的。

Compiling on linux using gcc.

我想这个转换为十六进制。 10这将是一个。
我已成功地做到以下这将code。

I would like to convert this to hex. 10 which would be a. I have managed to do this will the code below.

unsigned int index = 10;
char index_buff[5] = {0};
sprintf(index_buff, "0x%x", index);
data_t.un32Index = port_buff;

然而,问题是,我需要将其分配到一个结构
我需要分配给该元素是一个unsigned int类型。

However, the problem is that I need to assign it to a structure and the element I need to assign to is an unsigned int type.

然而,这作品:

data_t.un32index = 0xa;

不过,我的样本code没有工作,因为它认为我尝试转换
从字符串到unsigned int类型。

However, my sample code doesn't work as it thinks I am trying to convert from an string to a unsigned int.

我已经试过这一点,但是这也未能

I have tried this, but this also failed

data_t.un32index = (unsigned int) *index_buff;

非常感谢任何建议,

Many thanks for any advice,

推荐答案

咦?如果在一个变量中的值的小数/六角无关紧要。只要做到

Huh? The decimal/hex doesn't matter if you have the value in a variable. Just do

data_t.un32index = index;

打印号码时,让人们可以阅读

十进制和十六进制都只是符号。

Decimal and hex are just notation when printing numbers so humans can read them.

对于C(或C ++或Java,或任意数量的语言,这些类型都是原始人与语义紧密匹配的机器寄存器)整数变量,其持有的价值永远不能说要十六进制。

For a C (or C++, or Java, or any of a number of languages where these types are "primitives" with semantics closely matching those of machine registers) integer variable, the value it holds can never be said to "be in hex".

该值以二进制举行(在所有典型的现代电子计算机,这是数字化,本质上二进制)在内存或注册支持的变量,然后可以生成各种字符串重新presentations,这是当你需要选择一个基地使用。

The value is held in binary (in all typical modern electronic computers, which are digital and binary in nature) in the memory or register backing the variable, and you can then generate various string representations, which is when you need to pick a base to use.

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

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