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

In summary, the conversation is about how to print the hexadecimal contents of a character variable in Fortran. The solution suggested is to create a recursive function to convert the character to decimal and then produce the hexadecimal representation.
  • #1
physicsposter
1
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
  • #2
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.
 

1. What is Fortran?

Fortran is a high-level programming language used for scientific and engineering applications. It was developed in the 1950s and is still widely used today.

2. How do I print the Hex contents of a character variable in Fortran?

To print the Hex contents of a character variable in Fortran, you can use the ICHAR function. This function converts a character to its ASCII code, which can then be printed in Hex format using the Z edit descriptor.

3. Can I print the Hex contents of a character variable in Fortran without using the ICHAR function?

Yes, you can also use the ACHAR function to convert a character to its ASCII code, and then use the Z edit descriptor to print the Hex format. However, ICHAR is the recommended method for printing Hex contents of a character variable in Fortran.

4. What is a character variable in Fortran?

A character variable in Fortran is a variable that can hold a single character or a string of characters. It is declared using the CHARACTER keyword and can be used to store and manipulate text data.

5. Can I print the Hex contents of a non-character variable in Fortran?

Yes, you can print the Hex contents of a non-character variable in Fortran using the Z edit descriptor. However, the result may not be meaningful as the data is not interpreted as characters. It is best to use ICHAR or ACHAR for printing Hex contents of a character variable.

Similar threads

  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
10
Views
997
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
26
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
Back
Top