繁体   English   中英

将十六进制放入字节数组

[英]Putting Hex into a byte array

我试图将十六进制值放入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 []的数组,然后将每个hex值分成2,即78,66,1E,B5,4F,E7,67,63,然后将每个值添加到BYTE类型的数组中将十六进制值保存为8对,因此data [0]的值将为78,而data [8]的值将为63,因此我可以将BYTE类型的数组传递给另一种方法以进行进一步的工作

这是您想要的解决方案:

#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

假设BYTE是一种类型

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

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

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

这是一个非常简单的任务,只需提供给您的初始化值即可完成。

long long hex[] = { 0x78661EB54FE76763 };

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

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循环是这样编写的。

请参阅下面的完整程序清单,我在下面编写并测试了该程序,以证明这一点:

#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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM