CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: [RESOLVED] convert a int to char array

1. Member
Join Date
May 2015
Posts
124

## [RESOLVED] convert a int to char array

Hi All,

I am trying to use the stringstream function to convert from int to char array in c++

Lets say int num=1398; //0x576

my char array should have

char array[0]= 0x05;
array[1]=0x76;

Is there any efficient way of doing this type of conversion. Could some c++ experts help in this issue please ?

Last edited by pdk5; December 5th, 2017 at 06:53 AM.

2. ## Re: convert a int to char array

An int on VS is 32 bits (and 64 bits on some systems). So for VS you need an array of 4 chars to be able to represent an int. So

1398 is 00 00 05 76

and 23232456 is 01 62 7f c8

Is this what you are after? If you have

1398 as 05 76 00 00 which is implied by post #1, then this would actually mean 91619328.

And for a 64 bit int, then there would be 8 chars to represent the number.

3. ## Re: convert a int to char array

Assuming that you mean

1398 is 00 00 05 76

then for one solution, consider

Code:
```#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;

union num {
int in;
char ch[sizeof(int)];
};

int main()
{
num n;

n.in = 1398;

reverse(begin(n.ch), end(n.ch));	// For little endian

for (const auto& c : n.ch)
cout << hex << setw(2) << setfill('0') << (int)c << " ";

return 0;
}```
Which gives the output

Code:
`00 00 05 76`

4. Member
Join Date
May 2015
Posts
124

## Re: convert a int to char array

Thanks a lot kaud for the inputs and help. They help me a lot.

I just checked the legacy code, and they do something like

Code:
```   int number = 1398;

char ch[2];

ch[0] = number & 0xFF;
ch[1] = (number >> 8) & 0xFF;```
Last edited by 2kaud; December 5th, 2017 at 12:32 PM.

5. ## Re: convert a int to char array

Yes, but an int can have 16, 32 or 64 bits on some systems - you don't say how many on yours. The code as posted in #4 is for 16 bit integers. This won't work for numbers greater than 32767 (or 65535 if you use unsigned).

For this code to be correct, you should have

Code:
```short number = 1398;
char ch[2];

ch[0] = number & 0xFF;
ch[1] = (number >> 8) & 0xFF;```
where number is now 16 bits.

6. ## Re: convert a int to char array

Another option is to use standard ways of doing this.
Code:
```#include <iostream>
#include <string>

int main()
{
std::cout << std::to_string(0xffffffff);
}```

7. ## Re: convert a int to char array

Originally Posted by AvDav
Another option is to use standard ways of doing this.
Code:
```#include <iostream>
#include <string>

int main()
{
std::cout << std::to_string(0xffffffff);
}```
That's not the same. to_string() converts a number to its equivalent as a string - not to obtain the individual byte representation of the number.

to_string(1398) produces a string containing the characters 1398. The code in post #5 and previous produces a string containing the bytes 0x05 0x76.

8. ## Re: convert a int to char array

Ugh, yeah, that sort of hack would certainly work out for an integer type.

9. ## Re: convert a int to char array

Originally Posted by pdk5
Thanks a lot kaud for the inputs and help. They help me a lot.

I just checked the legacy code, and they do something like

Code:
```   int number = 1398;

char ch[2];

ch[0] = number & 0xFF;
ch[1] = (number >> 8) & 0xFF;```

NOTE that this is for computers with big-endian number layout. For little-endian systems (like Intel), the code would be

Code:
```   short number = 1398;

char ch[2];

ch[1] = number & 0xFF;
ch[0] = (number >> 8) & 0xFF;```

10. ## Re: convert a int to char array

For example code that deals with either big or little endian and different number of bits in the required type, consider

Code:
```#include <iostream>
#include <iomanip>

using namespace std;

using numtype = long long;

const size_t sz = sizeof(numtype);

bool isLittleEndian()
{
const short number = 1;
const char * const numPtr = (const char* const)&number;
return (*numPtr == 1);
}

union num {
numtype in;
char ch[sz];
};

int main()
{
numtype number = 1398;

const bool le = isLittleEndian();
char ch[sz] = {0};

for (size_t n = 0; n < sz; ++n) {
const size_t elem = le ? sz - n - 1 : n;
ch[elem] = (number >> (8 * n)) & 0xff;
}

for (const auto& c : ch)
cout << hex << setw(2) << setfill('0') << (int)c << " ";

cout << endl;

num n;

n.in = 1398;

if (le)
reverse(begin(n.ch), end(n.ch));

for (const auto& c : n.ch)
cout << hex << setw(2) << setfill('0') << (int)c << " ";

cout << endl;
}```
This demonstrates two of the ways.
Last edited by 2kaud; December 7th, 2017 at 06:14 AM.

11. Member
Join Date
May 2015
Posts
124

## Re: convert a int to char array

@kaud: Brilliant, thanks a lot for the thoughts , inputs really helped me a lot.

Yes, the number was short int.

typedef unsigned short int uint16_t;
typedef uint16_t u16;

u16 number=1398;

12. Member
Join Date
May 2015
Posts
124

## Re: convert a int to char array

@ kaud: Im sorry again, but it looks like my g++ compiler [
g++ (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4)] does not recognise the "using numtype".

13. ## Re: convert a int to char array

The c++ compiler isn't recognising c++11 statements. Is there a compiler option to enable c++11? Replace using numtype with

Code:
`typedef long long numtype;`
and replace long long with what ever type is required (eg int, short etc).

14. Member
Join Date
May 2015
Posts
124

## Re: convert a int to char array

Thanks a lot kaud. Google tells me that , -std=c11 can be used but we need at least gcc 4.7 to have this option supported.

15. ## Re: convert a int to char array

Originally Posted by pdk5
Thanks a lot kaud. Google tells me that , -std=c11 can be used but we need at least gcc 4.7 to have this option supported.
Note that if you are using a pre c++11 compiler (c++98?), the c++ language has changed a lot since then. The current standard is c++17. I would suggest investigating upgrading to the latest gcc compiler.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•