将整数值转换为十六进制字符串出错 [英] Error converting integer values to hex strings

查看:81
本文介绍了将整数值转换为十六进制字符串出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextBox,其十进制值输入为3039。

我想将该文本转换为十六进制,六个字符(3个字节)转换为第二个TextBox或Label。 br />


以下代码有效:

I have one TextBox with a decimal value typed in of "3039".
I want to convert that text to hexadecimal, with six characters (3 Bytes) into a second TextBox or Label.

The below code works:

Dim iValue as Integer = Val(TextBox1.Text)
TextBox2.Text = iValue.ToString("X6")



但是我尝试将两行合并,下面不起作用:


BUT when I attempt to combine the two lines, the below doesn't work:

TextBox2.Text = Val(TextBox1.Text).ToString("X6")



我缺少什么?

用于Windows桌面的Visual Studio Express 2013中的VB.Net



我尝试过:




What am I missing?
VB.Net in Visual Studio Express 2013 for Windows Desktop

What I have tried:

Dim iValue as Integer = Val(TextBox1.Text)
TextBox2.Text = iValue.ToString("X6")




TextBox2.Text = Val(TextBox1.Text).ToString("X6")

推荐答案

你说你得到的错误是......?



它更好将这两行留作单独的代码行以进行调试。根本不需要将它们组合在一起。你没有通过这样做获得任何东西。



你也不允许对TextBox中的文本进行任何验证,以确保它甚至可以得到一个它的有效价值。 Val将停止解析它看到的第一个非数字字符串。



此外,Val()已弃用。它仅用于向后兼容转换后的VB6代码。改为使用 Integer.TryParse()
And the error you say you're getting is ...... ?

It's better to leave those two lines as separate lines of code for debugging purposes. There is no need to combine them at all. You're not gaining anything by doing so.

You're also not allowing for any validation of the text in the TextBox to make sure it's even possible to get a valid value from it. Val will stop parsing a string on the first non-numeric character it sees.

Also, Val() is deprecated. It only exists for backwards compatibility with converted VB6 code. Use Integer.TryParse() instead.


这篇关于将整数值转换为十六进制字符串出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