GVKun编程网logo

Java中的二进制文本(java中的二进制文本是什么)

7

此处将为大家介绍关于Java中的二进制文本的详细内容,并且为您解答有关java中的二进制文本是什么的相关问题,此外,我们还将为您介绍关于-128作为Java中的二进制文字、C++中的二进制文件操作及示

此处将为大家介绍关于Java中的二进制文本的详细内容,并且为您解答有关java中的二进制文本是什么的相关问题,此外,我们还将为您介绍关于-128作为Java中的二进制文字、C++中的二进制文件操作及示例代码、hta实现的二进制文件转换为文本、JAVA中常用的二进制位操作的有用信息。

本文目录一览:

Java中的二进制文本(java中的二进制文本是什么)

Java中的二进制文本(java中的二进制文本是什么)

我有一个带有二进制数据的字符串(1110100),我想取出文本以便可以打印它(1110100将打印“ t”)。我尝试了这一点,它类似于我用来将文本转换为二进制的东西,但是根本不起作用:

    public static String toText(String info)throws UnsupportedEncodingException{
        byte[] encoded = info.getBytes();
        String text = new String(encoded,"UTF-8");
        System.out.println("print: "+text);
        return text;
    }

任何更正或建议将不胜感激。

-128作为Java中的二进制文字

-128作为Java中的二进制文字

基于byteJava 中的类型是带符号的8位
二进制补码整数这一事实,为什么第二种声明字节的方法不起作用?

byte ok = -128;
byte notok = 0b10000000;

我的理解是1000000应该是,-128但是java表示notok上面的变量应该是an int而不是abyte

C++中的二进制文件操作及示例代码

C++中的二进制文件操作及示例代码

c++中的二进制文件操作及示例代码

C++中的二进制文件操作及示例代码

在C++中,二进制文件是以二进制格式存储的文件,可以包含任何类型的数据,包括整数、浮点数、字符、结构体等,同时也可以对这些二进制文件进行读写操作。本文将为大家介绍C++中的二进制文件操作,以及提供一些示例代码,帮助大家更好地理解和使用二进制文件操作。

  1. 打开文件

在C++中,打开一个文件可以使用fstream库中的文件流对象,在操作文件之前需要先打开它,打开方式分为输入(读取文件)和输出(写入文件)两种方式,具体如下:

  • 输入方式:
ifstream fin;   //定义一个输入文件流对象
fin.open("test.txt", std::ios::binary);  //打开文件
if (!fin.is_open()) {
    std::cerr << "The file can not be opened.
";    //打开失败处理
    exit(EXIT_FAILURE);
}
登录后复制

其中,ifstream是一个输入文件流对象,open可以接受两个参数:第一个参数是文件名,第二个参数是打开文件的模式,这里使用的是二进制模式。如果打开失败,会将错误信息输出到标准错误流中,并使用exit函数退出程序。

立即学习“C++免费学习笔记(深入)”;

  • 输出方式:
ofstream fout;  //定义一个输出文件流对象
fout.open("test.bin", std::ios::binary); //创建文件
if (!fout.is_open()) {
    std::cerr << "The file can not be created.
";   //创建失败处理
    exit(EXIT_FAILURE);
}
登录后复制

同样,ofstream是一个输出文件流对象,open函数也可以接受两个参数。此处打开方式为创建文件,如果创建失败会将错误信息输出到标准错误流中,并使用exit函数退出程序。

  1. 读取文件

我们可以使用read函数从二进制文件中读取数据,如下所示:

struct Student {
    char name[20];
    int age;
    float score;
};

Student stu;
fin.read((char*)&stu, sizeof(stu));
登录后复制

read函数可以接受两个参数:第一个参数是一个字符指针类型,指向需要读取数据的内存地址;第二个参数则是需要读取的数据字节数。这里需要注意,读取的数据必须写入到一个已经分配内存的变量中,否则会导致程序异常终止。使用“&”符号取内存地址,强制类型转换为char指针,可实现二进制文件中char数组的读取。

  1. 写入文件

我们可以使用write函数向二进制文件中写入数据,如下所示:

struct Student {
    char name[20];
    int age;
    float score;
};

Student stu = {"Alice", 18, 85.5};
fout.write((char*)&stu, sizeof(stu));
登录后复制

write函数的用法与read函数类似,同样有两个参数。第一个参数是一个字符指针类型,它指向一个需要写入数据的内存地址;第二个参数是需要写入的数据字节数。使用“&”符号取内存地址,强制类型转换为char指针,可实现二进制文件中char数组的写入。

  1. 关闭文件

在完成对文件的操作后,我们需要关闭文件,释放系统资源,以防出现文件被其他程序占用无法访问的情况,如下所示:

