LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-07-2010, 09:20 PM   #1
bvkim
LQ Newbie
 
Registered: Apr 2010
Posts: 23

Rep: Reputation: 15
Question How to convert short array to char array?


I have trouble converting a short array to a char array
Code:
short pShort[4] = { 0x41, 0x42, 0x43, 0x44 };
How to convert this to a char array?
 
Old 06-07-2010, 11:26 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. Declare your character array

2. Copy the elements from your short array into the character array one at a time, e.g. with a "for ( ; ; )" loop.

'Hope that helps .. PSM
 
Old 06-08-2010, 01:36 AM   #3
bvkim
LQ Newbie
 
Registered: Apr 2010
Posts: 23

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by paulsm4 View Post
Hi -

1. Declare your character array

2. Copy the elements from your short array into the character array one at a time, e.g. with a "for ( ; ; )" loop.

'Hope that helps .. PSM
So how to convert from short* to unsigned char** ?
 
Old 06-08-2010, 08:55 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Are your two arrays of the same size?
Do you know the size of the arrays at compile time?

If the answer is yes then you don't need a char** just a char[4] and as paulsm4 said a loop.

Why not try and write a loop allocating the elements one at a time. Given that you are going from a larger memory allocation to a smaller I believe that you will also need to do a cast. But write the loop first and if you have any problems post it along with you code here.
 
Old 06-08-2010, 09:26 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Code:
int i;
for (i=0; i<4; i++)
{
    chars[i] = shorts[i];
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# convert char array to string exodist Programming 3 09-16-2008 08:06 AM
Convert C++ String Vector to char array anamericanjoe Programming 1 12-12-2006 09:29 PM
Convert 64 bit integer to char array in C++ syseeker Programming 2 06-27-2006 03:33 AM
search in char array xxfunkxx Programming 2 12-12-2004 11:23 PM


All times are GMT -5. The time now is 04:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration