Fortran: printing the Printing the Hex contents of a character variable

Click For Summary
SUMMARY

The discussion focuses on printing the hexadecimal contents of a character variable in Fortran, specifically using a character variable defined as character*10 name. Participants conclude that Fortran lacks a built-in shortcut for this task, unlike C. Instead, users must implement a custom function to convert the character to decimal using ichar(letter) and then create a recursive function to convert decimal to hexadecimal.

PREREQUISITES
  • Understanding of Fortran syntax and data types
  • Familiarity with the ichar intrinsic function in Fortran
  • Basic knowledge of recursive function implementation
  • Concept of number base conversion (decimal to hexadecimal)
NEXT STEPS
  • Research how to implement recursive functions in Fortran
  • Learn about number base conversion algorithms
  • Explore the use of intrinsic functions in Fortran
  • Investigate character manipulation techniques in Fortran
USEFUL FOR

This discussion is beneficial for Fortran developers, programmers working with legacy code, and anyone interested in character data manipulation and conversion in Fortran.

physicsposter
Messages
1
Reaction score
0
Given a character variable

character*10 name

write (6,111) name

111 format (?)

what do I put in the format statement to print the hexadecimal contents of name?
 
Technology news on Phys.org
Hhhmmm...I have never had such need. I don't think there is such a shortcut in Fortran as there is in C; so, I am afraid you are going to have to write your own little function to do so.

First, you will have to convert the character to decimal; you can use ichar(letter) for that. But then, you need your own little recursive function to produce hex. Here is a simple explanation of how to go about it for any decimal.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
6K
  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K