#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
/////////////////////////////////////////////////////
//
//功能:二进制取反
//
//输入:const unsigned char *src 二进制数据
// int length 待转换的二进制数据长度
//
//输出:unsigned char *dst 取反后的二进制数据
//
//返回:0 success
//
//////////////////////////////////////////////////////
int convert(unsigned char *dst, const unsigned char *src, int length)
{
int i;
for (i = 0; i < length; i++)
{
dst[i] = src[i] ^ 0xFF;
}
return 0;
}
//////////////////////////////////////////////////////////
//
//功能:十六进制转为十进制
//
//输入:const unsigned char *hex 待转换的十六进制数据
// int length 十六进制数据长度
//
//输出:
//
//返回:int rslt 转换后的十进制数据
//
//思路:十六进制每个字符位所表示的十进制数的范围是0 ~255,进制为256
// 左移8位(<<8)等价乘以256
//
/////////////////////////////////////////////////////////
unsigned long HextoDec(const unsigned char *hex, int length)
{
int i;
unsigned long rslt = 0;
for (i = 0; i < length; i++)
{
rslt += (unsigned long)(hex[i]) << (8 * (length - 1 - i));
}
return rslt;
}
/////////////////////////////////////////////////////////
//
//功能:十进制转十六进制
//
//输入:int dec 待转换的十进制数据
// int length 转换后的十六进制数据长度
//
//输出:unsigned char *hex 转换后的十六进制数据
//
//返回:0 success
//
//思路:原理同十六进制转十进制
//////////////////////////////////////////////////////////
int DectoHex(int dec, unsigned char *hex, int length)
{
int i;
for (i = length - 1; i >= 0; i--)
{
hex[i] = (dec % 256) & 0xFF;
dec /= 256;
}
return 0;
}
/////////////////////////////////////////////////////////
//
//功能:求权
//
//输入:int base 进制基数
// int times 权级数
//
//输出:
//
//返回:unsigned long 当前数据位的权
//
//////////////////////////////////////////////////////////
unsigned long power(int base, int times)
{
int i;
unsigned long rslt = 1;
for (i = 0; i < times; i++)
rslt *= base;
return rslt;
}
/////////////////////////////////////////////////////////
//
//功能:BCD转10进制
//
//输入:const unsigned char *bcd 待转换的BCD码
// int length BCD码数据长度
//
//输出:
//
//返回:unsigned long 当前数据位的权
//
//思路:压缩BCD码一个字符所表示的十进制数据范围为0 ~ 99,进制为100
// 先求每个字符所表示的十进制值,然后乘以权
//////////////////////////////////////////////////////////
unsigned long BCDtoDec(const unsigned char *bcd, int length)
{
int i, tmp;
unsigned long dec = 0;
for (i = 0; i < length; i++)
{
tmp = ((bcd[i] >> 4) & 0x0F) * 10 + (bcd[i] & 0x0F);
dec += tmp * power(100, length - 1 - i);
}
return dec;
}
/////////////////////////////////////////////////////////
//
//功能:十进制转BCD码
//
//输入:int Dec 待转换的十进制数据
// int length BCD码数据长度
//
//输出:unsigned char *Bcd 转换后的BCD码
//
//返回:0 success
//
//思路:原理同BCD码转十进制
//
//////////////////////////////////////////////////////////
int DectoBCD(int Dec, unsigned char *Bcd, int length)
{
int i;
int temp;
for (i = length - 1; i >= 0; i--)
{
temp = Dec % 100;
Bcd[i] = ((temp / 10) << 4) + ((temp % 10) & 0x0F);
Dec /= 100;
}
return 0;
}
//main函数
int main(int argc, char** argv)
{
//BCD码转十进制
unsigned char BCD[3] = { 0x00, 0x53, 0x20 };
int dec_bcd = BCDtoDec(BCD, 3);
cout << "dec_bcd : " << dec_bcd << endl;
//十进制转BCD码
unsigned char tmp_bff[3] = "";
DectoBCD(dec_bcd, tmp_bff, 3);
for (int i = 0; i < 3; ++i)
{
//cout << "tmp_buff[" << i << "]: " << tmp_bff[i] << endl;
printf("tmp_bff[%d] = 0x%02X\n", i, tmp_bff[i]);
}
cout << endl << endl;
//十六进制转十进制
unsigned char Hex[3] = { 0x00, 0x53, 0x20 };
int dec_hex = HextoDec(Hex, 3);
cout << "dec_hex: " << dec_hex << endl;
//十进制转十六进制
unsigned char hex_bff[3] = "";
DectoHex(dec_hex, hex_bff, 3);
for (int i = 0; i < 3; ++i)
{
printf("hex_bff[%d] = 0x%02X\n", i, hex_bff[i]);
}
system("pause");
return 0;
}
相关推荐
NC65增加自定义参照并且参照数据要根据选择的财务组织过滤
1、首先定义一个参照类 package nc.pub.jzcm.ref; import nc.ui.bd.ref.AbstractRefModel; import nc.ui.bd.ref.IRefDocEdit; import nc.ui.bd.ref.IRefMaintenanceHandler; public class PMPrPayDefaultRefModel extends AbstractRefModel { public PMPrPayDefaultRefModel(){ ...
c语言中int转成bcd码,16进制 10进制 BCD码之间的转换
分类: C&C++ 2012-03-04 17:24 2278人阅读 评论(0) 收藏 举报#include #include /////////////////////////////////////////////////////////功能:二进制取反////输入:const unsigned char *src 二进制数据// int length...
Xilinx Chip2Chip IP核实战:Aurora PHY通信配置与仿真避坑指南
本文深入解析Xilinx Chip2Chip IP核与Aurora PHY的通信配置与仿真优化,提供实战避坑指南。通过AXI协议转换桥实现FPGA间高速数据传输,详细讲解Aurora PHY参数设置、仿真加速技巧及常见问题解决方案,帮助工程师快速部署稳定高效的芯片间通信系统。
BCD码以及各进制之间的相互转换
BCD码的学习以及进制之间的换算
wireshark抓包分析TCP协议和用Fiddler抓包Https分析
wireshark抓包分析TCP协议和用Fiddler抓包Https分析 1.实验说明: 以某种游戏的客户端连接服务器、断开服务器为例,用wireshark抓包分析TCP协议的三次握手连接、四次握手断开,与计算机网络原理进行验证;用Fiddler抓包,分析验证一个HTTPS网站的TCP连接过程 2.实验过程: wireshark实验: (1)实验准备:安装《洛克王国》游戏客户端, 运行洛克王国客户端,打开内存资源管理器查看PID 根据PID查看洛克王国服务端的IP地址和端口号 并打开命令行运行以下命令: n
ROS自定义地图实战:从CAD到导航的完整流程
本文详细介绍了如何将CAD图纸或手绘草图高效转换为ROS机器人可直接使用的导航地图。通过解析PGM与YAML标准格式,提供从图像处理、尺度校准到集成AMCL与move_base导航栈的完整实战流程,帮助开发者跳过繁琐的SLAM建图,实现快速、精准的自定义地图部署。
c++ 转bcd码_C++如何实现BCD码和ASCII码的相互转换
BCD即BCD代码。Binary-Coded Decimal,简称BCD,称BCD码或二-十进制代码,亦称二进码十进数。是一种二进制的数字编码形式,用二进制编码的十进制代码。这种编码形式利用了四个位元来储存一个十进制的数码,使二进制和十进制之间的转换得以快捷的进行。这种编码技巧,最常用于会计系统的设计里,因为会计制度经常需要对很长的数字串作准确的计算。相对于一般的浮点式记数法,采用BCD码,既可...
双目立体成像(一)双目图片生成点云
利用左右目图片生成点云
c++ 转bcd码_c++ 十进制、十六进制和BCD的相互转换
#include#include#includeusingnamespacestd;/////////////////////////////////////////////////////////功能:二进制取反////输入:constunsignedchar*src二进制数据//intlength待转换的二进制数据长度////输...
uni踩坑笔记 - 离线打包调起微信分享后从微信返回APP时黑屏
apk能成功调起微信并进行分享操作,但分享完成后点击回到应用后应用出现黑屏的情况,并在约3-5秒后自动重启。
Depth Anything 3实战:如何用DINOv2 Transformer一键生成3D高斯点云?
本文详细介绍了Depth Anything 3(DA3)模型如何利用DINOv2 Transformer架构,实现从单张或多视角图像一键生成高质量3D高斯点云。DA3通过统一的深度-射线表示和自适应注意力机制,简化了传统复杂的3D重建流程,并可直接微调用于3D高斯渲染,为图形学、CV和内容创作领域提供了高效的端到端解决方案。
c++ 转bcd码_c++ 十进制、十六进制和BCD的相互转换,与打印printf,与函数调用
转载:转载自:http://www.cppblog.com/tdweng/articles/139022.html#include #include #include using namespace std;/////////////////////////////////////////////////////////功能:二进制取反////输入:const unsigned char *src...
060、模型蒸馏:把扩散模型压进单步的魔法
《端侧文生图模型蒸馏实战:从32步到1步的优化之道》摘要:针对边缘设备部署难题,本文分享了扩散模型蒸馏的核心经验。通过重构损失函数(感知损失+对抗训练+KL散度)、优化采样轨迹截取策略和改造学生模型架构,成功将Stable Diffusion压缩至单步推理。关键发现包括:小批量训练更有效、标签平滑保障生成多样性、双模型策略平衡质量与速度。在Cortex-A53芯片上实现300ms内生成,内存占用控制在200MB以下。特别强调工业部署需权衡压缩率与稳定性,建议保留迭代模式开关以适配不同场景需求。
c 十进制、十六进制和BCD的相互转换
[cpp] view plain copy #include #include #include using namespace std; ///////////////////////////////////////////////////// // //功能:二进制取反 // //输入:const u
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
WINCC7.3破解文件
WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!
c++ 转bcd码_BCD码、十六进制与十进制互转
在做嵌入式软件的设计中,经常会遇到十六进制、BCD码与十进制之间的转换,最近做M1卡的应用中,涉及了大量的十六进制、BCD码与十进制之间的转换。笔者通过对BCD码、十六进制 权的理解,轻松的实现了他们之间的互换。#include #include /////////////////////////////////////////////////////////功能:二进制取反////输入:cons...
7-4 BCD解密
BCD数是用一个字节来表达两位十进制的数,每四个比特表示一位。所以如果一个BCD数的十六进制是0x12,它表达的就是十进制的12。但是小明没学过BCD,把所有的BCD数都当作二进制数转换成十进制输出了。于是BCD的0x12被输出成了十进制的18了!输入在一行中给出一个[0, 153]范围内的正整数,保证能转换回有效的BCD数,也就是说这个整数转换成十六进制时不会出现A-F的数字。现在,你的程序要读入这个错误的十进制数,然后输出正确的十进制数。
计算机进制bcd啥意思,怎样理解二进制和十六进制及BCD码 | 北岛夜话
所谓“数制”,是指“数”的表示方法,即计数的方法。数制采用进位的方法,当数值增加到该位能表示的最大值时,会将位数增加一位(进位)。按照进位的规则进行计数的数制,称为进位数制。常见的进位数制有:二进制(Binary)、十进制(Decimal)和十六进制(Hexadecimal)。人类的思维习惯于十进制(Decimal),所谓“逢十进一”。这个没什么好说的,地球人都知道。本文要介绍的是PLC编程相关的...
一文攻破BCD码转换与各进制转换
一文攻破BCD码转换与各进制转换0.导语最近做的项目中时刻看到时间戳用BCD[xx]来定义,那么针对这种定义,究竟代表什么意思,如何来使用呢,本节来阐述BCD码与其他进制...
C++字节转换 (byte转int 互转)(BCD和HEX转换)
2个字节short int 高低位转化: short int y=0x7f21; y = ( (y & 0xff00) >> 8) | ( (y & 0x00ff ) << 8); printf("%#x",y); 用short型变量储存,用移位操作和或操作拼接。 例: short a = dt_data[1]; a <<= 8; a |= d...
编程题 7-4 BCD解密【PAT】
编程题 7-4 BCD解密
PTA基础编程题目集 7-4 BCD解密 (10分)
BCD数是用一个字节来表达两位十进制的数,每四个比特表示一位。所以如果一个BCD数的十六进制是0x12,它表达的就是十进制的12。但是小明没学过BCD,把所有的BCD数都当作二进制数转换成十进制输出了。于是BCD的0x12被输出成了十进制的18了! 现在,你的程序要读入这个错误的十进制数,然后输出正确的十进制数。提示:你可以把18转换回0x12,然后再转换回12。 输入格式: 输入在一行中给出一个[0, 153]范围内的正整数,保证能转换回有效的BCD数,也就是说这个整数转换成十六进制时不会出现A-F的数字
3494



被折叠的 条评论
为什么被折叠?



