Like Tree2Likes

need function to convert int to hex

This is a discussion on need function to convert int to hex within the C Programming forums, part of the General Programming Boards category; I am loading some integers and long integers into a string as hexidecimal. For example: int I 1234; str char[2]; ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    14

    need function to convert int to hex

    I am loading some integers and long integers into a string as hexidecimal. For example:

    int I 1234;
    str char[2];
    Then funct(I) returns:
    str[0]=0x04 and str[1]=0xD2

    I have written the expectable verbose routine that loops through dividing by powers of 16, generates the hex digits and stores them one by one into the string. However can someone point me to some "nice" compact way to do this?

    Thanks,
    Fritz

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I know we're not supposed to hand out code but in this case it's just way too tempting...

    Code:
    void DecToHexStr(int dec, char *str)
      { sprintf(str, "%X", dec); }
    Just make sure your string buffer has enough characters to store the full 32 bit returned value plus an extra for the null terminator... thus char hex[9];
    Last edited by CommonTater; 08-10-2011 at 02:58 PM.
    AndrewHunter likes this.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Haha....I had to 'like' that one.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Haha....I had to 'like' that one.
    LOL... some days you gets the bear, some days the bear gets you...

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    Ah-didn't know about the code rule-sorry.

    Thanks for the answer-works fine! I wouldn't have thought of using a "print" command to convert data but if the shore fits---------

    Cheers
    Fritz

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by sonnichs View Post
    Ah-didn't know about the code rule-sorry.
    No worries, Tater was just making a joke.

    Quote Originally Posted by sonnichs View Post
    Thanks for the answer-works fine! I wouldn't have thought of using a "print" command to convert data but if the shore fits---------

    Cheers
    Fritz
    If that is what you were looking for. I guess in this case, you need to wrap your head around the concept that decimal, hex, octal, ect., are all just representations of numbers. There is no such thing as a decimal type. The types of int, double, float, ect. just define the binary format for how the number is stored. The actual representation of an int as a decimal or hex, is just the graphical representation of that binary format. Hence, yes "print" commands would work quite well.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    "The actual representation of an int as a decimal or hex, is just the graphical representation of that binary format"

    I really like that way of expressing it!

    I used to write mostly in assembler and I always felt spoiled becuase you knew exactly what you stuffed into registers and "core" and it stayed that way. I don't write much code these days and I forget a lot of the conversions that C does when you perform mixed type operations. I sometimes lay awake at night wondering what is going to over flow, underflow etc because I forgot to check "signed" vs "unsigned", roundoff and so on. As Tater's motto ends: TEST----

    Cheers
    Fritz

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sonnichs View Post
    As Tater's motto ends: TEST----
    Tater still hasn't figured out that you can't just keep adding more -- to the end to keep subtracting one more! That's not how you subtract two! Plus, he forgot his ; at the end. Silly tater!


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    LOL...now we are moving on to C syntax humor. Quzah you really are an odd one
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Quzah you really are an odd one
    LOL... And you are only now figuring that out????

    You might say his number is 41 ... just short of the answer to life, love, the universe, everything.
    quzah likes this.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    LOL... And you are only now figuring that out????

    You might say his number is 41 ... just short of the answer to life, love, the universe, everything.
    And now you have learned about the prefix operator!


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by quzah View Post
    And now you have learned about the prefix operator!
    Quzah.
    Haha.....but I do have to ask, what happened to all the colorful posts?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    And now you have learned about the prefix operator!
    Quzah.
    Code:
    if (quzah)
      --credibility;
    You mean like that?

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    more like:
    Code:
    if(quzah)
         ExitWindowsEx(EWX_SHUTDOWN, SHTDN_REASON_MAJOR_APPLICATION);
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    more like:
    Code:
    if(quzah)
         ExitWindowsEx(EWX_SHUTDOWN, SHTDN_REASON_MAJOR_APPLICATION);
    From "The Jackel"... "Da good guys don't hide"...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert string to function
    By justcurious in forum C Programming
    Replies: 6
    Last Post: 03-24-2010, 10:34 AM
  2. function to convert hex to decimal
    By Moony in forum C Programming
    Replies: 5
    Last Post: 07-11-2006, 04:14 AM
  3. Function: convert to notes
    By alice in forum C Programming
    Replies: 8
    Last Post: 05-03-2004, 03:56 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. help on a function of INT > CHAR convert!
    By SublicK in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2001, 02:21 PM

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