turn int into 6 character array, not itoa

This is a discussion on turn int into 6 character array, not itoa within the C++ Programming forums, part of the General Programming Boards category; Does anyone know of a function that can turn an int into a five character array plus a null terminator. ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    turn int into 6 character array, not itoa

    Does anyone know of a function that can turn an int into a five character array plus a null terminator.

    e.g. If the int where 12345 it would turn it into:
    Code:
    byte[0] = 0x49;
    byte[1] = 0x50;
    byte[2] = 0x51;
    byte[3] = 0x52;
    byte[4] = 0x53;
    byte[5] = 0x00;
    or the string "12345" pluss a null terminator.

    e.g. If the int where 0 it would turn it into:
    Code:
    byte[0] = 0x48;
    byte[1] = 0x48;
    byte[2] = 0x48;
    byte[3] = 0x48;
    byte[4] = 0x48;
    byte[5] = 0x00;
    or the string "00000" pluss a null terminator.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    How about
    sprintf( mybuff, "%05d", myInt );

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    thank you.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Unsafe! If myInt is greater than 99999, or less than -9999, you'll get a buffer overflow. The width specification of sprintf() is a minimum, not a maximum.

    You can either range-check before the call, or make the buffer large enough to accomodate larger ints (10 digits + 1 sign + 1 NUL on 32-bit int platforms).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    well the largest integer i'll ever be working with is 65535 which is less than 99999, and the smallest integer i'll ever be working with is 0 which is larger than -9999. So I don't really see what the problem is.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If you can guarantee that the integers won't even be outside that range, it's fine. (Although I still think it's a bug waiting for the next program modification to strike.)
    If it's just an assumption on your part, i.e. user input touches this in any way, then it's dangerous.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    If you use an unsigned short then it will never be outside the range of 0-65535.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > If you use an unsigned short then it will never be outside the range of 0-65535.
    The standard doesn't limit short to 16 bits, only that it should be at least 16 bits.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  2. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM

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