将unsigned char数组的内容分成两半 [英] Divide contents of a unsigned char array into 2 halves

查看:355
本文介绍了将unsigned char数组的内容分成两半的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我我只学习c,我试图将无符号字符数组的内容分成两半,结果可以存储在两个无符号整型数组中,



举例来说,我有一些代码将一个十六进制值添加到BYTE数组中,所以如何将val []的内容拆分为两个,但保持相同的顺序

  #include< stdio.h> 
typedef unsigned char BYTE;
int main()
{
//将十六进制值放入val的示例用途[8]
int i,j;
long long hex = 0x78661EB54FE76763;
BYTE val [8]; (j = 0,i = 7; i> = 0; i - ,j ++){
val [j] =(hex>(i * 8))的

&安培; 0xff的;
}

//如何分割val [8]的内容(现在包含十六进制)

return 0;
}

我试图将十六进制值拆分为78661EB5,4FE76763,里面有一个unsigned int,它存储在我的例子中的val [8]里面

解决方案

创建一个帮助函数, 无符号字符的数组,并调用它两次。



给OP一些事情发现。



$ unsigned long form_big_endian4(const unsigned char * x){
unsigned long y = TBB; //初始值应该是多少?
for(int i = 0; i y * =待定; //提示:8位的最大值+ 1
y + = x [i];
}
return y;
}

#include< stdio.h>
int main(void){
const unsigned char val [8] =
{0x78,0x66,0x1E,0xB5,0x4F,0xE7,0x67,0x63};
unsigned long half = form_big_endian4(val); //为什么长:提示可以有多小的无符号数?
printf(%08lX \\\
,一半); //为什么在格式中为0?为什么`lX`?
half = form_big_endian4(val + TBD); //偏移量有多远?
printf(%08lX \\\
,一半);
返回0;
}


I am wondering if anyone can help me I am only learning c, I am trying to Divide contents of a unsigned char array into 2 halves, which the result can stored in two unsigned int's,

For example purposes, I have some code below which adds a hex value into a BYTE array, so How would split the contents of val[] into two but keep the same order

#include <stdio.h>
typedef unsigned char BYTE;
int main()
{
    // Sample purposes putting hex into val[8]
    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;
    }       

    // How to split the contents of val[8] which now holds the hex

    return 0;  
}

I am trying to split the hex value into 78661EB5, 4FE76763 and store each one inside an unsigned int which is stored inside val[8] in my example

解决方案

Create a helper function that takes a pointer to an array of unsigned char and call it twice.

Left some things out for OP to discover.

unsigned long form_big_endian4(const unsigned char * x) {
  unsigned long y = TBB;           // What should the initial value be?
  for (int i = 0; i < TBD; i++) {  // How many times to loop?
    y *= TBD;                      // Hint: max value of 8 bits + 1
    y += x[i];                     
  }
  return y;
}

#include <stdio.h>
int main(void) {
  const unsigned char val[8] =
      { 0x78, 0x66, 0x1E, 0xB5, 0x4F, 0xE7, 0x67, 0x63 };
  unsigned long half = form_big_endian4(val); // Why long: Hint how small can unsigned be?
  printf("%08lX\n", half);                    // Why 0 in format? Why `lX`?
  half = form_big_endian4(val + TBD);         // How far an offset?
  printf("%08lX\n", half);
  return 0;
}

这篇关于将unsigned char数组的内容分成两半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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