Thread: String Conversion

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    String Conversion

    i have the char value that contain the hexadecimal input.

    char Val2[9];
    strncpy (Val2, buffer+8, 8);
    Val2[8]="\0";
    printf("\nValue 2 : %s", Val2);

    the Val2 value need to assign to :

    int ptext[0].

    how can i do that?

    thanks

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Some questions?
    • What will Val2 contain after your call to strncpy(..)?
    • Will this always be a valid hexadecimal number represented as a string? (i.e. "0x0A", "0xDEADBEEF", etc)


    Without knowing exactly what you have in mind, I think that the function int sscanf(const char *str, const char *format, ...); might solve your issues.

    a simple example:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int
    main (int argc, char **argv)
    {
      char *str = "0x4a";
      int n;
    
      sscanf (str, "%x", (unsigned int *) &n);
      printf ("n = %d\n", n);
    
      return 0;
    }
    Last edited by edoceo; 03-17-2009 at 09:30 AM. Reason: typo

  3. #3
    Banned
    Join Date
    Mar 2009
    Posts
    5
    Quote Originally Posted by 6kyAngel View Post
    i have the char value that contain the hexadecimal input.

    char Val2[9];
    strncpy (Val2, buffer+8, 8);
    Val2[8]="\0";
    printf("\nValue 2 : %s", Val2);

    the Val2 value need to assign to :

    int ptext[0].

    how can i do that?

    thanks
    can you explain can we declare val2[9] variable name like this?
    And tell me what this program do?
    Last edited by CornedBee; 03-17-2009 at 08:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM