首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将带符号int号转换为无符号shell脚本

问将带符号int号转换为无符号shell脚本
EN

Stack Overflow用户
提问于 2016-09-21 07:07:15
回答 2查看 4.3K关注 0票数 2

我需要使用shell脚本将有符号的int32编号转换为无符号的

例如:

代码语言:javascript
复制
convert  Input : -256
Expected output: 4294967040
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-21 07:38:27

这能帮到你;

代码语言:javascript
复制
#!/bin/bash
input=$1
if [[ $input -lt "0" ]]; then
    output=$((4294967296+$input))
else
    output=$input
fi
echo $output


#signed int32;
#–2,147,483,648 to 2,147,483,647
#unsigned int32
#0 to 4,294,967,295
票数 5
EN

Stack Overflow用户

发布于 2017-03-20 21:16:44

这可能不是最快的,但似乎起作用了

代码语言:javascript
复制
unsigned_to_signed()
{
    local hex=$(printf "0x%x" $(( $* )))
    local unsigned=$(printf "%u" $hex )
    echo $unsigned
}

unsigned_to_signed32()
{
    local hex=$(printf "0x%x" $(( $* & 0xFFFFFFFF )))
    local unsigned=$(printf "%u" $hex )
    echo $unsigned
}

unsigned_to_signed -256
18446744073709551360

unsigned_to_signed32 -256
4294967040
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39609628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档