fin.close();
fout.close();
登录后复制

close函数可以用来关闭文件流,并清空文件缓存区。当然,如果程序出现异常退出,也会自动调用关闭文件的操作。

  1. 完整示例代码

下面是一个完整的二进制文件读写的示例代码,其中定义了一个结构体类型,将其作为数据的读取和写入对象:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

struct Student {
    char name[20];
    int age;
    float score;
};

int main() {

    // 创建一个存储二进制文件的fstream对象fout,并打开test.bin文件
    ofstream fout;
    fout.open("test.bin", std::ios::binary);
    if (!fout.is_open()) {
        cerr << "The file can not be created." << endl;
        exit(EXIT_FAILURE);
    }

    // 向test.bin文件中写入二进制数据
    Student stu1 = {"Alice", 18, 85.5};
    Student stu2 = {"Bob", 20, 90.0};
    fout.write((char*)&stu1, sizeof(stu1));
    fout.write((char*)&stu2, sizeof(stu2));
    fout.close();

    // 打开存储二进制文件的fstream对象fin,并读取test.bin文件
    ifstream fin;
    fin.open("test.bin", std::ios::binary);
    if (!fin.is_open()) {
        cerr << "The file can not be opened." << endl;
        exit(EXIT_FAILURE);
    }

    // 从test.bin文件中读取数据,并输出到屏幕
    Student stu3, stu4;
    fin.read((char*)&stu3, sizeof(stu3));
    fin.read((char*)&stu4, sizeof(stu4));
    cout << stu3.name << ", " << stu3.age << ", " << stu3.score << endl;
    cout << stu4.name << ", " << stu4.age << ", " << stu4.score << endl;
    fin.close();

    return 0;
}
登录后复制
  1. 总结

二进制文件操作在C++中是一个非常方便的方法,可以读取和写入任何类型的数据,同时也可以保证数据的精度不受损失。在实际应用中,我们可以根据需要灵活运用,提高程序效率和数据处理能力。

以上就是C++中的二进制文件操作及示例代码的详细内容,更多请关注php中文网其它相关文章!

hta实现的二进制文件转换为文本

hta实现的二进制文件转换为文本

保存为.hta运行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<title>package file v0.1</title> 
<meta http-equiv="Content-Type" content="text/html; charset=GB2312"> 
<HTA:APPLICATION     
     ID="package file v0.1"     
     APPLICATIONNAME="package file v0.1"     
     VERSION="0.1"     
     SCROLL="no"     
     INNERBORDER="no"     
     CONTEXTMENU="yes"     
     CAPTION="yes"     
     ICON="no"     
     SHOWINTASKBAR="yes"     
     SINGLEINSTANCE="yes"     
     SYSMENU="yes"     
     MAXIMIZEBUTTON ="no" 
     WINDOWSTATE="normal" 
     NAVIGABLE="yes" 
     /> 
<SCRIPT LANGUAGE="VBScript"> 
function transfert() 
     dim filename 
     filename = document.getElementById("srcFile").value 
     if len(filename)>0 then 
             dim oReq         
             ''on error resume next 
             ''//创建XMLHTTP对象 
             set oReq     = CreateObject("MSXML2.XMLHTTP") 
                 oReq.open "get","file:\\" & filename,false 
                 oReq.send     
             ff = oReq.responseBody 
             dim u,s,kk 
             u = lenb(ff) 
             redim kk(u-1) 
             for i=0 to u-1 
                 s = hex(ascb(midb(ff,i+1,1))) 
                 if len(s)<2 then 
                     s = "0" & s 
                 end if 
                 ''kk = kk & s 
                 kk(i) = s 
             next 
             make filename,join(kk,"") 
     else 
             document.getElementById("srcFile").focus 
             msgbox "请选择要压缩的文件",16,"提示" 
     end if 
