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 7578127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:21:30+00:00 2026-05-30T17:21:30+00:00

How would I create a C macro to get the integer value of a

  • 0

How would I create a C macro to get the integer value of a string? The specific use-case is following on from a question here. I want to change code like this:

enum insn {
    sysenter = (uint64_t)'r' << 56 | (uint64_t)'e' << 48 |
               (uint64_t)'t' << 40 | (uint64_t)'n' << 32 |
               (uint64_t)'e' << 24 | (uint64_t)'s' << 16 |
               (uint64_t)'y' << 8  | (uint64_t)'s',
    mov = (uint64_t)'v' << 16 | (uint64_t)'o' << 8 |
          (uint64_t)'m'
};

To this:

enum insn {
    sysenter = INSN_TO_ENUM("sysenter"),
    mov      = INSN_TO_ENUM("mov")
};

Where INSN_TO_ENUM expands to the same code. The performance would be the same, but the readability would be boosted by a lot.

I’m suspecting that in this form it might not be possible because of a the C preprocessor’s inability for string processing, so this would also be an unpreferred but acceptable solution (variable argument macro):

enum insn {
    sysenter = INSN_TO_ENUM('s','y','s','e','n','t','e','r'),
    mov      = INSN_TO_ENUM('m','o','v')
};
  • 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-30T17:21:31+00:00Added an answer on May 30, 2026 at 5:21 pm

    Here’s a compile-time, pure C solution, which you indicated as acceptable. You may need to extend it for longer mnemonics. I’ll keep on thinking about the desired one (i.e. INSN_TO_ENUM("sysenter")). Interesting question 🙂

    #include <stdio.h>
    
    #define head(h, t...) h
    #define tail(h, t...) t
    
    #define A(n, c...) (((long long) (head(c))) << (n)) | B(n + 8, tail(c))
    #define B(n, c...) (((long long) (head(c))) << (n)) | C(n + 8, tail(c))
    #define C(n, c...) (((long long) (head(c))) << (n)) | D(n + 8, tail(c))
    #define D(n, c...) (((long long) (head(c))) << (n)) | E(n + 8, tail(c))
    #define E(n, c...) (((long long) (head(c))) << (n)) | F(n + 8, tail(c))
    #define F(n, c...) (((long long) (head(c))) << (n)) | G(n + 8, tail(c))
    #define G(n, c...) (((long long) (head(c))) << (n)) | H(n + 8, tail(c))
    #define H(n, c...) (((long long) (head(c))) << (n)) /* extend here */
    
    #define INSN_TO_ENUM(c...) A(0, c, 0, 0, 0, 0, 0, 0, 0)
    
    enum insn {
        sysenter = INSN_TO_ENUM('s','y','s','e','n','t','e','r'),
        mov      = INSN_TO_ENUM('m','o','v')
    };
    
    int main()
    {
        printf("sysenter = %llx\nmov = %x\n", sysenter, mov);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So following on from Clojure macro to create a synonym for a function ,
How does the code looks that would create an object of class: string myClass
How would I create the equivalent Linq To Objects query? SELECT MIN(CASE WHEN p.type
can someone please get me started on the following: i would like to package
I would like to create a macro as such: <macrodef name=testing> <element name=test implicit=yes/>
I am having a problem with a lisp macro. I would like to create
I would like to create a C pre-processor macro that will single-quote the argument.
How could I create a macro that would check each cell of column A,
I normally would create a limited rights user and run the process under that
I'd like to know how one would create an application that starts in the

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.