DEV Community


Converting names in C to Java

John Mercier
A software developer. I'm interested in learning new technologies and core language features. I love to dive into legacy code writing tests and refactoring as I go.
・2 min read

The x11 protocol is described in several xml files in the xcb project. These files define structures used in the C client. Each struct contains types and variable names. The variable names have two problems in java.

  • do not follow the lower camel case convention
  • start with invalid characters (numbers)

When converting these names to java I wanted to find an existing solution that could do this quickly. One benefit of java is that so many problems have already been solved by one library or another. I came across two projects that helped me solve these issues.

implementation group: '', name: 'guava', version: '29.0-jre'
implementation ''

Guava contains the class CaseFormat which is able to convert between java and c style naming conventions. To convert from c to java:

String converted =, x11Name)

This works for the majority of names but some of the enum items in the protocol start with a number.

    <enum name="PropertyFormat">
        <item name="8Bits">  <value>8</value> </item>
        <item name="16Bits"> <value>16</value> </item>
        <item name="32Bits"> <value>32</value> </item>

To convert these names tradukisto can be used to first convert the number to words. Then the rest of the name can be appended.

        String startNumbers = x11Name.find('^\\d+')
        if(startNumbers) {
            String remainingString = x11Name.substring(startNumbers.length())
            String numberWords = ValueConverters.ENGLISH_INTEGER.asWords(startNumbers.toInteger())
                .replace('-', ' ')
                .split(' ').collect{ it.capitalize() }.join('')
            return numberWords + remainingString

With Guava and tradukisto I am able to easily convert c variable names to java variable names.

Discussion (0)