Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 585321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:59:26+00:00 2026-05-13T14:59:26+00:00

I can do this: int main(int argc, char** argv) { unsigned char cTest =

  • 0

I can do this:

int main(int argc, char** argv) {
  unsigned char cTest = 0xff;
  return 0;
}

But what’s the right way to get a hexadecimal number into the program via the command line?

unsigned char cTest = argv[1];

doesn’t do the trick. That produces a initialization makes integer from pointer without a cast warning.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:59:26+00:00Added an answer on May 13, 2026 at 2:59 pm

    As the type of main indicates, arguments from the command line are strings and will require conversion to different representations.

    Converting a single hexadecimal command-line argument to decimal looks like

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      printf("%ld\n", strtol(argv[1], NULL, 16));
    
      return 0;
    }
    

    Example usage:

    $ ./hex ff
    255
    

    Using strtol and changing the final argument from 16 to 0 as in

    printf("%ld\n", strtol(argv[1], NULL, 0));
    

    makes the program accept decimal, hexadecimal (indicated by leading 0x, and octal (indicated by leading 0) values:

    $ ./num 0x70
    112
    $ ./num 070
    56
    $ ./num 70
    70
    

    Using the bash command shell, take advantage of ANSI-C Quoting to have the shell perform the conversion, and then your program just prints the values from the command line.

    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
      int i;
      for (i = 1; i < argc; i++) {
        unsigned char value = argv[i][0];
        if (strlen(argv[i]) > 1)
          fprintf(stderr, "%s: '%s' is longer than one byte\n", argv[0], argv[i]);
    
        printf(i + 1 < argc ? "%u " : "%u\n", value);
      }
    
      return 0;
    }
    

    Bash supports many formats of the form $'...', but $'\xHH' appears to be the closest match to your question. For example:

    $ ./print-byte $'\xFF' $'\x20' $'\x32'
    255 32 50
    

    Maybe you pack the values from the command line into a string and print it.

    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
      int i;
    
      if (argc > 1) {
        char *s = malloc(argc);
        if (!s) {
          fprintf(stderr, "%s: malloc: %s\n", argv[0], strerror(errno));
          return 1;
        }
    
        for (i = 1; i < argc; i++)
          s[i - 1] = strtol(argv[i], NULL, 16) & 0xff;
    
        s[argc - 1] = '\0';
        printf("%s\n", s);
        free(s);
      }
    
      return 0;
    }
    

    In action:

    $ ./pack-string 48 65 6c 6c 6f 21
    Hello!
    

    All of the above is reinventing wheels that bash and the operating system already provide for you.

    $ echo $'\x48\x65\x6c\x6c\x6f\x21'
    Hello!
    

    The echo program prints its command-line arguments on the standard output, which you can think of as a for loop over the arguments and a printf for each.

    If you have another program that performs the decoding for you, use Command Substitution that replaces a command surrounded by backticks or $() with its output. See examples below, which again use echo as a placeholder.

    $ echo $(perl -e 'print "\x50\x65\x72\x6c"')
    Perl
    $ echo `python -c 'print "\x50\x79\x74\x68\x6f\x6e"'`
    Python
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 297k
  • Answers 297k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As Bill remarked, the operator shouldn't be const. I believe… May 13, 2026 at 7:14 pm
  • Editorial Team
    Editorial Team added an answer I was looking at these guys a couple months ago:… May 13, 2026 at 7:14 pm
  • Editorial Team
    Editorial Team added an answer If you're set on not allowing your loop to continue… May 13, 2026 at 7:14 pm

Related Questions

I have an abstract class defining a pure virtual method in c++: class Base
Here is what I am trying to do: 1) Open an ofstream object in
In my programming class we currently have a project that requires us to take
I am in the following situation: I am writing code for a kernel that
I'm trying to tag a picture file with a date in Cocoa and are

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.