Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #24481 > unrolled thread

convert byte array to hex string using BigInteger

Started byLaura Schmidt <ls@mailinator.com>
First post2013-06-20 14:03 +0200
Last post2013-11-24 10:43 +0000
Articles 7 on this page of 27 — 12 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  convert byte array to hex string using BigInteger Laura Schmidt <ls@mailinator.com> - 2013-06-20 14:03 +0200
    Re: convert byte array to hex string using BigInteger Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-06-20 09:03 -0400
    Re: convert byte array to hex string using BigInteger rossum <rossum48@coldmail.com> - 2013-06-20 14:09 +0100
      Re: convert byte array to hex string using BigInteger Laura Schmidt <ls@mailinator.com> - 2013-06-20 15:32 +0200
        Re: convert byte array to hex string using BigInteger rossum <rossum48@coldmail.com> - 2013-06-20 15:44 +0100
        Re: convert byte array to hex string using BigInteger lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-20 19:22 +0100
          Re: convert byte array to hex string using BigInteger markspace <markspace@nospam.nospam> - 2013-06-20 11:56 -0700
            Re: convert byte array to hex string using BigInteger lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-20 20:43 +0100
          Re: convert byte array to hex string using BigInteger Lew <lewbloch@gmail.com> - 2013-06-20 13:39 -0700
            Re: convert byte array to hex string using BigInteger lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-21 08:17 +0100
          Re: convert byte array to hex string using BigInteger Arne Vajhøj <arne@vajhoej.dk> - 2013-06-26 22:16 -0400
    Re: convert byte array to hex string using BigInteger Joerg Meier <joergmmeier@arcor.de> - 2013-06-20 22:09 +0200
    Re: convert byte array to hex string using BigInteger Roedy Green <see_website@mindprod.com.invalid> - 2013-06-21 14:51 -0700
      Re: convert byte array to hex string using BigInteger Joerg Meier <joergmmeier@arcor.de> - 2013-06-22 00:26 +0200
        Re: convert byte array to hex string using BigInteger Roedy Green <see_website@mindprod.com.invalid> - 2013-06-23 08:49 -0700
          Re: convert byte array to hex string using BigInteger Joerg Meier <joergmmeier@arcor.de> - 2013-06-23 22:37 +0200
        Re: convert byte array to hex string using BigInteger Roedy Green <see_website@mindprod.com.invalid> - 2013-06-23 12:40 -0700
          Re: convert byte array to hex string using BigInteger Joerg Meier <joergmmeier@arcor.de> - 2013-06-23 22:41 +0200
    Re: convert byte array to hex string using BigInteger Arne Vajhøj <arne@vajhoej.dk> - 2013-06-26 22:12 -0400
      Re: convert byte array to hex string using BigInteger Arne Vajhøj <arne@vajhoej.dk> - 2013-06-26 22:20 -0400
      Re: convert byte array to hex string using BigInteger Laura Schmidt <ls@mailinator.com> - 2013-06-28 07:55 +0200
        Re: convert byte array to hex string using BigInteger Joerg Meier <joergmmeier@arcor.de> - 2013-06-29 00:01 +0200
      Re: convert byte array to hex string using BigInteger Stanimir Stamenkov <s7an10@netscape.net> - 2013-06-29 19:10 +0300
        Re: convert byte array to hex string using BigInteger Arne Vajhøj <arne@vajhoej.dk> - 2013-07-03 22:24 -0400
    Re: convert byte array to hex string using BigInteger aydin.k.abadi@gmail.com - 2013-11-23 16:40 -0800
    Re: convert byte array to hex string using BigInteger aydin.k.abadi@gmail.com - 2013-11-23 16:43 -0800
      Re: convert byte array to hex string using BigInteger Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-11-24 10:43 +0000

Page 2 of 2 — ← Prev page 1 [2]


#24538

