473,602 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

int packed as hex with memcpy

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
Jul 19 '05 #1
16 7656

"Delali Dzirasa" <De************ @jhuapl.edu> wrote in message news:bm******** **@houston.jhua pl.edu...
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?


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?
Jul 19 '05 #2
"Delali Dzirasa" <De************ @jhuapl.edu> wrote in message
news:bm******** **@houston.jhua pl.edu...
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?


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.merke rk(at)dse.nl
Jul 19 '05 #3
Delali Dzirasa wrote:
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?


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

Jul 19 '05 #4
Yes they are sent to a file via another program that I am testing, this
program is acting like a simulator and the other application sits and waits
for data then displays them in log files as to what was sent, so I am not
entirely sure how they are bring printed. I read the log files by opening
then as a binary file and viewing the content. here is a bit of
clarification as to what is happening.

AddData( USHORT myVal.....)
{
//in the first case myVal is 20

UCHAR tmp2[2]; //yes unsigned char
tmp2[1] = myVal & 0x00FF;
tmp2[0] = (myVal & 0xFF00) >> 8 ;
memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*

myVal = 0x1500;
tmp2[1] = myVal & 0x00FF;
tmp2[0] = (myVal & 0xFF00) >> 8 ;
memcpy(&myPkt[2], &tmp2, 2); // (where myPkt is a UCHAR*

myVal = 0x0430;
tmp2[1] = myVal & 0x00FF;
tmp2[0] = (myVal & 0xFF00) >> 8 ;
memcpy(&myPkt[4], &tmp2, 2); // (where myPkt is a UCHAR*
}
in the binary file I see : 00 20 15 00 04 30

when I need to be seeing: 00 14 15 00 04 30
When I explicitly assign myVal a hex value (0x.......) it works fine.....but
when it is represented in decimal it pack the that decimal as if it were the
original hex value ( ie 0020, and not 0014);


I hope this is a little more clear

Delali

also when I try to change the code: memcpy(&myPkt[2], &tmp2, 2);
as suggested to: memcpy(myPkt[2], tmp2, 2);" I get the following error

"error C2664: 'memcpy' : cannot convert parameter 1 from 'unsigned char' to
'void *'"

"Peter van Merkerk" <me*****@deadsp am.com> wrote in message
news:bm******** ****@ID-133164.news.uni-berlin.de...
"Delali Dzirasa" <De************ @jhuapl.edu> wrote in message
news:bm******** **@houston.jhua pl.edu...
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?
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.merke rk(at)dse.nl

Jul 19 '05 #5

"Delali Dzirasa" <De************ @jhuapl.edu> wrote in message
news:bm******** **@houston.jhua pl.edu...
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;
Suppose you pass in the decimal value 20. That's 0x14 in hex. The above
line masks off the high word (of a character, which is smaller!), so that
you are doing 0x0014 & 0x00ff, which results in 0x0014. Stored in an
unsigned char, that is 0x14, which is just what you started with: decimal
20!
tmp2[0] = (myVal & 0xFF00) >> 8 ;
Here, you have (0x0014 & 0xff00)>>8, which results in (0x0000) >> 8, which
is 0x0000, or simply decimal 0 (zero). So, your two unsigned chars stored
in tmp2 are 0 and 20.

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?


I think your "hex dump" is not hex at all, but decimal, showing the first
byte as zero, and the second as 20, just like your code told it to do.

I'm not sure why you want to take an unsigned short and store it in two
unsigned characters, but that's hardly "packing", which implies reducing the
space required. What do you need in the output? Characters representing
the hex digits such as ['0','0','1','4']? A pair of unsigned char values,
one for each hex digit, such as [0,0,1,4]? Or do you really need to do this
"packng" at all? I mean, a hex dump of the original decinal value 20 will
show you 0x0014 just like you've been trying to get in the first place.

If you're trying to get the hex digits as separate values, such as
[0,0,1,4], remember that you've got 4 bytes in a short, not 2, and will need
an array of 4 unsigned char's to handle all possible unsigned short values.
And, your masks should mask off one byte at a time, not two like 0x00ff and
0xff00 do.

-Howard

Jul 19 '05 #6
"Delali Dzirasa" <De************ @jhuapl.edu> wrote in message
news:bm******** **@houston.jhua pl.edu...
I would have a number packed with its hex representation of the integer
below is some sample code of what is being done.

