473,803 Members | 3,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to convert a char array to int array n back

1 New Member
i hav gotta convert an int array to a char array.. so that i can write it to the socket.. that is in the server side..
n in the client side i gotta read d socket... get d char array n convert it into an int array.. so HELP!!!!!!!!!!! !!
May 7 '06 #1
1 11835
Banfa
9,065 Recognized Expert Moderator Expert
Well if you know that the sending and receiving computers have the same endianness (this is byte ordering, a little endian computer must the least significant byte of a multibyte integer in the lowest memory address for the integer, a big endian computer puts the least significant byte of a multibyte integer in the highest memory address for the integer so the bytes of the integer appear in memory in a different order) then you can just cast the pointer something like

Expand|Select|Wrap|Line Numbers
  1.     extern void Send( char *data, int length);
  2.  
  3.     int aIntegers[10];
  4.  
  5.     Send( (char *)aIntegers, sizeof(aIntegers) );
  6.  
however if you are not sure that the sending and receiving computers have the same endianness then you should read and write the bytes of the integer into and out of memory in a specific location so that the values are maintained like so

Expand|Select|Wrap|Line Numbers
  1.     int aIntegers[10];
  2.     int i;
  3.     char aBytes[40];  /* assuming 4 byte ints */
  4.  
  5. /* Int array to byte array */
  6.     for(i=0; i<sizeof(aIntegers)/sizeof(aIntegers[0]); i++)
  7.     {
  8.         aBytes[i*4+0] = (aIntegers[i]>>24) & 0xFF;
  9.         aBytes[i*4+1] = (aIntegers[i]>>16) & 0xFF;
  10.         aBytes[i*4+2] = (aIntegers[i]>>8)  & 0xFF;
  11.         aBytes[i*4+3] = aIntegers[i] & 0xFF;
  12.     }
  13.  
  14.  
  15. /* Byte array to int array */
  16.     for(i=0; i<sizeof(aIntegers)/sizeof(aIntegers[0]); i++)
  17.     {
  18.         aIntegers[i]  = (int)aBytes[i*4+0] << 24;
  19.         aIntegers[i] |= (int)aBytes[i*4+1] << 16;
  20.         aIntegers[i] |= (int)aBytes[i*4+2] << 8;
  21.         aIntegers[i] |= aBytes[i*4+3];
  22.     }
  23.  
This code allows the compiler to correctly handle the endianness of the sending/receiving computer for you.
May 8 '06 #2

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

Similar topics

27
51737
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I have some legcay C code that needs to process a string taken from a textbox, then I need to re-display the string as the textbox->Text. I easily found how to convert from system::string to char but I can't figure out how to go the other way!!
15
34614
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
6
53729
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how do I convert it to: dim myStr as string = "11,22,33,44"
10
26650
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char* and I want to give it a std::string no matching function for call to `MD5::update(std::string&, size_t)' candidates are: void MD5::update(unsigned char*, unsigned int) void PrintMD5(string str){
29
5094
by: Harlin Seritt | last post by:
Hi... I would like to take a string like 'supercalifragilisticexpialidocius' and write it to a file in binary forms -- this way a user cannot read the string in case they were try to open in something like ascii text editor. I'd also like to be able to read the binary formed data back into string format so that it shows the original value. Is there any way to do this in Python? Thanks!
2
2358
by: Steve Kershaw | last post by:
I can convert from a string to a char array (char) by using string.ToCharArray() function. Now how do I convert it back? The string looks like this: "000457". I've converted it to a char array: char = {0, 0, 0, 4, 5, 7}. Now I need to convert it back to a string. Thanks in advance for your help. Steve
8
16253
by: Frank Liebelt | last post by:
Hi I try to convert a int array into a char array. My code: void exec() { char mds; int i; int mdc = {50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};
12
13569
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} System.Text.UTF8Encoding str = new System.Text.UTF8Encoding(); string s = new string((char)107, 1); s += new string((char)62, 1);
14
13546
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h>
0
9562
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,...
1
10289
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
10068
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
9119
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
7600
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
6840
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
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
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
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.