Professional Web Applications Themes

Decimal to Hex in g++ - UNIX Programming

How do i convert a Decimal number to Hex in g++, I know there is not an itoa function so what is the best way to do this? would it be: char *hexstring[10]; int number = 30; sprintf(hexstring, "%x", number); I will be passing this number to another program which will change the hex to binary. Any help would really be appreciated. Thank You...

  1. #1

    Default Decimal to Hex in g++

    How do i convert a Decimal number to Hex in g++, I know there is not
    an itoa function so what is the best way to do this?

    would it be:

    char *hexstring[10];
    int number = 30;
    sprintf(hexstring, "%x", number);

    I will be passing this number to another program which will change the
    hex to binary. Any help would really be appreciated. Thank You
    Josh Guest

  2. #2

    Default Re: Decimal to Hex in g++

    Josh Parker <ecu.edu> wrote: 
     
     

    Drop the '*' in front of hexstring, or you get an array of 10
    uninitialized char pointers instead of an array with enough space
    for 9 characters.
     

    Otherwise that looks completely reasonable, at least as long as you're
    on a typical 32-bit machine. To make sure it works with whatever size
    an integer has on a machine you could use instead

    char hexstring[ ( CHAR_BIT * sizeof( int ) ) / 4 + 2 ];

    CHAR_BIT is the number of bits in the smallest unit on your machine
    (a char per definitionem), which is typically 8, but could be any
    other larger value in principle (don't forget to include <climits>
    or <limits.h> when you're using it). The additional 2 is one for
    the '\0' at the end of the string and another one for cases where
    CHAR_BIT can't be divided by 4.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ fu-berlin.de
    \__________________________ http://www.toerring.de
    Jens.Toerring@physik.fu-berlin.de Guest

  3. #3

    Default Re: Decimal to Hex in g++

    fu-berlin.de wrote: [/ref]
     [/ref]

    One thing I forgot: At least in C (don't know about C++ but I would guess
    it's the same) the "%x" is only allowed for _unsigned_ ints.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ fu-berlin.de
    \__________________________ http://www.toerring.de
    Jens.Toerring@physik.fu-berlin.de Guest

  4. #4

    Default Re: Decimal to Hex in g++

    fu-berlin.de writes:
     [/ref]
    > [/ref]
    >
    > One thing I forgot: At least in C (don't know about C++ but I would guess
    > it's the same) the "%x" is only allowed for _unsigned_ ints.
    >[/ref]

    And better use snprintf() if your system has one instead of sprintf().

    Bye, Dragan

    --
    Dragan Cvetkovic,

    To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

    !!! Sender/From address is bogus. Use reply-to one !!!
    Dragan Guest

  5. #5

    Default Re: Decimal to Hex in g++

    Josh Parker wrote:
     
    #include <iostream>
    #include <sstream>

    int main()
    {
    std::ostringstream ss;

    int num = 30;

    ss << std::hex << num;

    std::cout << ss.str() << "\n";

    return 0;
    }

    Greg Guest

Similar Threads

  1. decimal vs. int vs. numeric ???
    By Robin in forum ASP Database
    Replies: 8
    Last Post: May 14th, 06:57 PM
  2. Corrupted decimal?
    By Bas in forum FileMaker
    Replies: 3
    Last Post: February 15th, 02:55 PM
  3. Decimal result
    By Simon in forum Microsoft SQL / MS SQL Server
    Replies: 7
    Last Post: August 6th, 07:45 PM
  4. converting HH:MM to a decimal
    By John David Adamski in forum Informix
    Replies: 0
    Last Post: July 7th, 04:28 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139