十六进制的字符串还原成图片的问题。3Q,很着急 ( 积分: 5 )

H

himoo

Unregistered / Unconfirmed
GUEST, unregistred user!
我在oracle中的一个blob中保存了几张图片,保存时把它转换成了16进制,每张图片后用|分开,如 FFFFF...|FFFFFF....|
我写了以下程序,把每一段的十六进制保存成图片:这段是java翻译过来的,该java可以实现我的功能。(ss是那每段的十六进制)
procedure TMainForm.SaveImg(ss, path, fileName:string);
var
bArray:Array of byte;
i, j, high1, low1:integer;
tempstream:Tstream;
begin
if (length(ss) mod 2 <>0 ) then
begin
exit;
end;
setlength(bArray, length(ss) div 2);
tempStream := Tfilestream.Create(path + fileName, fmCreate);
j:=0;
for i := 0 to length(ss)-1 do
if (i=0) or ((i mod 2)=0) then
begin
high1 := charToNibble(ss);
low1 := charToNibble(ss[i+1]);
bArray[j]:= ((high1 shl 4) or low1) ;
j:=j+1;
end;
tempStream.WriteBuffer(bArray, length(bArray));
tempStream.Free;
end;

function TMainForm.charToNibble(c:char):integer;
begin
if (c>='0') and (c<='9') then
result := Ord(c) - Ord('0')
else if (c>='a') and (c<='f') then
result := 10+Ord(c)- ord('a')
else if (c>='A') and (c<='F') then
result := 10+Ord(c)- ord('A');

end;
可是生成文件后,还是不能正常地显示成原图片。

原java如下
public static void main(String[] args) throws Exception {

//-----将十六进制字符串生成图片文件-----

String s = &quot;&quot;;

byte[] ss = str.getByte(s);

FileOutputStream fw = new FileOutputStream(&quot;E://xcxc2.jpg&quot;);

fw.write(ss);

//----- END -----

}



public static byte[] getByte(String hexString) throws Exception {



if (hexString.length() % 2 != 0) {

return null;

}



byte[] bArray = new byte[hexString.length() / 2];

int j = 0;

for (int i = 0; i < hexString.length(); i += 2, j++) {

int high = charToNibble(hexString.charAt(i));

int low = charToNibble(hexString.charAt(i + 1));

bArray[j] = (byte) ((high << 4) | low);



}

return bArray;

}



private static int charToNibble(char c) {

if ('0' <= c &amp;&amp; c <= '9') {

return c - '0';

}

else if ('a' <= c &amp;&amp; c <= 'f') {

return c - 'a' + 0xa;

}

else if ('A' <= c &amp;&amp; c <= 'F') {

return c - 'A' + 0xa;

}

else {

throw new IllegalArgumentException(&quot;Invalid hex character: &quot; + c);

}

}

大家帮我看看哪里有问题?谢谢
 
顶部