I do not really understand what you are trying to do. But I think that
you think that an integer can hold a number in decimal representation and
*in addition* in a, as you say, packed hexadecimal representation. That is
not the case. Assign 20 to an int and you will get the *exact* same bit
pattern when assigning 0x14 to it (which is 00010100 in both cases).

In another post you said that if you use the hexadecimal representation,
it works fine. I do not want to call you a liar, but whether you pass '20'
or '0x14' is completely irrelevant and will produce the *exact* same code.
It might be a good idea to post a new *minimal*, compilable code sample,
which people just have to paste into an new cpp file and compile.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #7

"Delali Dzirasa" <De************ @jhuapl.edu> wrote in message news:bm******** **@houston.jhua pl.edu...

also when I try to change the code: memcpy(&myPkt[2], &tmp2, 2);
as suggested to: memcpy(myPkt[2], tmp2, 2);" I get the following error


I didn't suggest that.

Jul 19 '05 #8

"Delali Dzirasa" <De************ @jhuapl.edu> wrote in message news:bm******** **@houston.jhua pl.edu...
Yes they are sent to a file via another program that I am testing,


Make a complete version of a program that demonstrates the problem AND compiles.
You keep giving us fragments, which look like they ought to be fine. We're not
clairvoyant. The fault is almost certianly in the creation of the value you pass
to AddData.
Jul 19 '05 #9

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

"Delali Dzirasa" <De************ @jhuapl.edu> wrote in message news:bm******** **@houston.jhua pl.edu...
Yes they are sent to a file via another program that I am testing,


Make a complete version of a program that demonstrates the problem AND

compiles. You keep giving us fragments, which look like they ought to be fine. We're not clairvoyant. The fault is almost certianly in the creation of the value you pass to AddData.


I think the fault is actually with the fact the OP is masking off the
high/low words but trying to get bytes:

tmp2[1] = myVal & 0x00FF;
tmp2[0] = (myVal & 0xFF00) >> 8 ;
-Howard
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2922
by: myhotline | last post by:
hi all im very confused about using memcpy and i have three questions....memcpy takes a pointer to src and a pointer to dest and copies src to destination...but im very confuzed about when to use '&' operator while using memcpy....i have code that use '&' and the code that call memcpy without '&' like is the following same Quest1 ---
56
10421
by: ccwork | last post by:
Hi all, Here is a sample code segment: .... typedef PACKED struct { union { PACKED struct { char red:1;
3
3844
by: Amaryllis | last post by:
Hi again, I'm new to the world of communicating between VB.NET and AS/400, so I've been posting a lot of questions lately since no one else in the company has done anything like this before. Hopefully, this one is fairly simple. I'm trying to insert records into a table on the 400. I can insert into a packed field that has no decimal places and into the alphanumeric fields as well. Everything seems to translate except the values that...
3
7370
by: Brian Henry | last post by:
Does anyone know of or know how to convert a COBOL packed decimal in a text file to a decimal that .NET can work with? we are importing Old COBOL data files that have packed data in them and need to convert it to a form we can use. thanks!
3
12128
by: parag.kanade | last post by:
I have a packet structure which has a field of Integer arrays, that is packed struct { int a; char b; int count; }foo; I need to initialize all the entries of count to a particular value,
6
36541
by: Juergen Wohnich | last post by:
Hello, i want to do store int variablen into a char Buffer. Like this: char Buffer; int b = 1447; int *pb; // pb deklariert als pointer auf int pb = &b; // & ist Adress operator, liefert Adresse von b memcpy( Buffer, pb, 4 );
4
5149
by: Spufi | last post by:
I have read several messages on converting packed decimals (EBCDIC) in a C++ program, but they were all for AS400 systems. I am looking for a way to convert the packed decimals in a COBOL program that is on a MVS (z/OS) system in a C++ program. Anyone have any idea how to do this? Is there a header or class that will do this?? Thanks in advanced!!
15
23949
by: Daniel Rudy | last post by:
What is the difference between packed and unpacked structs? -- Daniel Rudy Email address has been base64 encoded to reduce spam Decode email address using b64decode or uudecode -m Why geeks like computers: look chat date touch grep make unzip
9
17585
by: Sikandar | last post by:
Hi, I am beginner in C. Pls let me know what is packed array in C. Where is it used? Thanks, Sikandar
0
7993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.