微信小程序开发小项目:进制转换器

功能实现:实现二进制、八进制、十六进制、十进制的一个转换

功能界面

在这里插入图片描述

代码实现(完整版)

wxml文件

<view class="container">
  <view class="title">进制转换器</view>
  <view class="form">
    <view class="item">
      <view>二进制:</view>
      <input type="number" bindinput="binaryInput" value="{{binary}}" />
    </view>
    <view class="item">
      <view>八进制:</view>
      <input type="number" bindinput="octalInput" value="{{octal}}" />
    </view>
    <view class="item">
      <view>十进制:</view>
      <input type="number" bindinput="decimalInput" value="{{decimal}}" />
    </view>
    <view class="item">
      <view>十六进制:</view>
      <input type="text" bindinput="hexInput" value="{{hex}}" />
    </view>
  </view>
</view>

xcss文件

.container {
    margin: 20px;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    background-color: #fff;
  }
  
  .title {
    font-size: 24px;
    font-weight: bold;
    margin-bottom: 20px;
  }
  
  .form {
    display: flex;
    flex-direction: column;
  }
  
  .item {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
  }
  
  .item view {
    font-size: 16px;
    font-weight: bold;
    margin-right: 10px;
    width: 40%;
  }
  
  .item input {
    width: 100%;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
    font-size: 16px;
  }
  

js文件

Page({
    data: {
      binary: '',
      octal: '',
      decimal: '',
      hex: ''
    },
  
    binaryInput: function (e) {
      var decimal = parseInt(e.detail.value, 2);
      this.setData({
        binary: e.detail.value,
        octal: decimal.toString(8),
        decimal: decimal.toString(),
        hex: decimal.toString(16).toUpperCase()
      });
    },
  
    octalInput: function (e) {
      var decimal = parseInt(e.detail.value, 8);
      this.setData({
        binary: decimal.toString(2),
        octal: e.detail.value,
        decimal: decimal.toString(),
        hex: decimal.toString(16).toUpperCase()
      });
    },
  
    decimalInput: function (e) {
      var decimal = parseInt(e.detail.value);
      this.setData({
        binary: decimal.toString(2),
        octal: decimal.toString(8),
        decimal: e.detail.value,
        hex: decimal.toString(16).toUpperCase()
      });
    },
  
    hexInput: function (e) {
      var decimal = parseInt(e.detail.value, 16);
      this.setData({
        binary: decimal.toString(2),
        octal: decimal.toString(8),
        decimal: decimal.toString(),
        hex: e.detail.value.toUpperCase()
      });
    }
  })
  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值