gpt4 book ai didi

java - 如何将HEX转换成两位数格式?

转载 作者:行者123 更新时间:2023-11-29 09:37:42 25 4
gpt4 key购买 nike

我想将十进制数转换为十六进制数。例如 14E 但我想将 E 转换成两位数格式,如 0E。这是我的代码,但它给了我一个错误。

int lightOnHours = 14;
Log.d("Two Digits", String.format("%02s",Integer.toHexString(lightOnHours));

错误信息

07-11 16:57:32.422: E/AndroidRuntime(12832): java.util.FormatFlagsConversionMismatchException: %s does not support '0'
07-11 16:57:32.422: E/AndroidRuntime(12832): at java.util.Formatter$FormatToken.checkFlags(Formatter.java:1359)
07-11 16:57:32.422: E/AndroidRuntime(12832): at java.util.Formatter.transform(Formatter.java:1440)
07-11 16:57:32.422: E/AndroidRuntime(12832): at java.util.Formatter.doFormat(Formatter.java:1079)
07-11 16:57:32.422: E/AndroidRuntime(12832): at java.util.Formatter.format(Formatter.java:1040)
07-11 16:57:32.422: E/AndroidRuntime(12832): at java.util.Formatter.format(Formatter.java:1009)
07-11 16:57:32.422: E/AndroidRuntime(12832): at java.lang.String.format(String.java:1998)
07-11 16:57:32.422: E/AndroidRuntime(12832): at java.lang.String.format(String.java:1972)
07-11 16:57:32.422: E/AndroidRuntime(12832): at com.test.digits.TestFragment2$5.onClick(TestFragment2.java:158)
07-11 16:57:32.422: E/AndroidRuntime(12832): at android.view.View.performClick(View.java:3627)
07-11 16:57:32.422: E/AndroidRuntime(12832): at android.view.View$PerformClick.run(View.java:14329)
07-11 16:57:32.422: E/AndroidRuntime(12832): at android.os.Handler.handleCallback(Handler.java:605)
07-11 16:57:32.422: E/AndroidRuntime(12832): at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 16:57:32.422: E/AndroidRuntime(12832): at android.os.Looper.loop(Looper.java:137)
07-11 16:57:32.422: E/AndroidRuntime(12832): at android.app.ActivityThread.main(ActivityThread.java:4511)
07-11 16:57:32.422: E/AndroidRuntime(12832): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 16:57:32.422: E/AndroidRuntime(12832): at java.lang.reflect.Method.invoke(Method.java:511)
07-11 16:57:32.422: E/AndroidRuntime(12832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
07-11 16:57:32.422: E/AndroidRuntime(12832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
07-11 16:57:32.422: E/AndroidRuntime(12832): at dalvik.system.NativeStart.main(Native Method)

谢谢!

最佳答案

您可以使用 x 说明符,将整数格式化为十六进制,而不是使用 s 格式说明符来表示 String.format:

String.format("%02x", lightOnHours)

或者 X 如果你喜欢大写的十六进制:

String.format("%02X", lightOnHours)

关于java - 如何将HEX转换成两位数格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24706477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com