编解码bin文件

Encoding and Decoding bin file

我在网上看到了很多例子,但它们并不是我要找的。

我有一个 .bin 文件,我想 POST 到服务器,所以我将其编码并存储为字符串。

然而,对其进行解码并没有给出相同的结果。

import org.apache.commons.codec.binary.Base64;

File file = getFileStreamPath(ENROLL_FILENAME);
byte[] bytes = loadFile(file);
byte[] encoded = Base64.encodeBase64(bytes);

for(int i =0; i< encoded.length; i++){
    System.out.print(encoded[i]);
}
System.out.println();

String encodedString = new String(encoded); //send to sever and decode
System.out.println("Encode "+ encodedString);

byte[] decoded = Base64.decodeBase64(encodedString.getBytes());
for(int i =0; i< decoded.length; i++){
    System.out.print(decoded[i]);
}
System.out.println();

结果:

System.out: 1147948656688781216567661069850481179071904876109571171018810311782109108117905086121997274112981108285908749119987170489081656565656565656565666511965678710365769951100112904878789087498010050537565651041221005010811081496648991101041191011036565666565656567122119891035265658065115656565686565656511081736565757366656565706565656565656565656565656565661016881656583857712010311971576582896765119491017310369786574119798181658665676580886569866574107788465658765661038082119698965748977756569906573119781148169976580115751026569986577657448103659965736578886569996566697811610369102657273755310365103656548781181036510465658579104656910565777374122119651086565527811281691086572107707165691106565119794365651126573897855656511465651157790656911465746979791196911865668178103816911965795675656569122656669787465691226566107795511965496574737768103694965668179841036550657473798811965516566103808465695365747778108119695365806573104816554656511580102656954656681759010369576566567886119666665737379831197066656765789711966686574103805211966736573111809065707665678979101103668165666980115103708365785677856570886567997970816697657289789011966976573858011381709965767378838166102656510779708166103656910778791196610465651077885817010465686580741036610665801117610265701096568657779103701146575698080656611565678178861196611565668979119103661156565119801001196611665737378107119661166573568097816612065658178978170122656885801121197050657665791081196653657248789065665565655276102119665565731077767103705665686980103817043657673801038167676580103777011971676568858011465717165688579122816772657399785011967776574737810211971776568103791068171806576997953119678265705278571036783656948808910371836568568010311967856573817610981718965699977100119679065805277111657197656973771161036798657377781161036710465801197811410371109656965797665671126566898085816711265756580108657111565778178791037111765777380106656712065657378102817112165698979110119711226577737869656756656777801066567476565103791101036865656565781148168656572737789103726665778980100816867656665805565686965711118011781687065738978120816872657252784881687665727780110816878657356791076572786577857976119688165655280114817281656969767711972876570111809711968906573103751011196890657369768481689965731157811365689965721118010211972996577107808010372104657873808511968106657277781081196810665728579116103721086574487376103681096567111798811968109657211177801036811265714879701196811365688980891197211765781038010110368118657156771076572118657077751001197248657811579731036851657552797910368536579107791141197257656910373821036843658069797065724765796580578165676510185801066569686585697487816971651018179901196972658611179761036574651004879114816977651006977688269817268798174105756874476571675081737353717976897773778950101661211017370876968701201077373101479067431225273759771741127765767454877010184577010275707110654677250107756897721197086101657110611212272821005267112113746811643677411652767511848536757514370118102827283103118721196711567854912170651041227088905572104864966121575269901176570102100557371538267113785169111685375727567771111211196890566770739772708011778705478556989885170887170757211452765284546749115676810310284718111711772117768367844379691021197471711076672110111686811770436911810772101103656566656589897310710410555998210880778212210210386865265985210211577995111578725185808680818510688107751138810777981118578731025287104102111821095265837551738811110210399871185285103111698853102115891064781978172527653781157987105488783675689111102567870731157910010248825411056874711989826611948875211985888965859776894875111111998012165119825489698782811037956103778282811038797801159065735279691046582568811110347471111031147211178120748982105736582545289107110537773771107785103515697565181995065738973656989978899986857119101786752901218011199547277109438077115497389768012185875347859079867398971218179112471078372895287886511597535211978501091117911111865856511711110174474873748948881084710789651191199899801118210573658566899985986689887612077871221021031011191105610751881071081
System.out: Encode rO0ABXNyACBjb20uZGZ0Lm9ueXguRmluZ2VycHJpbnRUZW1wbGF0ZQAAAAAAAAABAwACWgALc3dpZ0NNZW1Pd25KAAhzd2lnQ1B0cnhwegAABAAAACzwYg4AAPAsAAADAAAAnQIAAKIBAAAFAAAAAAAAAAAAAABeDQAASUMxgwG9ARYCAw1eIgENAJwOQQAVACAPXAEVAJkNTAAWABgPRwEYAJYMKAEZAIwNrQEaAPsKfAEbAMAJ0gAcAIANXAEcABENtgEfAHIK5gAgAA0NvgAhAAUOhAEiAMIJzwAlAA4NpQElAHkFGAEnAAwO+AApAIYN7AArAAsMZAErAJEOOwEvABQN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E4aHsF2BACeGwBsCQFeIBb+p8VQBgCdIQzC/z8DAPwrBsEGAT8uEP7B/cAIAO4vFlhBBAG5Mm0wCAEnMxP/N0AFAA8098D8/hkA4TUPwD7/Qf4uRPxd//1lFAEDNhbAwP7/KcApwD8xBQEmNxf/QiIAYjgWcsJYV/4u/v4hRP/+wMJDYgcAiTgJ/13CBAAuPP1QHQCIPQ9za0H+wC4p/S9GRAsBT0Aa/ktVLBUA3UETW//+wSL9wP7+/v/A/zsGAFNDei/GBwFNRSDB/cA7FgDdTQYr//3AKv/9wP79aMA1BQFmTiL+wP4QAApO/cL8wMMqTv/DPgcAflAM/23DCAAJUwnE//7DMQYAe1QTdMEDAZdVIvwRAVNbIP79S8H+KFH+wAQAY1yDfQQAa1wXjwoADpA3wcPCjG8EAEtgCcL8BAGqYDf/whAAE2RDf8LDwMTAwl1uCAFVZCkvNRkAzGUG/if//fsp///+Vv9zwMDC
System.out: -84-19051151140329911110946100102116461111101211204670105110103101114112114105110116841011091121089711610100000001302900111151191051036777101109791191107408115119105103678011611412011212200400044-16981400-1644003000-99200-94100500000000000941300736749-1251-67122231394341130-100146502103215921210-103137602202415711240-10612401250-11613-831260-5101241270-649-460280-128139212801713-74131011410-2603201313-660330514-1241340-629-4903701413-91137012152413901214-80410-12213-20043011121001430-111145914702013-1271480-17100151017133615102514-170530-110121415302014780540-110149505502415761570-10913-1051570-168-123058011151241580201010216103113870650-1261475165032131070670-10415-290720-118151001750381412208001715-781820-33128018703914210900118131030900-12315-871920-781373095091421096073135909709138119704815380990-6111241102048125811070-95156001080361387010802214-6201080121511901090-12613-10901090-1131510501130413105111505315-8911180-8014-105012101251310001230141112701230-1191210112404915-12711260-7815-1270-1260-812231-12605315-841-12205314-510-1210-12113-370-1160-110131271-11605614-1151-1130-7314-250-11109413-100-11007715981-11006315-1250-1080-12411-1031-104071121190-1030-212-961-10206612-740-1010-12513-740-950-413-821-9006414440-8702215810-870-9615-1081-840-6013581-820-6215-1160-7902131251-7807014-971-770-6213160-6803515-1160-650814-980-640013-830-64011412981-630-58151170-6201615-200-60010615-710-590-12213-590-57012613-470-53011515-990-510-11314-1121-510-5914470-4801415-831-4806511511-42090151070-390-120101230-390-12711770-360-11713-880-360122151271-360-5515621-310-4615830-29011513-1050-29011714-741-270-998460-2604214950-26012212620-23010914230-2205415991-180-40151220-17011112-1121-17083101191-120-3714340-90-8214580-70-2314-811-30728700-20-1514201-10-3215-11021-2715-11613165989161-2814103171901446091-3514-831121-47121317162851-1123834-12539-16611100835-111-11445-125849-115-98739-120219732325833-17-3911-20-832-90-12238-109044-98-10621-28-321-14-1232662-126311051013-95-162187-1282658115292312010-102-11914-33-12638-341142-35711-35-222-9-47294047310-8497711420811521118123302111774712017-101-12821-9123321108110-9311918-128-740114-12650-116-8013-97220-122-12120-5-11523-9312317-123-921113-12340122-847-124-611912147-452511-8230-30-46963-11417-4924105130122314-3112618-7712200402496-11933-117-7317-108-1317-51-82187-12827-31-51211512313311171584-1220-11512110-8712112110-1231333-222-123-617-101-128184311423-95-82890-220-126-12723-27-524-113-12266412611-28-3714904522724724-95-11320-11714117-317-2212722-161771322-295239652645-11510-94-12115-561217-23-1272269814-1431769822104-5250-11414181617-1512232-1-632-8412213-60-10617-120-12817-21-12236-97-10985011520-12512726-1311628-40224321241051192715-3630524625-56-628-2411538-8-1344-44-12211633722-25-11255782271073614-89-71829-11422921126-25-11613-3810614-94-16202-223039-3837-11523-105-72431227112-617-120-128205-121201082223471922-51-830-6212736-3512137-5041432723-60-127-22629-73-10201212121-37-1162566629-77-11313-1453764-1738-1947478610210-107-9231672367130-69-122674451930124391810943288221-251162356-12723-62-19248911113-84-452617239123-62421112514-15018-41-2719-106-1330112-1246-9-4517627938566441673711-77-2714113021-131530-122-131012925-65-11329451030106-51713-791926-12421-126-1524-106-4223-72-525-12012636-131038-115-825-5011330-63122427110845-81-12711242718602724-204227921419117181991320-11582472-11129-92-12341-10312245-24-11347-13125151151261641-72344-11824100116133618248862878-105281131311-49-1015113-126197-182357127235152693-12530-27532-108-1017412326-109-23649-7372411013-74-1824115-12324-128-123493-71169102021-82488-11627114-123744-11116-94724-121224-119-1231497-430-83-2841-71-4550-62-97142012221-11-525-122-126269612576111118-6011623541182412012314-35437-71-244251-10850-65-4117-1-12323-95-1326-84-1832-119-11157-112884129-871233092413-252151241128-9694094151414-1221481-1272990-1530-64-7148212715-2911221112522-3311713-78-5215371728-79-4141124515-74-1728-68-69301728373324305773210103742-11542101-713108-12218-72-11625-74-53048-126769515-45-11221-125-1252581713114122258

为什么 byte 数组不一样?

我认为它们是一样的。请注意,输出的第一行是 encoded 字节,114 是 'r' 的十进制 ASCII 码,79 是 'O' 的十进制 ASCII 码, 48 是“0”等的十进制 ASCII 代码,因此您的第一行和第二行匹配,并且是我所期望的代码。

然后当你解码值时,字节被符号扩展为整数,所以 -84 实际上是 256 - 84 = 172,-19 是 256 - 19 = 237,0 是 0,等等。 ,如果您使用命令行工具对第二行进行 base64 解码:

$ base64 -d < x.bin | od -tu1 | head -2
0000000 172 237   0   5 115 114   0  32  99 111 109  46 100 102 116  46
0000020 111 110 121 120  46  70 105 110 103 101 114 112 114 105 110 116

你会发现事情是一致的。所以我认为一切都很好,只是你打印的方式和内容让事情看起来不对劲。

据我所知,您打印出编码、编码字符串和解码但不是字节。你能按字节检查你的数组是否相等吗?