473,804 Members | 3,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing unsigned char[]

hi

got a little prob with data types.
I´m reading data from a socket in to a unsigned char buf[4096]; .
Now, lets say there are 60Bytes written into the array( but it is
always different).
Now i want to give the bytes to a other class like:

pushControlPack et(buf);

but i only want to give the recieved data, not the whole buffer (4096)
icluding the length of the recieved data.

should i cast the recieced data into a std::string and define the
function like this:

pushControlPack et(std::string) ;

or is there a way to define a function for variable chararrays like
this:

pushControlPack et(unsigned char[]);

it should be "call by value"

thankx

Oct 14 '06 #1
2 3140
bubzilla wrote:
hi

got a little prob with data types.
I´m reading data from a socket in to a unsigned char buf[4096]; .
Now, lets say there are 60Bytes written into the array( but it is
always different).
Now i want to give the bytes to a other class like:

pushControlPack et(buf);

but i only want to give the recieved data, not the whole buffer (4096)
icluding the length of the recieved data.

should i cast the recieced data into a std::string and define the
function like this:

pushControlPack et(std::string) ;
That is certainly one way. You can also use std::vector<cha rinstead
of string.

Most of the socket apps I've used I create a special container type but
unless you're pushing a huge amount of data, std::string is fine.
>
or is there a way to define a function for variable chararrays like
this:

pushControlPack et(unsigned char[]);

it should be "call by value"
You can't pass arrays by value, passing an array always is done by
passing a reference or degenerating to the pointer to the first object
in the array.
>
thankx
Oct 14 '06 #2

Gianni Mariani wrote:
bubzilla wrote:
hi

got a little prob with data types.
I´m reading data from a socket in to a unsigned char buf[4096]; .
Now, lets say there are 60Bytes written into the array( but it is
always different).
Now i want to give the bytes to a other class like:

pushControlPack et(buf);

but i only want to give the recieved data, not the whole buffer (4096)
icluding the length of the recieved data.

should i cast the recieced data into a std::string and define the
function like this:

pushControlPack et(std::string) ;

That is certainly one way. You can also use std::vector<cha rinstead
of string.

Most of the socket apps I've used I create a special container type but
unless you're pushing a huge amount of data, std::string is fine.
The OP has a buffer of unsigned char. Unless you are happy with the
consequences of converting unsigned char to char[*], std::string and
std::vector<cha rare not suitable. Use a std::vector<uns igned char>.
[*] which will either be nothing, if char has the same representation
as unsigned char, or will be implementation defined (i.e. not
necessarily portable, and you need to look it up) if char has the same
representation as signed char.

Gavin Deane

Oct 14 '06 #3

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

Similar topics

8
3971
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
58
10188
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
15
7987
by: Carramba | last post by:
hi! I am trying to confirm if input is digit, so I thought it would by easy to do it with with isdigit() funktion, but how do I pass arrays to it if the imput is more then 1 sign? #include <stdio.h> #include <ctype.h> #include <stdlib.h> int main(){
5
3494
by: Bucky Pollard | last post by:
I am calling a PVCS DLL function from C#. According to the documentation, I can pass NULL to any output parameter that I do not want to receive data back for. How do I do this? Every thing I've tried results in a compiler error. TIA: Bucky Pollard My declaration is below. I am only looking for one piece of information, so ultimately I'd like to call the function something like this.
1
3271
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be irrelavent. My syntax is surely where I am going wrong. I'd like to be able to call this routine to read different values from another device. This routine would be called quite simply as follows: void main() {
2
2443
by: jeniffer | last post by:
gcc -c test.c gcc -o test test.c ./test i=6 0 14 22 42 48 e6 014224248ffffffe6 ea =
9
7635
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 the function foo(). A pointer to char ** list is passed to foo() as an argument. The problem is that when foo() returns, char ** list contains rubbish
0
1058
by: MichK | last post by:
Hi, I have a problem with passing a pointer in visual basic. The thing is I receive an array address of a certain API I call in visual basic. There is no value passed, just an address in the form of a long. The address is the location of the first element of an array. Now I need to pass this array to a DLL function. This function uses the adress to copy the array to a different one.
11
7197
by: Bob Yang | last post by:
Hi, I have this in C++ and I like to call it from c# to get the value but I fail. it will be good if you can give me some information. I tried it in VB.net it works but I use almost the same way as VB in C# but it doens't work. c++: (csp2.dll) NoMangle long DLL_IMPORT_EXPORT csp2TimeStamp2Str(unsigned char *Stamp, char *value, long nMaxLength);
0
9706
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
9579
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
10575
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
7616
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
6851
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();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.