插入六角codeS数组转换成整数 [英] insert array of hex codes into an integer

查看:112
本文介绍了插入六角codeS数组转换成整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一些事情,我认为将是pretty基本的,但不是我只是忽略了一些东西明显或它实际上是一个有点棘手。我的问题是:我有一个包含4个十六进制值4字符数组。例如:

I'm trying to do something that I thought would be pretty basic but either I'm just ignoring something obvious or it is actually a bit tricky. My problem is: I have an array of 4 chars that contains 4 hex values. For example:

array[0] = 0xD8
array[1] = 0xEC
array[2] = 0xA2 
array[3] = 0x83

欲该数组存储在与该组合值的一个整数,在这种情况下0xD8ECA283

I want to store this array in an integer with the combined value, in this case 0xD8ECA283

我试图做逻辑或,然后移位并用该方法我设法0xD8的值存储在整数,但不能休息。任何提示将AP preciated。

I've tried doing logical OR and then shifting the bits and with this method I managed to store the value of 0xD8 in the integer, but not the rest. Any tips would be appreciated.

推荐答案

这应该这样做:

int i;
int combined = 0;
for (i = 0; i < 4; i++) {
    combined = (combined << 8) | ((unsigned char) array[i]);
}

这篇关于插入六角codeS数组转换成整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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