将Char缓冲区转换为一个短数组

将Char缓冲区转换为一个短数组,c,casting,char,buffer,short,C,Casting,Char,Buffer,Short,我有一个由API函数填充的char*缓冲区。我需要获取该指针包含的数据,将其转换为未签名的Short,并将其转换为网络(htons())格式,以便通过UDP发送。这是我的代码(并非所有原因) 下面的代码可以工作,但另一端的数据不好(不是短路或网络翻译) 任何帮助都将不胜感激。谢谢。如果您的字符数组以null结尾,则只需执行以下操作: for (int i=0; i<strlen(CHAR_ARRAY); i++) short_buffer[i] = (unsigned short

我有一个由API函数填充的char*缓冲区。我需要获取该指针包含的数据,将其转换为未签名的Short,并将其转换为网络(htons())格式,以便通过UDP发送。这是我的代码(并非所有原因)

下面的代码可以工作,但另一端的数据不好(不是短路或网络翻译)


任何帮助都将不胜感激。谢谢。

如果您的字符数组以null结尾,则只需执行以下操作:

for (int i=0; i<strlen(CHAR_ARRAY); i++)
     short_buffer[i] = (unsigned short) CHAR_ARRAY[i];

for(int i=0;i假设您的缓冲区中有4080个字节,由16位样本组成,这意味着您的缓冲区的4080个字节中总共有2040个16位样本(为标头保留了16个字节)。因此,您可以执行以下操作:

#define MAXBUFSIZE 4096
#define MAXSHORTSIZE 2040

unsigned char pcZap[MAXBUFSIZE];
unsigned ushort[MAXSHORTSIZE];

//get the value of the returned packed length in uLen, and the header in uLob

unsigned short* ptr = (unsigned short*)(pcZap + uLob);
for (int i=0; i < ((uLen - uLob) / 2); i++)
{
    ushort[i] = htons(*ptr++);
}
#定义MAXBUFSIZE 4096
#定义MAXSHORTSIZE 2040
无符号字符pcZap[MAXBUFSIZE];
未签名的ushort[MAXSHORTSIZE];
//获取返回的压缩长度(以uLen为单位)的值,以及标题(以uLob为单位)的值
无符号短路*ptr=(无符号短路*)(pcZap+uLob);
对于(int i=0;i<((uLen-uLob)/2);i++)
{
ushort[i]=htons(*ptr++);
}

现在,您的
ushort
数组将由网络字节顺序
unsigned short
值组成,这些值是从
pcZap
数组中的原始值转换而来的
,请确保使用来自
ushort
的值,而不是来自
pcZap

的值。如果您只需要将表示主机端到网络端的短整数的字节块转换为网络端,请执行以下操作:

size_t i;
size_t len = uLen - 16 - uLob;
size_t offset = uLob + 16;

if(len % 2 != 0) {
  ..error not a multiple of 16 bit shorts...
}
//now, if you're on a little endian host (assuming the shorts in 
//pcZap is laid out as the host endian...), just swap around the bytes
//to convert the shorts to network endian.
for(i = 0; i < len; i+=2) {
    //swap(&pcZap[offset + i],&pcZap[offset + i + 1]);
    char tmp = pcZap[offset + i];
    pcZap[offset + i] =  pcZap[offset + i + 1]; 
    pcZap[offset + i + 1] = tmp;
}
//if you're on a big endian host, forget the above loop, the data
//is already in big/network endian layout.

//and just send the data.
if(sendto(sockFd,pcZap + offset,len,0,(struct sockaddr *)&Server_addr,
               sizeof Server_addr) == -1) {
   perror("sendto");
}
size\u ti;
尺寸长度=uLen-16-uLob;
尺寸偏差=uLob+16;
如果(长度%2!=0){
..错误不是16位短路的倍数。。。
}
//现在,如果你在一个小小的endian主机上(假设短裤在
//pcZap被布置为主机端,只需交换字节即可
//将短裤转换为网络端点。
对于(i=0;i

请注意,您的代码在sendto()调用中有
sizeof(struct sockaddr)
,这是错误的,您希望它是服务器地址的实际大小。

您想再次将其转换为什么?有多少个“短路”呢?糟糕的是,我无法确定从get\u packet()返回的字符缓冲区中有多少个“短路”,我只知道我请求的缓冲区的大小,以及缓冲区是否被正确填充。我知道我得到的字符*的头是4个32位的字,之后的每个字都是2个16位的样本(短),达到我请求的大小。-编辑(我应该知道大小,因为我给它一个大小,前4个32位字是头,4096-16 4080是我的示例缓冲区的大小)如果您不知道有多少个,如何复制它们?1020是我在缓冲区中包含的16位样本数。@blsmit5728听起来可能是uLen或uLob或return_val将返回提取数据的大小-请尝试验证,或者它以某种方式嵌入到4*32位头中。我们无法猜测,因为我们不知道保存get_数据包()的所有文档等等。但一旦你知道了这一点,就只需要编写一个非常简单的循环。我需要我们在char缓冲区中包含所有未签名的短样本,这样我就可以对其进行强制转换并发送它。我将取下它并对其进行建模以进行尝试。看起来在我网络的另一端,我只得到了第一个16位样本。我们不应该是stepp吗通过pcZap进行初始化?pcZap++?
ptr
指向
pcZap
(一个临时指针),我们在for循环中增加它。因此,当我们增加
ptr
的值时,它指向数组中的下一个无符号短元素。是的!明白了!愚蠢的我忘记了发送ptr时发送的无符号短缓冲区
size_t i;
size_t len = uLen - 16 - uLob;
size_t offset = uLob + 16;

if(len % 2 != 0) {
  ..error not a multiple of 16 bit shorts...
}
//now, if you're on a little endian host (assuming the shorts in 
//pcZap is laid out as the host endian...), just swap around the bytes
//to convert the shorts to network endian.
for(i = 0; i < len; i+=2) {
    //swap(&pcZap[offset + i],&pcZap[offset + i + 1]);
    char tmp = pcZap[offset + i];
    pcZap[offset + i] =  pcZap[offset + i + 1]; 
    pcZap[offset + i + 1] = tmp;
}
//if you're on a big endian host, forget the above loop, the data
//is already in big/network endian layout.

//and just send the data.
if(sendto(sockFd,pcZap + offset,len,0,(struct sockaddr *)&Server_addr,
               sizeof Server_addr) == -1) {
   perror("sendto");
}