FromLaura Schmidt <ls@mailinator.com>
Date2013-06-28 07:55 +0200
Message-ID<kqj8fp$qin$1@news.albasani.net>
In reply to#24535
On 06/27/2013 04:12 AM, Arne Vajhøj wrote:
> On 6/20/2013 8:03 AM, Laura Schmidt wrote:

> I will strongly recommend you drop the idea about using
> BigInteger for this.

Thank you! I agree. In general, I try not to duplicate code that already 
exists in standard libraries, but in this case there were too many hacks 
needed when using BigInteger.

Laura

[toc] | [prev] | [next] | [standalone]


#24539

FromJoerg Meier <joergmmeier@arcor.de>
Date2013-06-29 00:01 +0200
Message-ID<17d3f6bde2ws$.kpx4wkwzxutg.dlg@40tude.net>
In reply to#24538
On Fri, 28 Jun 2013 07:55:05 +0200, Laura Schmidt wrote:

> On 06/27/2013 04:12 AM, Arne Vajhøj wrote:
>> On 6/20/2013 8:03 AM, Laura Schmidt wrote:
>> I will strongly recommend you drop the idea about using
>> BigInteger for this.
> Thank you! I agree. In general, I try not to duplicate code that already 
> exists in standard libraries, but in this case there were too many hacks 
> needed when using BigInteger.

Thankfully, as I pointed out in
<16xd6kqk55a7s$.1w2vnmqtc1nx4.dlg@40tude.net>, you still don't need to
duplicate code that already exists.

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#24541

FromStanimir Stamenkov <s7an10@netscape.net>
Date2013-06-29 19:10 +0300
Message-ID<kqn0j3$aj1$1@dont-email.me>
In reply to#24535
Wed, 26 Jun 2013 22:12:30 -0400, /Arne Vajhøj/:

> You have already received several working solutions.
>
> I will strongly recommend you drop the idea about using
> BigInteger for this.
>
> It becomes extremely tricky to get it right - you have already
> found the sign problem - next problem will be the leading zero
> problem.
>
> [... bunch of code...]

I may be dense, but I didn't got why using BigInteger is not good? 
What's wrong with the following, for example:

     byte[] val;
     ...
     String.format("%064x", new BigInteger(1, val));

I guess one could come up with a method like:

     String toHexString(byte[] val, int len) {
         BigInteger num = new BigInteger(1, val);
         return (len > 0) ? String.format("%0" + len + "x", num)
                          : String.format("%x", num);
     }

Doesn't it appear sufficient?

-- 
Stanimir

[toc] | [prev] | [next] | [standalone]


#24615

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-07-03 22:24 -0400
Message-ID<51d4dcd0$0$290$14726298@news.sunsite.dk>
In reply to#24541
On 6/29/2013 12:10 PM, Stanimir Stamenkov wrote:
> Wed, 26 Jun 2013 22:12:30 -0400, /Arne Vajhøj/:
>
>> You have already received several working solutions.
>>
>> I will strongly recommend you drop the idea about using
>> BigInteger for this.
>>
>> It becomes extremely tricky to get it right - you have already
>> found the sign problem - next problem will be the leading zero
>> problem.
>>
>> [... bunch of code...]
>
> I may be dense, but I didn't got why using BigInteger is not good?
> What's wrong with the following, for example:
>
>      byte[] val;
>      ...
>      String.format("%064x", new BigInteger(1, val));

You will not be able to get back to the same byte array with that.

> I guess one could come up with a method like:
>
>      String toHexString(byte[] val, int len) {
>          BigInteger num = new BigInteger(1, val);
>          return (len > 0) ? String.format("%0" + len + "x", num)
>                           : String.format("%x", num);
>      }
>
> Doesn't it appear sufficient?

It seems to work.

I would probably drop the len argument and use 2*val.length.

But it is still not as simple as the DatatypeConverter.

Arne




[toc] | [prev] | [next] | [standalone]


#26591

