Professional Web Applications Themes

Integer to Hex string? - Mac Programming

Hi, Are there any functions to convert an integer in to a Hex string (any string type)? I know about using the std 'hex' routine to cout to the screen, but I need to store a string. Thanks Steve...

  1. #1

    Default Integer to Hex string?

    Hi,

    Are there any functions to convert an integer in to a Hex string (any
    string type)?

    I know about using the std 'hex' routine to cout to the screen, but I
    need to store a string.

    Thanks

    Steve

    Steve Guest

  2. #2

    Default Re: Integer to Hex string?

    In article <bqs6lt$ph8$btinternet.com>,
    Steve James <com> wrote:
     

    The snprintf() function ('man snprintf') has more options than you will
    ever need to do exactly that.
    Michael Guest

  3. #3

    Default Re: Integer to Hex string?

    Thanks, of all the option, sprintf will suffice for now.
    For anyone searching on this:

    char *str = new char[8];
    sprintf(str, "%lX", (unsigned long)(theNum));

    (though as Michael pointed out, there's tons of options)



    Michael Ash wrote: 
    >
    >
    > The snprintf() function ('man snprintf') has more options than you will
    > ever need to do exactly that.[/ref]

    Steve Guest

  4. #4

    Default Re: Integer to Hex string?

    In article <bqsip6$ovn$btinternet.com>,
    Steve James <com> wrote:
     

    You should really use snprintf instead of sprintf. sprintf isn't safe
    unless you can be absolutely sure of the maximum length of the string,
    because it will happily write past the end of your buffer and into
    whatever's on the other end. It's ok in this case, but it's better to
    get into the habit of using snprintf everywhere to avoid problems when
    you can't know the maximum length.
    Michael Guest

  5. #5

    Default Re: Integer to Hex string?

    Good point. A lot of code is cut/pasted from other old bits, so best
    start with the safest example.


    Michael Ash wrote: 
    >
    >
    > You should really use snprintf instead of sprintf. sprintf isn't safe
    > unless you can be absolutely sure of the maximum length of the string,
    > because it will happily write past the end of your buffer and into
    > whatever's on the other end. It's ok in this case, but it's better to
    > get into the habit of using snprintf everywhere to avoid problems when
    > you can't know the maximum length.[/ref]

    Steve Guest

  6. #6

    Default Re: Integer to Hex string?

    In article <bqs6lt$ph8$btinternet.com>,
    Steve James <com> wrote:
     
    The iostreams library allows you to use the std 'hex' routine to output
    to a memory buffer. Simply use a stringstream as output stream instead
    of std::cout:

    #include <iostream>
    #include <iomanip>
    #include <sstream>

    //(use "using namespace std" if the 'std::' prefixes annoy you)

    int main( int argc, char *argv[])
    {
    std::ostringstream os;
    os << std::hex << std::setw( 2) << 19;
    std::string hexString = os.str();
    std::cout << hexString << std::endl;
    return EXIT_SUCCESS;
    }

    Reinder
    Reinder Guest

  7. #7

    Default Re: Integer to Hex string?

    Thanks very much for that snippet. I'd been having trouble understanding
    the bridge between std:cout and memory.

    Steve

    Reinder Verlinde wrote: 
    >
    > The iostreams library allows you to use the std 'hex' routine to output
    > to a memory buffer. Simply use a stringstream as output stream instead
    > of std::cout:
    >
    > #include <iostream>
    > #include <iomanip>
    > #include <sstream>
    >
    > //(use "using namespace std" if the 'std::' prefixes annoy you)
    >
    > int main( int argc, char *argv[])
    > {
    > std::ostringstream os;
    > os << std::hex << std::setw( 2) << 19;
    > std::string hexString = os.str();
    > std::cout << hexString << std::endl;
    > return EXIT_SUCCESS;
    > }
    >
    > Reinder[/ref]

    Steve Guest

Similar Threads

  1. string to integer
    By Mark Fenbers in forum Macromedia Flash Data Integration
    Replies: 10
    Last Post: May 23rd, 08:08 AM
  2. check string rep's integer
    By sinister in forum PHP Development
    Replies: 7
    Last Post: November 24th, 09:27 AM
  3. ado xtra - integer variable in sql-string
    By janosch webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 2
    Last Post: October 14th, 10:18 PM
  4. php array problem string to integer
    By Sébastien in forum PHP Development
    Replies: 0
    Last Post: August 3rd, 06:17 AM
  5. Equating a String & Integer Variable
    By Roger Lord in forum ASP.NET General
    Replies: 2
    Last Post: July 3rd, 02:21 AM

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