工作笔记,英文烂没办法,如果不幸被你看到,请勿喷。。。

Summary:

  1. To unsigned type: just do And operator, or 2st method.

Eg. Dim a As Byte = CByte(300 And &HFF)

  1. To signed type: left shift n bits, then right shift n bits, which is to expand the signed bit. n = (sizeof(type1) - sizeof(type2)) * 8   or VB:use Len(New type) instead of sizeof(type)

Eg. Dim a As Short = CShort(34042 << 16 >> 16)

You can ignore the following information in case you don't want to know the detail.

int a = 34042;

short a1 = a;

You would got an error as follow:

Language compilers do not perform this conversion automatically because it can involve data loss. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CShort in Visual Basic) is used. Otherwise, they display a compiler error.

In C#, we can use a casting operator:

int a = 34042;

short a1 = (short)a;

In VB, we should use a conversion function:

Dim a As Integer = 100

Dim a1 As Short = CShort(a)

But issues occurred when the value is beyond the rang of short type in VB.

' Short: -32768 ~ 32767( 0FFFF8000H ~ 7FFFH)

Dim a As Integer = 34042

Dim a1 As Short = CShort(a)

In the 1st C# code, it works fine. But according to the value, how can we get -31494 from 34042? And why the VB code would catch an overflow exception(although it actually does)?

In fact, (short)a would generate IL:conv.i2  in C#. But CShort(a) would generate IL:conv.ovf.i2 in VB, and which might throw an overflow exception.

So, what we want to do is just to find out the rules of conversion.

conv.i2=Converts the value on top of the evaluation stack to int16, then extends (pads) it to int32.

Ok, let's follow it.

' Short: -32768 ~ 32767( FFFF8000H ~ 7FFFH)

Dim a As Integer = 34042

'Dim a1 As Short = CShort(a)

Dim a2 As Short = CShort(a And &HFFFF)

But unfortunately failed again, and got the same exception.

Actually, a And &HFFFF is still an integer but grater than Short.MaxValue, so it failed again.

Short is a 16-bit signed integer type so that it only contains 15 data-bit and 1 signed-bit.

Short.MaxValue and Short.MinValue:

00000000 00000000 01111111 11111111

11111111 11111111 10000000 00000000

So, after you do the "And" operator, you must expand another 16 bits according to the signed bit.

My solution is that: Left shift 16 bits, and then right shift 16 bits.

' Short: -32768 ~ 32767( FFFF8000H ~ 7FFFH)

Dim a As Integer = 34042

'Dim a1 As Short = CShort(a)

'Dim a2 As Short = CShort(a And &HFFFF)

Dim a3 As Short = CShort((a And &HFFFF) << 16 >> 16)

Let's see what happened.

a

84FAH

a And &HFFFF

84FAH

(a And &HFFFF) << 16

84FA 0000H

(a And &HFFFF) << 16 >> 16

FFFF 84FAH

Actually you can skip the And operator step.

Dim a3 As Short = CShort(a << 16 >> 16)

You can get these hex value by : Console.WriteLine("{0:X}", a) ...

Now it's all clear!

You can expand this method to other similar conversion, such as long to int( can use Fix()), long to short, etc.

转载于:https://www.cnblogs.com/liujq007/archive/2010/12/04/1896059.html

完美解决VB中int to short溢出问题相关推荐

  1. winform界面嵌入dwg图纸_完美解决窗体中预览DWG图形(C#版)

    看到完美解决VB.NET窗体中预览DWG图形帖子后,用C#代码 实现如下: class ViewDWG { struct BITMAPFILEHEADER { public short bfType; ...

  2. htmltd文本自动换行,完美解决table中td里面的内容自动换行

    完美解决table中td里面的内容自动换行2018-08-03 11:50 对于将td里面的内容自动换行,在很久以前就遇到的了,但是一直没有完美的解决. 今天要打印一个报表,有一列中的内容太长,将ta ...

  3. Java中int和short的取值范围_java中short、int、long、float、double取值范围

    1. 基本类型:int 二进制位数:32 包装类:java.lang.Integer 最小值:Integer.MIN_VALUE= -2147483648 (-2的31次方) 最大值:Integer. ...

  4. java中int与short哪个速度更快?

    网上查了查,总结一个最优答案,以后如果有变动,咱们再看: 建议使用 int 类型,在 HotSpot JVM 中 int 是经过特别优化的,从性能和效率上是最优的选择. 传输存储用short还行,计算 ...

  5. vb excel编程实例_用过程和函数来解决VB中的计算问题

    学习不延期,宅在家也能高效掌握VB编程! VB干货 ●用VB实现字母滚动效果! ●VB从入门到精通视频教程! ●用VB获取网站登陆的用户名和密码! ●VB编程语言基础知识点总结 VB过程简介 Ø在VB ...

  6. 完美解决Visio中MathType公式变形的问题以及visio图转pdf之后公式不显示的问题

    很多博主说的转换为打印是可以解决公式变形的问题,但是放大后会有些地方模糊.然后我发现啊!!! 插入公式前先插入矩形框,把公式和矩形框组合起来,就可以了,完美解决,不会糊,也不会变形!!!Nice

  7. Java中int和short的取值范围_我的处理器上C中的int,short和其他数据类型的范围?...

    从this开始,看一下脚注: 实际值取决于特定系统和库实现,但应反映目标平台中这些类型的限制 . 如果您在系统上运行以下代码,它应该提供一些见解,因为返回的值可能与上述链接中的值不同或不同 . #in ...

  8. 完美解决SSM中 java.lang...c3p0/impl/NewProxyResultSet.isClosed()Z is abstract 【插入数据只能插入一条问题】

    报错信息如下 解决办法 在pom.xml文件中修改我们引入的c3p0依赖的jar包: 之前是: <!--数据库连接池 驱动--><!-- https://mvnrepository. ...

  9. int函数在Oracle,vb中int是什么意思 ?

    VB语言中int函数的意思是取整数. 即:int(x)函数是取不大于x的最大整数. 例如: 1.int(4.88)=4 int(4.88)即是取一个不大于4.88且最接近4.88的整数,所以int(4 ...

  10. 完美解决IE6中fixed抖动问题的方法

    我们可以通过position:fixed来实现元素的固定效果,如网页中底部的"回到顶部菜单",底部的toolbar,对联广告等等,可惜fixed属性在IE6及以下是不支持的.通常的 ...

最新文章

  1. Java B2B2C多用户商城 springcloud架构-服务容错保护(Hystrix服务降级)
  2. 电脑重启后python导入的库不见_为什么python不会在启动时自动导入每个模块?
  3. linux内核源码剖析 博客,【Linux内存源码分析】页面迁移
  4. 已知一个掺杂了多个数字字符的中文名拼音,去掉所有数字字符之后,形式为“名”+空格+“姓”;并且名和姓的首字母大写,其他小写,要求输出姓名全拼,且全为小写。(后附详细样例说明)
  5. 学生成绩统计c语言课程设计,学生成绩管理系统-C语言课程设计
  6. java编写一个程序_计算已知长和宽的长方形的周长,请教一下大佬们,我们java留了一个作业,编写程序,定义一个接口Comput,声明计算周长和面积的方法...
  7. linux signal 处理
  8. 超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大[转]...
  9. 设置环境变量ANDROID_SDK_HOME有什么用?
  10. python爬虫:bs4搜索文档树
  11. 【Cadence】制作AD630焊盘SOIC_20
  12. MATLAB2010激活后无法打开MATLAB的解决方法
  13. CocosCreator如何制作微信小游戏
  14. 神经网络训练双螺旋曲线模型
  15. toMap函数 java.lang.IllegalStateException: Duplicate key 123 (attempted merging values 123 and 124)
  16. 使用curl请求接口
  17. Seeker的奇妙求职历险(华为笔试)
  18. 那么如何高效管理Linkedin账号
  19. uniapp实现点击播放mp3音频文件
  20. 【GCC】2: RTCP cc-feeback 抓包对比协议

热门文章

  1. (二)GitHub使用随记
  2. WinForm 的定时器使用
  3. 配置项setOption -- title
  4. lib的编写与使用(C/C++)
  5. 不当IT民工——技术带来质的飞跃
  6. Windows2003 WINS 服务
  7. vue 组件传值$attrs $listeners $bus provide/inject $parent/$children
  8. 使用REST framework
  9. 关于local storage及session storage 应用问题
  10. 简易效率的抽奖算法(转)