convert a int to char array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: convert a int to char array

  1. #1
    Join Date
    May 2015
    Posts
    103

    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 ?

    Thanks a lot for your time in advance
    Last edited by pdk5; December 5th, 2017 at 06:53 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,876

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,876

    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
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  4. #4
    Join Date
    May 2015
    Posts
    103

    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. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,876

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  6. #6
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    701

    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);
    }
    Popular opinion is the greatest lie in the world.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,876

    Re: convert a int to char array

    Quote Originally Posted by AvDav View Post
    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  8. #8
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    701

    Re: convert a int to char array

    Ugh, yeah, that sort of hack would certainly work out for an integer type.
    Popular opinion is the greatest lie in the world.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,876

    Re: convert a int to char array

    Quote Originally Posted by pdk5 View Post
    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;
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,876

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  11. #11
    Join Date
    May 2015
    Posts
    103

    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. #12
    Join Date
    May 2015
    Posts
    103

    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. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,876

    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).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  14. #14
    Join Date
    May 2015
    Posts
    103

    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. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,876

    Re: convert a int to char array

    Quote Originally Posted by pdk5 View Post
    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)