473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Short to char array and back.

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
Sep 14 '07 #1
1 20023
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. short s= 0x1234;
  2. char a[sizeof(short)];
  3.  
  4. memcpy(&s, a, sizeof(short)); // copy a to s
  5. memcpy(a, &s, sizeof(short)); // and back to s again
  6.  
kind regards,

Jos
Sep 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
11
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--- ...
2
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#...
16
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...
9
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...
4
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{
7
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...
8
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 :)
2
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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,...
0
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...
0
jinu1996
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...
0
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...
0
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,...

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.