Fromaydin.k.abadi@gmail.com
Date2013-11-23 16:40 -0800
Message-ID<5e580259-cc16-42c4-ae7b-118d52dee294@googlegroups.com>
In reply to#24481
Hi
I`m wondering, how I can implement java constructor in C++ and what private member I need ? 
Thanks Aydin 




On Thursday, June 20, 2013 1:03:21 PM UTC+1, Laura Schmidt wrote:
> Hi,
> 
> 
> 
> I try to convert a byte array to a hex string like this:
> 
> 
> 
>   private static String hex_encode (byte [] val)
> 
>   {
> 
>    BigInteger b = new BigInteger(val);
> 
> 
> 
>    String t = b.toString(16);
> 
> 
> 
>    return (t);
> 
>   }
> 
> 
> 
> For a long byte array it returns a "negative" hex string, i. e. starting 
> 
> with a "-" sign.
> 
> 
> 
> But I want just the bytes in the array converted to hex representation, 
> 
> each one ranging from "00" to "FF". There should be no minus sign then.
> 
> 
> 
> Can you help?
> 
> Thanks!
> 
> 
> 
> Laura

[toc] | [prev] | [next] | [standalone]


#26592

Fromaydin.k.abadi@gmail.com
Date2013-11-23 16:43 -0800
Message-ID<aaa71880-263c-422c-9b59-44b3520950db@googlegroups.com>
In reply to#24481
Hi
in c++ ,there is no val.length to check the size of val similar to Binint(byte [] val) in java, can anybody let me know,please,what exactly I need to do. 


Regards
Aydin


On Thursday, June 20, 2013 1:03:21 PM UTC+1, Laura Schmidt wrote:
> Hi,
> 
> 
> 
> I try to convert a byte array to a hex string like this:
> 
> 
> 
>   private static String hex_encode (byte [] val)
> 
>   {
> 
>    BigInteger b = new BigInteger(val);
> 
> 
> 
>    String t = b.toString(16);
> 
> 
> 
>    return (t);
> 
>   }
> 
> 
> 
> For a long byte array it returns a "negative" hex string, i. e. starting 
> 
> with a "-" sign.
> 
> 
> 
> But I want just the bytes in the array converted to hex representation, 
> 
> each one ranging from "00" to "FF". There should be no minus sign then.
> 
> 
> 
> Can you help?
> 
> Thanks!
> 
> 
> 
> Laura

[toc] | [prev] | [next] | [standalone]


#26593

FromAndreas Leitgeb <avl@gamma.logic.tuwien.ac.at>
Date2013-11-24 10:43 +0000
Message-ID<slrnl93m2j.42s.avl@gamma.logic.tuwien.ac.at>
In reply to#26592
Not sure, if I understand you correctly, but why use a BigInteger or even C++ at all?
The thread you replied to was quite long already, and I think there were a couple
of alternatives posted in it to do this rather simple task (byte[] -> hexstring)
in a loop, even without BigInteger.

Anyway, in C++ you would either pass the length of the array as a separate
argument, or create a wrapper class that packs pointer and length into one
entity (thus, mimicking Java).


aydin.k.abadi@gmail.com <aydin.k.abadi@gmail.com> wrote:
> Hi
> in c++ ,there is no val.length to check the size of val similar to Binint(byte [] val) in java, can anybody let me know,please,what exactly I need to do. 
>
> Regards
> Aydin
>
>
> On Thursday, June 20, 2013 1:03:21 PM UTC+1, Laura Schmidt wrote:
>> Hi,
>> I try to convert a byte array to a hex string like this:
>> 
>>   private static String hex_encode (byte [] val)
>>   {
>>    BigInteger b = new BigInteger(val);
>> 
>>    String t = b.toString(16);
>> 
>>    return (t);
>>   }
>> 
>> For a long byte array it returns a "negative" hex string, i. e. starting 
>> with a "-" sign.
>> 
>> But I want just the bytes in the array converted to hex representation, 
>> each one ranging from "00" to "FF". There should be no minus sign then.
>> 
>> Can you help?
>> Thanks!
>> Laura

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.java.programmer


csiph-web