Convert Integer to Hex in an array of chars

This is a discussion on Convert Integer to Hex in an array of chars within the C Programming forums, part of the General Programming Boards category; Hey there, I want to know if there is a routine that can take in an array of char, and ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    2

    Convert Integer to Hex in an array of chars

    Hey there,

    I want to know if there is a routine that can take in an array of char, and an integer, convert the integer to hex and store the hex representation as text in the char array.

    For example, if integer is 123, its hex representation is 0x7B, so it can return 7 and B in the char array.

    Any routine/code/function would be helpful.

    Thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    int main( void )
    {
    	const int len = 1024;
    	char buf[ len + 1 ];
    	int val = 123;
    	sprintf( buf, "%x", val ); // or if avaliable: snprintf( buf, len, "%x", val );
    	puts( buf );
    	return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    const int len = 1024;
    char buf[ len + 1 ];
    Every time I try initializing an array with a const in Visual Studio , I get an error saying
    Expected constant expression.
    Cannot allocate array of size 0.
    This only happens in C and not C++. Although, it works if I use a macro -

    Code:
    #define LEN 1024
    char buf[ LEN  + 1 ];
    Does anyone know why that is ? I thought the const keyword was added to the C standard.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    C recognizes the const keyword, but regardless if the value used to create the array is const or not, in C it is still considered a variable length array. In other words you can only do that if you are using C99; it is illegal in C90.

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Ah, I see. So Visual Studio supports C90 then.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Quote Originally Posted by Spidey View Post
    Ah, I see. So Visual Studio supports C90 then.
    Most compilers still only support C90. Even though C99 is almost a decade old, there are very few compilers which are fully C99 compliant.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I did try writing a quick code to check this scenario on Dev-C++. It turned out to be compiling with no warning. Which I guess I’m running C90 complaint compiler. But surely there should be somewhere in the IDE where I can find this information. Any ideas where?

    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    Many popular compilers can have C99 compliance explicitly turned on, actually. Usually the website of the developers of your compiler can field questions about compliance.

  9. #9
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Ah, I see. I never noticed that. I found one called lcc, I guess I'll try it out now

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I did try writing a quick code to check this scenario on Dev-C++. It turned out to be compiling with no warning. Which I guess I’m running C90 complaint compiler. But surely there should be somewhere in the IDE where I can find this information. Any ideas where?

    Not really. The IDE is just a front end for whatever compiler you set it up to use, and as such it doesn't know the particulars of the compiler (VS is probably an exception, but then there isn't such a clear distinction between compiler and IDE there).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Yeah, usually a compiler won't warn about the parts of C99 that it supports. For instance, it will compile fine in GCC if you do:
    Code:
    gcc -Wall file.c
    But it will warn you if you do:
    Code:
    gcc -Wall -pedantic file.c

  12. #12
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    I have used lcc for embedded projects. I would highly recommend against using it for something you don't absolutely have to use it on. It is not the best compiler in the world.

  13. #13
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    I have used lcc for embedded projects. I would highly recommend against using it for something you don't absolutely have to use it on. It is not the best compiler in the world.
    Yeah, I'm fine with Visual Studio. Just trying out some C99!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Byte Array / Integer conversion
    By AdLab in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 09:04 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21