473,372 Members | 1,357 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,372 software developers and data experts.

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 10808

"Tricky" <Tr********@gmail.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.invalidwrote:
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.invalidwrote:
>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
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...
4
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...
4
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 ...
16
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
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! ...
18
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
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...
18
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...
9
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....
0
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.