Skip navigation
This discussion is archived

convert number to hex

2590 Views 7 Replies Latest reply: Mar 18, 2009 1:25 PM by iamravenous RSS
fireman888 Calculating status...
Currently Being Moderated
Oct 30, 2008 4:58 PM
Hi, how can I conver number to a hex string?

For exmple convert 255 to "FF"?

Thank you very much.

fm
Mac mini, Mac OS X (10.5.4)
  • robhedin Calculating status...
    Currently Being Moderated
    Oct 30, 2008 5:25 PM (in response to fireman888)
    I believe something like the following should work...


    int intValue = 255;
    NSString *hexValue = [NSString stringWithFormat:@"%02x", intValue];


    If I remember my formatting codes correctly, that is... (note this assumes that you're dealing with a value from 0 - 255 as it will zero pad to two spaces)

    rob.
    Macbook Pro 2.33, Mac OS X (10.5.2)
  • RickMaddy Calculating status...
    Currently Being Moderated
    Oct 30, 2008 6:44 PM (in response to robhedin)
    That will give 'ff'. If you want the hex in uppercase use %02X instead of %02x.
    15" MacBook Pro Intel Core Duo 2.0GHz, 2GB RAM, Mac OS X (10.5.5), iPod touch 16GB, iPhone 16GB
  • eknathkadam Calculating status...
    Currently Being Moderated
    Jan 26, 2009 5:49 PM (in response to RickMaddy)
    how can i do opposite.

    I have hex and want to convert to number.
    Macbook pro 2.33 gz, Mac OS X (10.5.5)
  • orangekay Calculating status...
    Currently Being Moderated
    Jan 26, 2009 9:30 PM (in response to eknathkadam)
    sscanf
    Several, Mac OS X (10.5.2)
  • Fish in a barrel Calculating status...
    Currently Being Moderated
    Jan 27, 2009 4:52 AM (in response to fireman888)
    This problem is in a class of problems called NPC (http://en.wikipedia.org/wiki/NP-complete), but don't worry yourself about this. Your problem can still be solved with the following BSD code:

    /*
    This file is part of libaltivec. sscanf implementation.
    Copyright (C) 2003. Red Hat, Inc.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    */

    #define GNUSOURCE

    #include <stdio.h>
    #include <string.h>
    #include <stdarg.h>

    #undef sscanf

    int
    sscanf (const char *string, const char *format, ...)
    {
    va_list ap;
    int result;

    va_start (ap, format);
    result = vsscanf (string, format, ap);
    va_end (ap);

    return result;
    }
    Toaster, Other OS, I'm working on my latest screenplay.
  • Brian Postow Calculating status...
    Currently Being Moderated
    Jan 27, 2009 7:16 AM (in response to Fish in a barrel)
    WHAT? converting binary to hex isn't NPC. It's linear in the number of bits.
    Mac Mini, Mac OS X (10.5.3)
  • iamravenous Calculating status...
    Currently Being Moderated
    Mar 18, 2009 1:25 PM (in response to fireman888)
    fireman888 wrote:
    Hi, how can I conver number to a hex string?

    For exmple convert 255 to "FF"?

    Thank you very much.

    fm


    Programmatically:

    NSString *c = @"FF";
    unsigned int h;

    [[[NSScanner scannerWithString:c] scanHexInt:&h]];
    Mac OS X (10.5.6)

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.