Skip to content

Format Integer type as hexadecimal value with output converter #521

Answered by HT154
fleckerbr asked this question in Q&A
Discussion options

You must be logged in to vote

Assuming that you don't want to affect all UInt instances (only Register.address), you can use an output converter like this:

output {
	renderer = new YamlRenderer {
		converters {
			[Register] = (reg) -> reg.toMap().mapValues((k, v) -> if (k == "address") new RenderDirective {
				text = " 0x\(v.toRadixString(16).padStart(2, "0"))"
			} else v)
		}
	}
}

This says:

  • When rendering the Register class,
  • Convert it to a Map
  • For each k/v pair,
  • If the key is address, replace it with a RenderDirective that has text 0x<zero-padded hex representation of the value>
  • Otherwise do not change the value

Based on your example (with minor edits so it evaluates successfully), this results in:

EXAMPLE_RE…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@fleckerbr
Comment options

@holzensp
Comment options

Answer selected by fleckerbr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants