int用memcpy打包为十六进制 [英] int packed as hex with memcpy

查看:128
本文介绍了int用memcpy打包为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会有一个数字包含其整数的十六进制表示

下面是一些示例代码。


int value = 20; //十六进制是0x14

AddData(value);

..

..

..


AddData(USHORT myVal .....)

{

UCHAR tmp2 [2];

tmp2 [1] = myVal& 0x00FF;

tmp2 [0] =(myVal& 0xFF00)>> 8;


memcpy(& myPkt [0],& tmp2,2); //(其中myPkt是UCHAR *

}


当我检查十六进制转储时我看到0x0020被打包而不是0x0014

我想要的。如何设置正确的标志(?)如果这是解决方案

打包整数的十六进制表示?

谢谢,非常感谢你的帮助!

Delali

I would have a number packed with its hex representation of the integer
below is some sample code of what is being done.

int value = 20; //in hex it is 0x14

AddData (value);
..
..
..

AddData( USHORT myVal.....)
{
UCHAR tmp2[2];
tmp2[1] = myVal & 0x00FF;
tmp2[0] = (myVal & 0xFF00) >> 8 ;

memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*
}

when I check the hex dump I see that 0x0020 was packed instead of the 0x0014
that I want. How can I set the proper flags(?) "if" that is the solution to
pack the hex representation of the integer?
Thanks, your help is greatly appreciated!
Delali

推荐答案



" Delali Dzirasa"< De ************ @ jhuapl.edu>在留言中写道:bm ********** @ houston.jhuapl.edu ...

"Delali Dzirasa" <De************@jhuapl.edu> wrote in message news:bm**********@houston.jhuapl.edu...
我会有一个数字包含其整数的十六进制表示
下面是一些示例代码正在做什么。

int value = 20; // in hex它是0x14
AddData(USHORT myVal .....)
{UCHAR tmp2 [2];
大概是UCHAR是无符号字符?tmp2 [1] = myVal& 0x00FF;
tmp2 [0] =(myVal& 0xFF00)>> 8;

memcpy(& myPkt [0],& tmp2,2); // (其中myPkt是UCHAR *
memcpy(myPkt,tmp2,sizeof tmp2);

当我检查十六进制转储时我看到0x0020被打包而不是0x0014
我想要。如何设置正确的标志(?)if这是解决整数的十六进制表示的解决方案吗?
I would have a number packed with its hex representation of the integer
below is some sample code of what is being done.

int value = 20; //in hex it is 0x14 AddData( USHORT myVal.....)
{
UCHAR tmp2[2]; presumably UCHAR is unsigned char? tmp2[1] = myVal & 0x00FF;
tmp2[0] = (myVal & 0xFF00) >> 8 ;

memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR* memcpy(myPkt, tmp2, sizeof tmp2);
when I check the hex dump I see that 0x0020 was packed instead of the 0x0014
that I want. How can I set the proper flags(?) "if" that is the solution to
pack the hex representation of the integer?




没有标志,并且没有像十六进制表示这样的东西代码。

上面已存储的值为20十进制和14十六进制和24八进制

等... tmp2 [1]。是什么让你想到的呢?你确定你的翻斗车是真的吗?b $ b显示十六进制的字节?



There are no flags and there''s no such thing as a hex representation in the code.
The above have stored the value which is both 20 decimal and 14 hex and 24 octal
etc...tmp2[1]. What makes you think otherwise? Are you sure your dumper is really
showing you the bytes in hex?


" Delali Dzirasa" <德************ @ jhuapl.edu>在消息中写道

news:bm ********** @ houston.jhuapl.edu ...
"Delali Dzirasa" <De************@jhuapl.edu> wrote in message
news:bm**********@houston.jhuapl.edu...
我的数字会以十六进制打包整数的表示
下面是一些示例代码。

int value = 20; file://十六进制是0x14

AddData(值);




AddData(USHORT myVal。 ....)
{UCHAR tmp2 [2];
tmp2 [1] = myVal& 0x00FF;
tmp2 [0] =(myVal& 0xFF00)>> 8;

memcpy(& myPkt [0],& tmp2,2); //(其中myPkt是UCHAR *
}当我检查十六进制转储时,我看到0x0020被打包而不是我想要的
0x0014。如何设置正确的标志(?)如果是解决方案
打包整数的十六进制表示?
I would have a number packed with its hex representation of the integer
below is some sample code of what is being done.

int value = 20; file://in hex it is 0x14

AddData (value);
.
.
.

AddData( USHORT myVal.....)
{
UCHAR tmp2[2];
tmp2[1] = myVal & 0x00FF;
tmp2[0] = (myVal & 0xFF00) >> 8 ;

memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*
}

when I check the hex dump I see that 0x0020 was packed instead of the 0x0014 that I want. How can I set the proper flags(?) "if" that is the solution to pack the hex representation of the integer?




遗憾的是,您发布的代码不是可编译,因为AddData()没有

有一个返回类型,并且UCHAR和USHORT没有在C ++
标准中定义。也就是说,我在你的代码中看不到任何内容这可以解释你的十六进制转储中的

值。你确定你使用的工具是显示十六进制格式而不是十进制格式的数据吗?


-

Peter van Merkerk

peter.van.merkerk(at)dse.nl



The code you posted is unfortunately not compilable since AddData() does not
have a return type, and UCHAR and USHORT are not defined in the C++
standard. That being said, I see nothing in your code that would explain the
values in your hex dump. Are you sure that the tool you are using is
displaying the data in hexadecimal format and not decimal format?

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Delali Dzirasa写道:
Delali Dzirasa wrote:
我将有一个数字打包其整数的十六进制表示
下面是一些示例代码是什么是d一。


您应该首先解释一下您在hex

表示整数下所理解的内容。并在用其六角形包装数字

表示在这种特殊情况下。 十六进制表示在'
本质上是一系列来自'''''''''''''''''''''''或'/'$ b $这样的事情。我没有在你的代码中看到任何与获得任何数字的十六进制表示有任何关系的内容,更不用说

打包了。 />
int value = 20; //十六进制是0x14

AddData(值);




AddData(USHORT myVal ... ..)
{UCHAR tmp2 [2];
tmp2 [1] = myVal& 0x00FF;
tmp2 [0] =(myVal& 0xFF00)>> 8;

memcpy(& myPkt [0],& tmp2,2); //(其中myPkt是一个UCHAR *
}当我检查十六进制转储时,我看到0x0020被打包而不是我想要的0x0014
我怎么能设置正确的标志(?)if是解决整数的十六进制表示的解决方案吗?
I would have a number packed with its hex representation of the integer
below is some sample code of what is being done.
You should probably start by explaining what you understand under "hex
representation of the integer" and under "packing a number with its hex
representation" in this particular case. "Hex representation" is in
essence a sequence of characters from ''0''..''9'', ''A''..''F'' set or
something like this. I don''t see anything in your code that has anything
to do with obtaining a hex representation of any number, let alone the
"packing".
int value = 20; //in hex it is 0x14

AddData (value);
.
.
.

AddData( USHORT myVal.....)
{
UCHAR tmp2[2];
tmp2[1] = myVal & 0x00FF;
tmp2[0] = (myVal & 0xFF00) >> 8 ;

memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*
}

when I check the hex dump I see that 0x0020 was packed instead of the 0x0014
that I want. How can I set the proper flags(?) "if" that is the solution to
pack the hex representation of the integer?




-

祝你好运,

Andrey Tarasevich

Brainbench C和C ++编程MVP



--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP


这篇关于int用memcpy打包为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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