Hello,
I been working on an application that need to build a data package to send it over the network, I have an unsigned short as the data length, this is two bytes.
On the server side I am working on C# and on the client side I am working on C++.
What I precisely need if to mimic some functionality I have on C# provided by the .net Framework.
System.BitConverter.GetBytes(value)
This function takes many data types (unsigned short for my need) and returns an array of bytes with the corresponding value of the data I gave it as parameter.
number 512 becomes,
byte 0 = 0
byte 1 = 2
and the other function that takes a byte array and returns an unsigned integer in my case is
System.BitConverter.ToUInt16(byte array,starting index)
this function takes two bytes from the starting index on the byte array becuase UInt16 is two bytes, and returns an unsigned short.
array = { 0,2}
returns 512.
How is the best way to do this, I have tried this method.
clen = new char[2];
*(unsigned short *)clen = m_length;
or
unsigned short* num_addr = &m_length;
char* num_val = (char*)num_addr;
this give me tha byte array from the short number and the other direction i have implemented this way
number = *(unsigned short *) array;
it works but someone pointed me that using this aproach could be risky in terms of memory handling, that pointing to memory areas with different types of pointers culd lead to problems.
So, which is the best way to do it?
Thanks
1 20023
Char (byte) arrays may be aligned on odd number boundaries so just casting
the address of that array to an (unsigned?) short may be tricky. Better construct
your short from the two char elements in the array; you have to decide on big or
little endianness though.
Vice versa doesn't work either because of that alignment; better decompose the
(unsigned?) short into the individual byte (char) values for your array.
Or use memcpy() back and forth: -
short s= 0x1234;
-
char a[sizeof(short)];
-
-
memcpy(&s, a, sizeof(short)); // copy a to s
-
memcpy(a, &s, sizeof(short)); // and back to s again
-
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: J. Campbell |
last post by:
I'm a novice with c/c++ and have been reading Eckel's book. I'd like
some feedback on using this method. What I need to do is treat a
string as numeric data. I know how to write functions to...
|
by: Pontus F |
last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers
and other C/C++ concepts. I would appreciate if somebody could explain
what's wrong with this code:
---begin code block---
...
|
by: vikas |
last post by:
I have following structure in c++.
typedef struct MMF_result_struct
{
int action;
char text;
int cols,rows;
int month,day,year;
} MMF_result;
Now this structure is shared between C++ and C#...
|
by: halukg |
last post by:
I am trying to send a 6 byte char array from the serial port in new C# 2005
Express:
com.Write(new string(new char { (char)34, (char)14, (char)192, (char)51,
(char)0, (char)0 }, 0, 6));
I am...
|
by: rob.kirkpatrick |
last post by:
Hello
I need to populate an array of char arrays at run-time. A very
simplifed version of the code is below.
char ** list should contain cnt char arrays. The values of char ** list
are set by...
|
by: Rahul |
last post by:
Hi,
I have following two classes
class base{
int i;
public:
virtual void fun() { cout<<i<<"Base \n"; }
};
class d: public base{
|
by: Neo |
last post by:
Hi,
I have a programs that reads char ouput and should send a signal when
it reads "login". I am currently reading into a char array but how do
I check for the match? I don't think I can do-
char...
|
by: Tricky |
last post by:
Is there a way I can easily convert a char array into an int array,
without having to cast each value in the array to an int and copying
it to a new array?
Thanks for any help :)
|
by: sam.barker0 |
last post by:
Hi guys,
I am trying to form an IPV6 address string from the address bytes
contained in a unsigned char buffer
char tempstring;
sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned...
|
by: Bats |
last post by:
I'm writing an XSUB to glue an existing C module to my perl code, but I'm kind of new to C.
I want to save 2 shorts and n unsigneds in a char * array (so I can return the array to the perl module...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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,...
| |