473,671 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting Char array to Int array

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 :)
Feb 4 '08 #1
8 10825

"Tricky" <Tr********@gma il.comwrote in message
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 :)
Basically no. On some very unusual machines sizeof(char) == sizeof(int) and
so you can reinterpret the bits. However generally you've got to add extra
padding within the array because it consists of two or, more usually, four
bytes.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Feb 4 '08 #2
Tricky wrote:
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?
It depends on what you mean by "convert." Please describe
exactly what you have, and exactly what you want to get.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Feb 4 '08 #3
On Feb 4, 4:31 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
Tricky wrote:
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?

It depends on what you mean by "convert." Please describe
exactly what you have, and exactly what you want to get.

--
Eric Sosman
esos...@ieee-dot-org.invalid
Ive read all of the data out of a Bitmap (Im ignoring anything other
than 8 bits). Because I may want to manipulate them as if they were 10
bit values I need to store them into something wider than a char
array.

No need to worry about the 10 bit thing, I know Im going to have to
bit shift them all left by 2 once they are in the int array. Its just
getting the values into the int array in the first place.
Feb 4 '08 #4
Tricky wrote:
) Ive read all of the data out of a Bitmap (Im ignoring anything other
) than 8 bits). Because I may want to manipulate them as if they were 10
) bit values I need to store them into something wider than a char
) array.
)
) No need to worry about the 10 bit thing, I know Im going to have to
) bit shift them all left by 2 once they are in the int array. Its just
) getting the values into the int array in the first place.

If you have to bit shift them anyway, then why can't you copy them
while you're at it ?
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Feb 4 '08 #5
Tricky wrote:
On Feb 4, 4:31 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
>Tricky wrote:
>>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?
It depends on what you mean by "convert." Please describe
exactly what you have, and exactly what you want to get.

Ive read all of the data out of a Bitmap (Im ignoring anything other
than 8 bits). Because I may want to manipulate them as if they were 10
bit values I need to store them into something wider than a char
array.

No need to worry about the 10 bit thing, I know Im going to have to
bit shift them all left by 2 once they are in the int array. Its just
getting the values into the int array in the first place.
Since you need each value in a form different than the
one you read, you need to convert it explicitly to that new
form. There's no "bulk conversion" operator.

unsigned char orig[N];
unsigned int new[N]; /* or maybe unsigned short? */
int i;
/* read orig[] values */
for (i = 0; i < N; ++i)
new[i] = orig[i]; /* or mabye orig[i] << 2? */

--
Eric Sosman
es*****@ieee-dot-org.invalid
Feb 4 '08 #6
Tricky wrote:
>
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?
Run the program on a machine where CHAR_BIT exceeds 15 and
sizeof(int) is 1. :-)

Of course you can eliminate the cast on any machine.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 4 '08 #7
CBFalconer wrote:
Tricky wrote:
>>
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?

Run the program on a machine where CHAR_BIT exceeds 15 and
sizeof(int) is 1. :-)
On the DS9K, sizeof(int) is 1, but int is two's complement and char is
sign-and-magnitude. :-)
--
Army1987 (Replace "NOSPAM" with "email")
Feb 5 '08 #8
Tricky:
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 :)


Nope, not on a system where char and int are different sizes and have
different representations . You'd have to go with something like:

int nums[50] = { /* numbers */ };

char signed byte_nums[50];
int const *src = nums;

char signed *dest = byte_nums;
char signed const *const destend = *(&byte_nums+1) ;

do *dest++ = *src++;
while (destend != dest);

--
Tomás Ó hÉilidhe
Feb 5 '08 #9

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

Similar topics

5
13917
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do something like: char chars = "Hello!"; byte bytes = (byte) chars;
4
16460
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope someone can help Thanks
4
5423
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is typedef struct Rec { unsigned char msg; unsigned long len;
16
1939
by: x | last post by:
converting 1944 to '1','9','4','4' how can I convert a number such as 1944 to a character array? thanks!
8
4771
by: Ramiro Barbosa, Jr. | last post by:
All, Any ideas on how to convert the first 8 bytes of raw uninterpreted sequence of bytes from 'char array;' (populated with _binary_ data read from a socket), into a 'long id'? Thank you! -RB
18
26707
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
4
5384
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use "System.Text.Encoding.ASCII.GetString(bytearray)" of .Net library, we found that the char (delimiters) specified above are replaced with different char.
18
7944
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify the string directly, I convert the string to a char using .ToCharArray() and set the first letter of each word to uppercase. The problem then is I can't figure out how to convert the char array back to a string. Whether I use .ToString() from the...
9
30508
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats. How can I convert the sets of 4 bytes to floats? Thanks, Greg Book
0
2025
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual Studio 2005). The problem occurred when I trying to opening and reading file: ============= C++ ============= void LoadSourceFile(char * fileName, unsigned char * data, int length)
0
8474
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
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8597
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
7428
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...
0
5692
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
4222
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...
1
2809
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
2049
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1807
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.