Thread: sprintf issue

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    sprintf issue

    I'm trying to write a basic function that converts a 6 byte array to a formatted mac address string:

    i.e. 0x12 0x13 0x14 0x15 0x16 0x17

    would be formatted to:

    12:13:14:15:16:17

    The code I wrote was as follows:

    Code:
    char buffer[100];
    char mac[] = { 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
    
    sprintf(buffer, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
    It appears to partially work. Here is the contents of buffer in memory after its executed:

    Code:
    buf = 0x003ffd58 "12:13:14:15:16:17"
    This is correct! Ok. But if I try mac as a different array:

    Code:
    mac = { 0x35, 0xaa, 0x11, 0xed, 0x71, 0xac };
    I get:

    Code:
    buf = 0x0028fd54 "35:ffffffaa:11:ffffffed:71:ffffffac"
    How do I get rid of all those f's?

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    RE: sprintf issue

    it looks as though your "characters" are being sign-extended.

    Notice how you only get an issue when trying with numbers 8 or higher.

    the answer is to make your array unsigned chars.

    Hope this helps!

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Have you tried it without the ".2" modifiers?

    Also you should define your mac array as unsigned char mac[6]; to prevent values greater than 0x7f from being represented as negative numbers.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bsattin1 View Post
    it looks as though your "characters" are being sign-extended.

    Notice how you only get an issue when trying with numbers 8 or higher.

    the answer is to make your array unsigned chars.

    Hope this helps!
    LOL ... I see you beat me to it.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    just started answering questions today...I gotta fight for my posts

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bsattin1 View Post
    just started answering questions today...I gotta fight for my posts
    Oh gosh, you mean it's a contest?
    Looks like I missed yet another memo...

    LOL!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  3. sprintf Wrapping, a tough one
    By AdmiralKirk in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2006, 10:43 AM
  4. Problem: 64k block issue
    By aaronc in forum C Programming
    Replies: 1
    Last Post: 06-16-2004, 03:22 AM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM