[英]Meaning of %04X in C and how to write the same in java
In the java project i am working on, some portion of the project was written previously by someone else in C and now i need to write the same in Java. 在我正在研究的java项目中,项目的某些部分之前由C中的其他人编写,现在我需要在Java中编写相同的内容。
There is a statement in C code for printing to a file: C代码中有一个用于打印到文件的语句:
fprintf(ff, "%04X ", image[y*width+x]);
Firstly i am not sure about the meaning of %04X
. 首先,我不确定%04X
的含义。 I think it means that if image[i]
has length five or more then print only leftmost four chararacters. 我认为这意味着如果image[i]
长度为5或更长,那么只打印最左边的四个字符。 To do the same in Java i thought about masking the value using and
operation 为了在Java中做同样的事情,我想到了使用and
操作掩盖值
image[i] & 0xFFFF
Can someone please tell me the correct meaning of %04X
and how to do the same in Java? 有人可以告诉我%04X
的正确含义以及如何在Java中做同样的事情? Thanks. 谢谢。
The value is formatted as a hexadecimal integer with four digits and leading zeros. 该值被格式化为十六进制整数,具有四位数和前导零。 Java uses the same format string syntax. Java使用相同的格式字符串语法。 You can find it in the javaDoc of Formatter
. 您可以在Formatter
的javaDoc中找到它。
Excerpt: 摘抄:
'x', 'X' integral The result is formatted as a hexadecimal integer
A related functions are 相关的功能是
// create a String with the formatted value.
String formatted = String.format("%04X ", image[y*width+x]);
// write a formatted value to the console (*)
System.out.printf("%04X ", image[y*width+x]);
(*) - write it to the PrintStream
System.out
which is usually the console but can be redirected to a file or something else (*) - 将它写入PrintStream
System.out
,它通常是控制台,但可以重定向到文件或其他东西
Lets break the format code "%04X"
into its separate parts: 让我们将格式代码"%04X"
分解为单独的部分:
X
means that it will print an integer, in hexadecimal, large X
for large hexadecimal letters X
表示它将打印一个十六进制的整数,大十六进制的大X
4
means the number will be printed left justified with at least four digits, print spaces if there is less than four digits 4
表示数字将打印左对齐,至少有四位数,如果少于四位,则打印空格 0
means that if there is less than four digits it will print leading zeroes. 0
意味着如果少于四位数,它将打印前导零。 The X in %04X means hexadecimal, with ABCDEF instead of abcdef, and the 04 means print at least four digits, padding with leading zeros. %04X中的X表示十六进制,ABCDEF代替abcdef,04表示打印至少四位数,填充前导零。 It will use more digits if needed. 如果需要,它将使用更多数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.