end function 
function make(filename,data) 
     dim htm,file 
     file = mid(filename,instrrev(filename,"\")+1) 
     htm = htm & "<html>"                             & vbcrlf 
     htm = htm & "<head>"                             & vbcrlf 
     htm = htm & "<title>selfdec</title>"     & vbcrlf 
     htm = htm & "<meta http-equiv=""Content-Type"" content=""text/html; charset=GB2312"">" & vbcrlf 
     htm = htm & "<HTA:APPLICATION "                 & vbcrlf 
     htm = htm & "     ID=""selfdec"" "             & vbcrlf 
     htm = htm & "     APPLICATIONNAME=""self"" " & vbcrlf 
     htm = htm & "     VERSION=""0.1"" "             & vbcrlf 
     htm = htm & "     SCROLL=""no"" "                 & vbcrlf 
     htm = htm & "     INNERBORDER=""no"" "     & vbcrlf 
     htm = htm & "     CONTEXTMENU=""no"" "     & vbcrlf 
     htm = htm & "     CAPTION=""no"" "             & vbcrlf 
     htm = htm & "     ICON=""no"" "                 & vbcrlf 
     htm = htm & "     SHOWINTASKBAR=""no"" "     & vbcrlf 
     htm = htm & "     SINGLEINSTANCE=""yes"" "& vbcrlf 
     htm = htm & "     SYSMENU=""no"" "             & vbcrlf 
     htm = htm & "     MAXIMIZEBUTTON =""no""" & vbcrlf 
     htm = htm & "     WINDOWSTATE=""normal""" & vbcrlf 
     htm = htm & "     NAVIGABLE=""yes"""             & vbcrlf 
     htm = htm & "     />"                                 & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "<SCRIPT LANGUAGE=""VBScript"">"             & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "''//保存文件"                     & vbcrlf 
     htm = htm & "function saveFile(filename,str)"             & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "     set adodbStream = CreateObject(""ADODB"" & ""."" & ""Stream"")" & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "     adodbStream.Type= 1"     & vbcrlf 
     htm = htm & "     adodbStream.Open"             & vbcrlf 
     htm = htm & "     adodbStream.write str"     & vbcrlf 
     htm = htm & "     adodbStream.SaveToFile filename,2" & vbcrlf 
     htm = htm & "     adodbStream.Close"             & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "end function"                     & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "''//VB数组转变成二进制格式" & vbcrlf 
     htm = htm & "Function MultiByteToBinary(MultiByte)" & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "     Dim RS, LMultiByte, Binary"                 & vbcrlf 
     htm = htm & "     Const adLongVarBinary = 205"             & vbcrlf 
     htm = htm & "     Set RS = CreateObject(""ADODB.Recordset"")" & vbcrlf 
     htm = htm & "     LMultiByte = LenB(MultiByte)"             & vbcrlf 
     htm = htm & "     If LMultiByte>0 Then"     & vbcrlf 
     htm = htm & "             RS.Fields.Append ""mBinary"", adLongVarBinary, LMultiByte"     & vbcrlf 
     htm = htm & "             RS.Open"                 & vbcrlf 
     htm = htm & "             RS.AddNew"                 & vbcrlf 
     htm = htm & "             RS(""mBinary"").AppendChunk MultiByte & ChrB(0)"                 & vbcrlf 
     htm = htm & "             RS.Update"                 & vbcrlf 
     htm = htm & "             Binary = RS(""mBinary"").GetChunk(LMultiByte)"                     & vbcrlf 
     htm = htm & "     End If"                             & vbcrlf 
     htm = htm & "     MultiByteToBinary = Binary"                 & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "End Function"                     & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "function DeleteMe()"             & vbcrlf 
     htm = htm & "     "                                 & vbcrlf 
     htm = htm & "     dim filename"                 & vbcrlf 
     htm = htm & "     filename     = document.location.href" & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "     filename     = mid(filename,instrrev(filename,""/"")+1)" & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "     Dim fso, MyFile"             & vbcrlf 
     htm = htm & "     Set fso             = CreateObject(""Script" & "ing.FileS" & "ystemObject"")     " & vbcrlf 
     htm = htm & "     Set MyFile     = fso.GetFile(filename)" & vbcrlf 
     htm = htm & "             MyFile.Delete"             & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "end function"                     & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "function exec()"                 & vbcrlf 
     htm = htm & "     "                                 & vbcrlf 
     htm = htm & "     ''//屏蔽错误"                 & vbcrlf 
     htm = htm & "     ''on error resume next"     & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "     ''//改变窗体大小"             & vbcrlf 
     htm = htm & "     window.resizeTo 0,0"     & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "     dim data,t,kk,filename" & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "     ''//得到数据"                 & vbcrlf 
     htm = htm & "     data             = document.getElementById(""divData"").innerText" & vbcrlf 
     htm = htm & "     ''//得到文件名"                 & vbcrlf 
     htm = htm & "     filename     = document.getElementById(""divFileName"").innerText" & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "     ''//得到数据长度"             & vbcrlf 
     htm = htm & "         u = len(data)"                 & vbcrlf 
     htm = htm & "     "                                 & vbcrlf 
     htm = htm & "     ''//获得文件数组"             & vbcrlf 
     htm = htm & "     for i=1 to u step 2"     & vbcrlf 
     htm = htm & "             t = mid(data,i,2)"     & vbcrlf 
     htm = htm & "             kk = kk & ChrB(clng(""&H"" & t))" & vbcrlf 
     htm = htm & "     next"                             & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "     ''//转变成二进制格式"     & vbcrlf 
     htm = htm & "     dataArry = MultiByteToBinary(kk)"     & vbcrlf 
     htm = htm & "     "                                 & vbcrlf 
     htm = htm & "     ''//保存文件     "                 & vbcrlf 
     htm = htm & "     saveFile filename,dataArry"                 & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "     ''//删除自己"                 & vbcrlf 
     htm = htm & "     DeleteMe"                     & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "     ''//关闭自己"                 & vbcrlf 
     htm = htm & "     window.opener = nothing"& vbcrlf 
     htm = htm & "     window.close"                 & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "end function"                     & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "<" & "/SCRIPT>"                 & vbcrlf 
     htm = htm & "<" & "/head>"                     & vbcrlf 
     htm = htm & "<body marginleft=0 marginright=0 onload=""exec()"">" & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "<div id=""divFileName""     display:none;"">" & file & "</div>" & vbcrlf 
     htm = htm & "<div id=""divData""             display:none;"">" & data & "</div>" & vbcrlf 
     htm = htm & ""                                     & vbcrlf 
     htm = htm & "</body>"                             & vbcrlf 
     htm = htm & "</html>"                             & vbcrlf 
     dim fso,f 
     dim this_file 
             this_file = file & "-pf.hta" 
     Set fso = CreateObject("Scripting.FileSystemObject") 
     Set f = fso.OpenTextFile(this_file, 2, True) 
             f.Write htm 
     msgbox "生成文件" & this_file & "成功!",64,"生成" 


end function 


</SCRIPT> 
</head> 
<body marginleft=0 marginright=0 onload="window.resizeTo 389,145 "> 
请选择文件:<input type=file id="srcFile" ><br><br> 
                 <input type=button value="     转换     " onclick="transfert">     <input type=button value="     关闭     " onclick="window.close"> 
</body> 
</html> 

JAVA中常用的二进制位操作

JAVA中常用的二进制位操作

一,计算某个正数的二进制表示法中 1 的个数

复制代码

1     //求解正数的二进制表示法中的 1 的位数
2     private static int countBit(int num){
3         int count = 0;
4         for(; num > 0; count++)
5         {
6             num &= (num - 1);
7         }
8         return count;
9     }

复制代码

算法思路:每次for循环,都将num的二进制中最右边的 1 清除。

为什么n &= (n – 1)能清除最右边的1呢?因为从二进制的角度讲,n相当于在n - 1的最低位加上1。举个例子,8(1000)= 7(0111)+ 1(0001),所以8 & 7 = (1000)&(0111)= 0(0000),清除了8最右边的1(其实就是最高位的1,因为8的二进制中只有一个1)。再比如7(0111)= 6(0110)+ 1(0001),所以7 & 6 = (0111)&(0110)= 6(0110),清除了7的二进制表示中最右边的1(也就是最低位的1)。

参考:算法-求二进制数中1的个数

 

二,获取某个数的第 i 位(判断某个数的第 i 位是0 还是 1?)

 思路:如果第 i 位 与 1 相与 结果为1 表明第 i 位为1;如果为0 表明第 i 位为0

//获取 整数 num 的第 i 位的值
    private static boolean getBit(int num, int i)
    {
        return ((num & (1 << i)) != 0);//true 表示第i位为1,否则为0
    }

1 左移 i 位后,得到一个数,这个数只有第 i 位为1,其它位都为0

num 与这个数相与,得到的结果 要么是0,要么非0。结果为 非0 表示第 i 位为1,结果为0 表示第 i 位为0

 

 三,将第 i 位设置为1

思路:第 i 位与0 或,值不变。第 i 位与1 或,变成1。因此,我们的目标是 num 与 一个第 i 位值为1,其它位的值都为0的数相 或

//将 整数 num 的第 i 位的值 置为 1
    private static int getBit(int num, int i)
    {
        return (num | (1 << i));
    }

 

四,将第 i 位设置为0(清0)

思路:第 i 位和0与,第 i 位就变成了0。其它位 都与 1 与,其它位保持不变。这样,就只把第 i 位清0了

//将 整数 num 的第 i 位的值 置为 1
    private static int getBit(int num, int i)
    {
       int mask = ~(1 << i);//000100
       return (num & (mask));//111011
    }

 

总之,获取第 i 位的值,或者是将第 i 位置0或者置1  ,其总体思路就是:将 1 左移 i 位,然后再进行 与操作 或者  或操作。

今天关于Java中的二进制文本java中的二进制文本是什么的介绍到此结束,谢谢您的阅读,有关-128作为Java中的二进制文字、C++中的二进制文件操作及示例代码、hta实现的二进制文件转换为文本、JAVA中常用的二进制位操作等更多相关知识的信息可以在本站进行查询。

本文标签: