英文:
How to convert int (int64) into uint16 in golang?
问题
我可以100%保证输入的int变量的值始终是无符号(正数)且小于int16。
如何将这个int类型的变量转换为uint16?
英文:
I can 100% guaranty the value of input int variable is always unsign(positive) and less than int16.
How can I convert this int type variable to uint16?
答案1
得分: 37
// 将类型转换并赋值给新变量或作为参数传递。
var i int
...
u := uint16(i)
foo(uint16(i))
英文:
// convert the type and assign to new variable or pass as a parameter.
var i int
...
u := uint16(i)
foo(uint16(i))
答案2
得分: 6
你需要检查该数字是否为非负数且小于等于0xFFFF,然后将其转换为无符号16位整数。
英文:
You need to check that the number is not negative and that it is <= 0xFFFF and then cast it to an unsigned 16 bit int.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论