将Hex放入字节数组中

时间:2016-12-06 19:52:40

标签: c arrays hex

我试图将十六进制值放入Byte[],试图达到78,66,1E,B5,4F,E7,67,63

   #define BYTE   unsigned char
   int num = 1;

   long long hex[]
   {
     0x78661EB54FE76763,
   };

 int main()
 {

        for (int c = 0; c < num; c++)
        {
            printf("%llx\n", hex[c]);

            unsigned int number = hex[c];
            unsigned int ef = number & 0xff;
            unsigned int cd = (number >> 8) & 0xff;
            unsigned int ab = (number >> 16) & 0xff;

            printf("%x", number & 0xff);

            BYTE data2[8]{
                ef, cd, ab
            };

        }
}

更新

基本上我有一个30个奇数十六进制值的数组。我试图循环通过名为hex []的数组,然后为每个十六进制值将其分成2,即78,66,1E,B5,4F,E7,67,63,然后将每一个添加到一个数组中键入BYTE,它将十六进制值保持为8对,因此data [0]将具有78到数据[8]的值,其值为63,因此我可以将BYTE类型的数组传递给另一个方法进一步的工作

3 个答案:

答案 0 :(得分:3)

以下是您想要的解决方案:

#include <stdio.h>
typedef unsigned char BYTE;
int main()
{
    int i,j;
    long long hex=0x78661EB54FE76763;
    BYTE val[8];

    for(j=0,i=7; i>=0; i--,j++){
        val[j]= (hex>>(i*8))&0xff;
    }       

    printf("Hexadecimal bytes are:\n");
    for(j=0;j<8;j++){
        printf("%02X ",val[j]);
    }

    return 0;

}   

输出是:

Hexadecimal bytes are:
78 66 1E B5 4F E7 67 63

答案 1 :(得分:0)

假设BYTE是一种类型

BYTE data2[] = {ef, cd, ab, '\0'};

使用null终止符允许使用"%s"printf()进行打印。

当然,您可以添加剩余的字节。

答案 2 :(得分:0)

这是一项非常简单的任务,可以完成您所需要的所有初始化值。

long long hex[] = { 0x78661EB54FE76763 };

接下来,您需要一个指向十六进制数组的字符指针

unsigned char* pByte = (unsigned char*)hex; // Point to the first element in hex array

然后创建一个8字节的缓冲区并用memset清除它:

unsigned char byteArray[8];
memset(byteArray, 0, sizeof(byteArray));

您现在需要做的就是取消引用您的char指针并将值放入8字节缓冲区。

// Loop through the hex array and assign the current byte
// the byte pointer is pointing to then increment the byte pointer
// to look at the next value.
// This for loop is for a system that is little endian (i.e. win32)
for (int i = 7; i >= 0; i--)
{
  byteArray[i] = *pByte;
  pByte++;  
} 

注意:自从我使用Visual Studio 2015运行它以来,一切都是小端。这就是为什么第一个for循环按原样编写的原因。

请参阅下文,了解我编写的完整程序列表&amp;测试如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
  long long hex[] = { 0x78661EB54FE76763 };    // Value of an element
  unsigned char* pByte = (unsigned char*)hex;  // Assign a char pointer to point to the first element in hex array
  unsigned char byteArray[8];                  // This is the byte buffer that will be populated from the hex array
  memset(byteArray, 0, sizeof(byteArray));     // Clear out the memory of byte array before filling in the indices

  // Loop through the hex array and assign the current byte
  // the byte pointer is pointing to then increment the byte pointer
  // to look at the next value.
  // This for loop is for a system that is little endian (i.e. win32)
  for (int i = 7; i >= 0; i--)
  {
    byteArray[i] = *pByte;  // Dereference the pointer and assign value to buffer
    pByte++;                // Point to the next byte
  }

  // Print the byte array
  for(int i = 0; i < 8; i++)
  {
    printf("The value of element %i is: %X\n", i, byteArray[i]);
  }

  printf("Press any key to continue...");   // Hold the console window open
  getchar();
  return 0;
}