Gives a hexadecimal number of 0xFF 0x80 (only 2 "bit") to convert it to a signed byte of decimal integer

Source: Internet
Author: User

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace Std;
int main ()
{
Char Ch[8];
Char ch2[8]={' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 '};

Ltoa ("0xba", "strtol", +), ch,2);
Cout<<strlen (CH);
for (int i=8-strlen (CH), j=0;i<8;i++,j++)
{

CH2[I]=CH[J];
}

if (ch2[0]== ' 1 ')
{
cout<< "Minus:" <<endl;
cout<< "-" << (256-strtol ("0xba") <<endl;
}
else{
cout<< "+:" <<endl;
Cout<<strtol ("0xba",' + ') <<endl;
}
return 0;
}

^ (* ̄ (oo)  ̄) ^: Red number portion for const char* type;

The main application functions are: Strtol;ltoa

The first function is to convert the string in the first argument to decimal, to the end of the conversion, to the trailing number 2-36, which refers to the numeric binary represented by the preceding string, rather than converting it to a binary.

Strtol is the conversion of the number of the first parameter into the binary of the third parameter and the array of the second argument. Char *ltoa (long Value,char *string,int radix)

The original code, anti-code, complement, operation is not introduced, here just take the opportunistic way ....

The reason why CH2 is used is because LTOA only assigns the binary from the beginning of the effective bit to CH from zero. If there is 0 in front, it is not assigned.

For example: Long (strtol) converted binary:10101--"Ltoa only gave 10101 to ch[0]=1,ch[1]=0,ch[2]=1,ch[3]=0,ch[4]=1 ....... ch[5]=null. etc...

Gives a hexadecimal number of 0xFF 0x80 (only 2 "bit") to convert it to a signed byte of decimal integer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.