Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convert int to hex

133 views
Skip to first unread message

ghada glissa

unread,
Jan 27, 2015, 4:55:23 PM1/27/15
to
Dear all,

Is there any predefined function that convert in to hex or octet string in c++.

Regards.

Mr Flibble

unread,
Jan 27, 2015, 5:09:33 PM1/27/15
to
std::ostringstream.

/Flibble

Ian Collins

unread,
Jan 27, 2015, 5:12:32 PM1/27/15
to
ghada glissa wrote:
> Dear all,
>
> Is there any predefined function that convert in to hex or octet string in c++.

It's easy with a stringstream:

std::string intToHex( int n )
{
std::ostringstream os;

os << std::hex << n;

return os.str();
}

Or more generally:

template <typename T> std::string integerToHex( T n )

--
Ian Collins

ghada glissa

unread,
Jan 27, 2015, 5:42:20 PM1/27/15
to
Thank you for your reply.
This function intToHex return a string or i want to get an array of char that will be easy to handle.

for exp res=intToHex(X)=12f5e188
i want it
res[0]=12
res[1]=f5
res[2]=e1
res[3]=88

Regards.

Ian Collins

unread,
Jan 27, 2015, 6:16:25 PM1/27/15
to
ghada glissa wrote:
> Le mardi 27 janvier 2015 22:55:23 UTC+1, ghada glissa a écrit :
>> Dear all,
>>
>> Is there any predefined function that convert in to hex or octet string in c++.
>>
>> Regards.
>
> Thank you for your reply.

Please reply to the correct message and quote some context!

> This function intToHex return a string or i want to get an array of char that will be easy to handle.

You asked for a function that converted to a string....

You can't return an array from a function, so how about:

template <typename T> std::array<unsigned char,sizeof(T)>
toChar( T n )
{
constexpr auto size = sizeof(T);

std::array<unsigned char,size> data;

for( auto i = size; i; --i, n >>= 8 )
{
data[i-1] = n;
}

return data;
}

Which passes your test.

> for exp res=intToHex(X)=12f5e188
> i want it
> res[0]=12
> res[1]=f5
> res[2]=e1
> res[3]=88
>
> Regards.
>


--
Ian Collins

Christopher Pisz

unread,
Jan 27, 2015, 6:50:41 PM1/27/15
to
If you want to convert each byte and save it independently, you'd
probably need to bitmask your int, figure out how many bytes it is, get
one byte from each mask, convert that to hex using stringstream and
std::hex, std::setw, and std::fill.

Message has been deleted
Message has been deleted

Ian Collins

unread,
Jan 28, 2015, 1:19:26 AM1/28/15
to
Stefan Ram wrote:
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>> Then, I'd think along the lines of »::std::hex« and stringstreams.
>
> #include <iostream>
> #include <ostream>
> #include <ios> // ::std::hex
> #include <sstream>
>
> ::std::string to_hex_string( int const i )
> { ::std::stringstream s; s << ::std::hex << i; return s.str(); }
>
> int main(){ ::std::cout << to_hex_string( 16 )<< '\n'; }

Which (minus the superfluous scoping) was what I originally posted!

--
Ian Collins

ghada glissa

unread,
Jan 28, 2015, 9:41:03 AM1/28/15
to
Le mardi 27 janvier 2015 22:55:23 UTC+1, ghada glissa a écrit :
Dear,

I'm sorry for any inconvenience but i'm unable to convert each byte and save it independently.

Regards.

Greg Martin

unread,
Jan 28, 2015, 10:03:38 AM1/28/15
to
I think the problem, here and in comp.lang.c is how you asked the
question. Is this what you're looking for?


#include <stdio.h>
#include <stdint.h>
#include <string.h>

int main () {
int i = 0x12f5e188;
unsigned char bytes[4];

memcpy (bytes, &i, 4);

for (int i = 0; i < 4; ++i) {
printf ("%x\n", bytes[i]);
}

return 0;
}

Note that the order the bytes are printed in will depend on the byte
order of the platform.


--
http://www.softsprocket.com

ghada glissa

unread,
Jan 28, 2015, 10:26:19 AM1/28/15
to
Thank you for this solution but %x is used to display in hexadecimal or I want a function that returns the conversion of each byte independently.

Regards.

Greg Martin

unread,
Jan 28, 2015, 11:14:51 AM1/28/15
to
Wellthen, you have an array of bytes that you can now convert into an array of
strings using one of suggestions that have been made, or is
this about you avoiding any effort on your part?

--
http://www.softsprocket.com

ghada glissa

unread,
Jan 28, 2015, 11:23:37 AM1/28/15
to
Le mardi 27 janvier 2015 22:55:23 UTC+1, ghada glissa a écrit :
No, i really need help,I'm new in C ++ and I can not easily handle its concepts.
Message has been deleted

Ian Collins

unread,
Jan 28, 2015, 1:31:42 PM1/28/15
to
ghada glissa wrote:
>
> I'm sorry for any inconvenience but i'm unable to convert each byte and save it independently.

What was wrong with the solution I posted? It passed the test case you
posted.

--
Ian Collins

Geoff

unread,
Jan 28, 2015, 2:30:27 PM1/28/15
to
You also seem to have trouble with the concept of how to reply to
other people on Usenet. DO NOT REPLY TO YOUR OWN POSTS.

ghada glissa

unread,
Jan 28, 2015, 3:48:00 PM1/28/15
to


> You also seem to have trouble with the concept of how to reply to
> other people on Usenet. DO NOT REPLY TO YOUR OWN POSTS.

I do not speak to you Mr Geoff, remains calm and polite please.

Rosario193

unread,
Jan 29, 2015, 2:52:14 AM1/29/15
to
#include <stdio.h>
#define u8 unsigned char

// return one pointer to pointer to arrays
// one array is 8 bit partition of the argument a [swapped]
// one as 4 bit partition of the argument a [swapped]
// and conversion to digits they rapresent
u8** intToHexChar(int a)
{static u8 r0[ sizeof(int)+4];
static u8 r1[2*sizeof(int)+4];
static u8 *r[2]={r0, r1};
static u8 hexStr[]="0123456789ABCDEF";
unsigned i, aa;

aa=a;
for( i=sizeof(int)-1; ; --i )
{r0[i]=aa&0xFF; aa>>=8;
if(i==0) break;
}
aa=a;
for( i=2*sizeof(int)-1; ; --i )
{r1[i]=hexStr[aa&0xF]; aa>>=4;
if(i==0) break;
}

return r;
}

int main(void)
{int m=0x12f5e188, i;
u8 **r;
unsigned x;

r=intToHexChar(m);
printf("As Hex: ");
for(i=0; i<sizeof(int) ; ++i)
{x=r[0][i];
printf("%x#", x);
}
printf("As Chars: ");
for(i=0; i<2*sizeof(int); ++i)
printf("%c#", r[1][i]);
printf("\n");
return 0;
}

As Hex: 12#f5#e1#88#As Chars: 1#2#F#5#E#1#8#8#

Ian Collins

unread,
Jan 29, 2015, 3:22:21 AM1/29/15
to
Rosario193 wrote:
> On Tue, 27 Jan 2015 13:55:13 -0800 (PST), ghada glissa wrote:
>
>> Dear all,
>>
>> Is there any predefined function that convert in to hex or octet string in c++.
>>
>> Regards.
>
>
> #include <stdio.h>
> #define u8 unsigned char

No!

--
Ian Collins

jak

unread,
Jan 29, 2015, 8:48:18 AM1/29/15
to
Forgive me but I did not understand clearly what you want but you can
take a look at this example. Maybe something can be worthwhile:

#include <stdio>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

string BaseConv(int, void *, int);
// BaseConv parameters:
// 1) Byte offset from base address
// 2) Base address
// 3) Conversion base: 2 to 16

int main(int argc, char *argv[])
{
long Value = 0x12f5e188;
int CBase;
vector <string> Cnv;
unsigned int i;

for (CBase = 2; CBase <= 16; CBase++)
{
cout << endl << "Base " << CBase << ": " << endl;
Cnv.clear();
for (i = 0; i < sizeof Value; i++)
Cnv.push_back(BaseConv(i, &Value, CBase));

for (i = 0; i < Cnv.size(); i++)
cout << i + 1 << ") " << Cnv[i] << endl;
cout << endl;
system("pause");
}
return 0;
}

string BaseConv(int BytePos, void *Data, int Base)
{
unsigned char Byte = ((unsigned char *)Data)[BytePos];
string Res = "";
const char LT[] = "0123456789ABCDEF";

if (Base > 1 && Base <= 16)
for (; Byte > 0; Byte /= Base)
Res = LT[Byte % Base] + Res;
return Res;
}

Christopher Pisz

unread,
Jan 29, 2015, 11:06:16 AM1/29/15
to
You win the ugly code of the week award.

What's wrong with #include <cstdio> ?

Defining your own types as primitive types accomplishes what? One more
search someone has to do when looking at your code.

Naming all variables with a single or double letter rather than a
meaningful name.

(void) parameters instead of (), for what purpose? I thought you were
trying to save keystrokes.

more than one statement on the same line

Statements sometimes on same line with brackets, sometimes not,
sometimes no brackets at all. At least be consistent with ugly style.



Öö Tiib

unread,
Jan 29, 2015, 1:26:55 PM1/29/15
to
On Thursday, 29 January 2015 18:06:16 UTC+2, Christopher Pisz wrote:
>
> What's wrong with #include <cstdio> ?

Does '#include <cstdio>' guarantee that we can use 'printf'
or only 'std::printf' (that Rosario193 will likely never write)?
AFAIK there are no guarantee despite in practice both are usually
defined after '#include <cstdio>'.

Rosario193

unread,
Jan 30, 2015, 5:04:57 AM1/30/15
to
On Thu, 29 Jan 2015 10:06:05 -0600, Christopher Pisz wrote:
>On 1/29/2015 1:51 AM, Rosario193 wrote:
>> On Tue, 27 Jan 2015 13:55:13 -0800 (PST), ghada glissa wrote:
>>
>>> Dear all,
>>>
>>> Is there any predefined function that convert in to hex or octet string in c++.
>>>
>>> Regards.
>>
>>
>> #include <stdio.h>
>> #define u8 unsigned char
>>
>> // return one pointer to pointer to arrays
>> // one array is 8 bit partition of the argument a [swapped]
>> // one as 4 bit partition of the argument a [swapped]
>> // and conversion to digits they rapresent
>> u8** intToHexChar(int a)
>> {static u8 r0[ sizeof(int)+4];
>> static u8 r1[2*sizeof(int)+4];
>> static u8 *r[2]={r0, r1};
>> static u8 hexStr[]="0123456789ABCDEF";
>> unsigned i, aa;
>>
>> aa=a;
>> for( i=sizeof(int)-1; ; --i )
>You win the ugly code of the week award.
>
>What's wrong with #include <cstdio> ?

i have one old C++ compiler that allow that

>Defining your own types as primitive types accomplishes what? One more
>search someone has to do when looking at your code.

here there is not "stdint.h" where would it be uint32_t

>Naming all variables with a single or double letter rather than a
>meaningful name.

not for the words one always use

>(void) parameters instead of (), for what purpose? I thought you were
>trying to save keystrokes.

i want sys call "int main(void)"
and not call "int main()"

>more than one statement on the same line

instruction that are connected, i sometime write in the same line

>Statements sometimes on same line with brackets, sometimes not,
>sometimes no brackets at all. At least be consistent with ugly style.

they call it readability...

Rosario193

unread,
Jan 30, 2015, 5:05:22 AM1/30/15
to
#include <stdio.h>
#include <string.h>
#include <limits.h>

#define u8 unsigned char
#define u32 unsigned
#define i32 int
#define P printf
#define F for
#define R return


// result ok if return one number > 0
// that is the len of the string result even if there is some 0 in it
i32 uToA(u8* rstr, u32 rstrsize, u32 argnumber, u32 base)
{u32 i, j, k;
u8 tmp[64];
u8 str[64]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

if(rstr==0) R -1;
if(rstrsize > 0xFFFFFFF) R -1;
rstr[0]=0;
if(base<=1 || base>255) R -1;

F( i=0, tmp[0]=0; argnumber; ++i)
{tmp[i]=argnumber%base; argnumber/=base;}
if(i+2>rstrsize) R -2;
if(i!=0) --i;
if(base<=36)
{F(k=0, j=i; ;--j, ++k)
{rstr[k]=str[tmp[j]];
if(j==0) break;
}
}
else {F(k=0, j=i; ;--j, ++k)
{rstr[k]=tmp[j];
if(j==0) break;
}
}
++k; rstr[k]=0;
R k;
}



int main(void)
{u32 m=0x12f5e188, i, x, base;
i32 r;
u8 result[128]; // 0..126 127[0]

F(base=0; base<=256; ++base)
{r=uToA(result, 128, m, base);
P("base=%u r=%u ", base, r);
if(r>0)
{F(i=0; i<(u32)r; ++i)
{if(base<=36) P("%c", result[i]);
else {x=result[i];
P("%x", x);
}
if(base>36 && i!=(u32)r-1) P("_");
}
}
if(base!=0 && base%3==0) P("\n");
else P("#");
if(base>=2 && base<=36 && strlen(result)!=(u32)r)
P("Find Error base=%u", base);
}
P("\n");
R 0;
}

base=0 r=4294967295 #base=1 r=4294967295
#base=2 r=29 10010111101011110000110001000
#base=3 r=18 211011120100120120
base=4 r=15 102331132012020
#base=5 r=13 1122413311234#base=6 r=11 51322023240
base=7 r=11 10611560514#base=8 r=10 2275360610#base=9 r=9 734510516
base=10 r=9 318103944#base=11 r=9 15361A224#base=12 r=8 8A647B20
base=13 r=8 50B991B2#base=14 r=8 30367144#base=15 r=8 1CDD8049
base=16 r=8 12F5E188#base=17 r=7 D30B6BC#base=18 r=7 9664A56
base=19 r=7 6E8HB6G#base=20 r=7 4J82JH4#base=21 r=7 3EIDG2I
base=22 r=7 2HFKBC4#base=23 r=7 239GIG6#base=24 r=7 1FMINJ0
base=25 r=7 17E8G7J#base=26 r=7 10K2JP2#base=27 r=6 M4F9FF
base=28 r=6 IDEON4#base=29 r=6 FELQPF#base=30 r=6 D2LIOO
base=31 r=6 B3DQEH#base=32 r=6 9FBOC8#base=33 r=6 847NFF
base=34 r=6 701EEC#base=35 r=6 61YBO4#base=36 r=6 59E2KO
base=37 r=6 4_15_1b_2_9_21#base=38 r=6 4_0_15_7_16_10#base=39 r=6
3_14_13_17_c_f

base=40 r=6 3_4_a_e_26_18#base=41 r=6 2_1e_17_13_26_20#base=42 r=6
2_12_9_19_1_1
2
base=43 r=6 2_7_1_29_3_6#base=44 r=6 1_28_26_d_27_4#base=45 r=6
1_20_19_26_10_18

base=46 r=6 1_19_2_4_1f_6#base=47 r=6 1_12_8_2a_1c_1#base=48 r=6
1_b_2c_11_2d_18

base=49 r=6 1_6_8_29_5_b#base=50 r=6 1_0_2c_29_1c_2c#base=51 r=5
2f_1_2_20_c
base=52 r=5 2b_1a_11_33_1c#base=53 r=5 28_10_24_1d_b#base=54 r=5
25_16_9_7_2a
base=55 r=5 22_29_35_12_4#base=56 r=5 20_13_14_b_20#base=57 r=5
1e_7_27_e_36
base=58 r=5 1c_6_15_c_2c#base=59 r=5 1a_e_32_36_10#base=60 r=5
18_20_2a_c_18
base=61 r=5 16_3b_1b_32_2e#base=62 r=5 15_20_2d_16_30#base=63 r=5
14_c_b_7_3c
base=64 r=5 12_3d_1e_6_8#base=65 r=5 11_35_14_38_36#base=66 r=5
10_32_1e_28_30
base=67 r=5 f_34_2b_42_4#base=68 r=5 e_3b_2e_7_c#base=69 r=5
e_2_16_24_6
base=70 r=5 d_11_1d_c_4#base=71 r=5 c_24_37_18_11#base=72 r=5
b_3c_12_2e_18
base=73 r=5 b_e_33_48_14#base=74 r=5 a_2d_0_29_46#base=75 r=5
a_4_1_3c_45
base=76 r=5 9_28_31_1e_10#base=77 r=5 9_3_3c_10_4#base=78 r=5
8_2e_19_19_36
base=79 r=5 8_d_f_2_10#base=80 r=5 7_3d_17_3b_18#base=81 r=5
7_1f_2e_5_f
base=82 r=5 7_2_4c_3c_20#base=83 r=5 6_3a_1b_34_35#base=84 r=5
6_20_3a_3f_3c
base=85 r=5 6_7_53_13_1d#base=86 r=5 5_46_a_17_6#base=87 r=5
5_30_6_12_f
base=88 r=5 5_1a_45_29_30#base=89 r=5 5_6_14_32_37#base=90 r=5
4_4c_20_8_18
base=91 r=5 4_3a_b_40_43#base=92 r=5 4_28_2f_f_34#base=93 r=5
4_17_2c_19_30
base=94 r=5 4_6_5c_54_30#base=95 r=5 3_56_1_5c_36#base=96 r=5
3_47_34_2e_48
base=97 r=5 3_39_34_2e_a#base=98 r=5 3_2b_60_2_3c#base=99 r=5
3_1e_53_1b_f
base=100 r=5 3_12_a_27_2c#base=101 r=5 3_5_4b_3d_0#base=102 r=5
2_5f_4d_10_c
base=103 r=5 2_55_b_23_53#base=104 r=5 2_4a_52_33_50#base=105 r=5
2_40_52_65_27
base=106 r=5 2_37_9_e_40#base=107 r=5 2_2d_47_2e_6#base=108 r=5
2_24_38_1e_60
base=109 r=5 2_1b_45_12_58#base=110 r=5 2_12_6d_40_4#base=111 r=5
2_a_42_3_21
base=112 r=5 2_2_2f_5_58#base=113 r=5 1_6b_34_17_11#base=114 r=5
1_64_51_7_36
base=115 r=5 1_5e_12_1a_1d#base=116 r=5 1_57_5c_23_2c#base=117 r=5
1_51_47_6c_f
base=118 r=5 1_4b_47_56_10#base=119 r=5 1_45_5b_2d_2e#base=120 r=5
1_40_a_42_18
base=121 r=5 1_3a_43_70_1a#base=122 r=5 1_35_16_19_2e#base=123 r=5
1_2f_74_c_72
base=124 r=5 1_2a_68_2a_30#base=125 r=5 1_25_6c_51_45#base=126 r=5
1_21_2_62_3c
base=127 r=5 1_1c_25_3d_3b#base=128 r=5 1_17_57_43_8#base=129 r=5
1_13_17_57_6
base=130 r=5 1_e_66_5d_36#base=131 r=5 1_a_41_3a_32#base=132 r=5
1_6_28_56_30
base=133 r=5 1_2_1c_13_82#base=134 r=4 84_1b_64_4#base=135 r=4
81_27_23_45
base=136 r=4 7e_3e_47_50#base=137 r=4 7b_61_32_52#base=138 r=4
79_5_57_6
base=139 r=4 76_3e_15_51#base=140 r=4 73_81_6f_4#base=141 r=4
71_43_38_30
base=142 r=4 6f_d_76_58#base=143 r=4 6c_6f_8a_f#base=144 r=4
6a_4c_5f_18
base=145 r=4 68_31_73_2c#base=146 r=4 66_1f_24_14#base=147 r=4
64_14_84_3c
base=148 r=4 62_12_5e_90#base=149 r=4 60_18_35_77#base=150 r=4
5e_25_8e_90
base=151 r=4 5c_3b_2f_60#base=152 r=4 5a_58_35_10#base=153 r=4
58_7c_92_72
base=154 r=4 57_f_8_4#base=155 r=4 55_41_53_4f#base=156 r=4
53_7b_33_84
base=157 r=4 52_1f_36_79#base=158 r=4 50_66_50_10#base=159 r=4
4f_15_73_75
base=160 r=4 4d_69_95_68#base=161 r=4 4c_24_8_90#base=162 r=4
4a_85_2_60
base=163 r=4 49_49_79_99#base=164 r=4 48_13_1e_20#base=165 r=4
46_86_2a_72
base=166 r=4 45_59_96_88#base=167 r=4 44_32_c_6#base=168 r=4
43_e_73_90
base=169 r=4 41_98_76_91#base=170 r=4 40_7f_9_72#base=171 r=4
3f_69_76_a8
base=172 r=4 3e_58_61_5c#base=173 r=4 3d_4b_6b_15#base=174 r=4
3c_42_8b_66
base=175 r=4 3b_3e_b_90#base=176 r=4 3a_3d_40_88#base=177 r=4
39_40_74_4b
base=178 r=4 38_47_9e_90#base=179 r=4 37_53_5_1#base=180 r=4
36_62_4_18
base=181 r=4 35_74_97_40#base=182 r=4 34_8b_4d_9e#base=183 r=4
33_a5_8a_a8
base=184 r=4 33_b_91_90#base=185 r=4 32_2c_5a_90#base=186 r=4
31_50_98_30
base=187 r=4 30_78_8a_72#base=188 r=4 2f_a4_2a_30#base=189 r=4
2f_16_2c_7b
base=190 r=4 2e_47_8d_36#base=191 r=4 2d_7c_88_81#base=192 r=4
2c_b5_17_48
base=193 r=4 2c_2f_b3_ba#base=194 r=4 2b_6e_17_a#base=195 r=4
2a_af_7f_36
base=196 r=4 2a_30_63_3c#base=197 r=4 29_77_80_a4#base=198 r=4
28_c2_d_72
base=199 r=4 28_48_90_38#base=200 r=4 27_98_77_90#base=201 r=4
27_22_85_8a
base=202 r=4 26_77_b6_0#base=203 r=4 26_5_39_66#base=204 r=4
25_5f_a1_c
base=205 r=4 24_bd_51_72#base=206 r=4 24_50_11_ba#base=207 r=4
23_b2_ad_6
base=208 r=4 23_48_81_b8#base=209 r=4 22_b0_5a_5c#base=210 r=4
22_49_32_90
base=211 r=4 21_b6_6_85#base=212 r=4 21_51_a6_40#base=213 r=4
20_c3_66_9f
base=214 r=4 20_62_17_6#base=215 r=4 20_1_8a_31#base=216 r=4
1f_7a_f_60
base=217 r=4 1f_1c_51_ac#base=218 r=4 1e_99_76_58#base=219 r=4
1e_3e_79_5d
base=220 r=4 1d_c0_57_4#base=221 r=4 1d_68_b_50#base=222 r=4
1d_10_70_90
base=223 r=4 1c_98_a7_13#base=224 r=4 1c_43_aa_c8#base=225 r=4
1b_d0_78_45
base=226 r=4 1b_7e_b_82#base=227 r=4 1b_2c_43_da#base=228 r=4
1a_bf_3c_a8
base=229 r=4 1a_6f_d7_2c#base=230 r=4 1a_21_46_90#base=231 r=4
19_ba_52_51
base=232 r=4 19_6e_11_a0#base=233 r=4 19_22_69_e4#base=234 r=4
18_c1_70_84
base=235 r=4 18_78_21_bd#base=236 r=4 18_2f_66_10#base=237 r=4
17_d4_4f_ae
base=238 r=4 17_8d_c9_2e#base=239 r=4 17_47_e2_ca#base=240 r=4
17_2_99_18
base=241 r=4 16_ae_d9_5b#base=242 r=4 16_6b_b1_1a#base=243 r=4
16_29_1c_b1
base=244 r=4 15_db_c_a8#base=245 r=4 15_9a_80_6d#base=246 r=4
15_5a_81_72
base=247 r=4 15_1b_c_36#base=248 r=4 14_d4_15_30#base=249 r=4
14_96_9b_db
base=250 r=4 14_59_a5_c2#base=251 r=4 14_1d_2f_62#base=252 r=4
13_dd_31_3c
base=253 r=4 13_a2_aa_d5#base=254 r=4 13_68_9d_ba#base=255 r=4
13_2f_6_72
base=256 r=4294967295 #

Christopher Pisz

unread,
Jan 30, 2015, 11:27:29 AM1/30/15
to
On 1/30/2015 4:04 AM, Rosario193 wrote:
> On Thu, 29 Jan 2015 10:06:05 -0600, Christopher Pisz wrote:
>> On 1/29/2015 1:51 AM, Rosario193 wrote:
>>> On Tue, 27 Jan 2015 13:55:13 -0800 (PST), ghada glissa wrote:
>>>
>>>> Dear all,
>>>>
>>>> Is there any predefined function that convert in to hex or octet string in c++.
>>>>
>>>> Regards.
>>>
>>>
>>> #include <stdio.h>
>>> #define u8 unsigned char
>>>
>>> // return one pointer to pointer to arrays
>>> // one array is 8 bit partition of the argument a [swapped]
>>> // one as 4 bit partition of the argument a [swapped]
>>> // and conversion to digits they rapresent
>>> u8** intToHexChar(int a)
>>> {static u8 r0[ sizeof(int)+4];
>>> static u8 r1[2*sizeof(int)+4];
>>> static u8 *r[2]={r0, r1};
>>> static u8 hexStr[]="0123456789ABCDEF";
>>> unsigned i, aa;
>>>
>>> aa=a;
>>> for( i=sizeof(int)-1; ; --i )
>> You win the ugly code of the week award.
>>
>> What's wrong with #include <cstdio> ?
>
> i have one old C++ compiler that allow that

Upgrade your compiler to the year 2015. You are posting example code for
someone else to use.

>
>> Defining your own types as primitive types accomplishes what? One more
>> search someone has to do when looking at your code.
>
> here there is not "stdint.h" where would it be uint32_t
>
>> Naming all variables with a single or double letter rather than a
>> meaningful name.
>
> not for the words one always use

Every single variable you have in your code is a single or double
letter. No one knows wtf aa=a; means without having to study your entire
listing. employeePay = twoWeekSalary is immediately obvious. You are
writing C++ (supposedly) code for humans to read. You are not working in
matlab.


>> (void) parameters instead of (), for what purpose? I thought you were
>> trying to save keystrokes.
>
> i want sys call "int main(void)"
> and not call "int main()"

Why? Are you posting to a C++ newsgroup? Are you programming modern C++?

>> more than one statement on the same line
>
> instruction that are connected, i sometime write in the same line
>
>> Statements sometimes on same line with brackets, sometimes not,
>> sometimes no brackets at all. At least be consistent with ugly style.
>
> they call it readability...

That's hilarious.

Christopher Pisz

unread,
Jan 30, 2015, 11:32:33 AM1/30/15
to
On 1/30/2015 4:05 AM, Rosario193 wrote:
>SNIP
> #include <stdio.h>
> #include <string.h>
> #include <limits.h>
>
> #define u8 unsigned char
> #define u32 unsigned
> #define i32 int
> #define P printf
> #define F for
> #define R return
>
>SNIP

For Pete's sake, just stop. Go back to 1970.



Rosario193

unread,
Jan 30, 2015, 1:22:07 PM1/30/15
to
On Fri, 30 Jan 2015 11:05:13 +0100, Rosario193 wrote:
>// result ok if return one number > 0
>// that is the len of the string result even if there is some 0 in it
>i32 uToA(u8* rstr, u32 rstrsize, u32 argnumber, u32 base)
>{u32 i, j, k;
> u8 tmp[64];
> u8 str[64]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
>
> if(rstr==0) R -1;
> if(rstrsize > 0xFFFFFFF) R -1;
> rstr[0]=0;
> if(base<=1 || base>255) R -1;
>
> F( i=0, tmp[0]=0; argnumber; ++i)
> {tmp[i]=argnumber%base; argnumber/=base;}
> if(i+2>rstrsize) R -2;
> if(i!=0) --i;
> if(base<=36)
> {F(k=0, j=i; ;--j, ++k)
> {rstr[k]=str[tmp[j]];
> if(j==0) break;
> }
> }
> else {F(k=0, j=i; ;--j, ++k)
> {rstr[k]=tmp[j];
> if(j==0) break;
> }
> }

all these swap show that arabs representation for number, or little
endian
is the right way...
and we have only wrong

Rosario193

unread,
Jan 30, 2015, 1:54:35 PM1/30/15
to
On Fri, 30 Jan 2015 11:05:13 +0100, Rosario193 wrote:

some corrections... base 256 shoule be ok too...

#include <stdio.h>
#include <string.h>
#include <limits.h>

#define u8 unsigned char
#define u32 unsigned
#define i32 int
#define P printf
#define F for
#define R return


// result ok if return one number > 0
i32 uToA(u8* rstr, u32 rstrsize, u32 argnumber, u32 base)
{u32 i, j, k;
u8 tmp[64];
u8 str[64]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

if(rstr==0) R -1;
if(rstrsize > 0xFFFFFFF) R -1;
rstr[0]=0;
if(base<=1 || base>256) R -1;

F( i=0, tmp[0]=0; argnumber; ++i)
{tmp[i]=argnumber%base; argnumber/=base;}
if(i+2>rstrsize) R -2;
if(i!=0) --i;
if(base<=36)
{F(k=0, j=i; ;--j, ++k)
{rstr[k]=str[tmp[j]];
if(j==0) break;
}
}
else {F(k=0, j=i; ;--j, ++k)
{rstr[k]=tmp[j];
if(j==0) break;
}
}
++k; rstr[k]=0;
R k;
}


int main(void)
{u32 m=0x12f5e188, i, x, base;
i32 r;
u8 result[128]; // 0..126 127[0]

if(CHAR_BIT!=8 || sizeof(u32)!=4)
{P("this can not to run here\n"); R 0;}

F(base=0; base<=257; ++base)
{r=uToA(result, 128, m, base);
P("base=%u r=%u ", base, r);
if(r>0)
{F(i=0; i<(u32)r; ++i)
{if(base<=36) P("%c", result[i]);
else {x=result[i];
P("%u", x);

Rosario193

unread,
Jan 30, 2015, 2:04:30 PM1/30/15
to
On Thu, 29 Jan 2015 14:48:02 +0100, jak wrote:

>string BaseConv(int BytePos, void *Data, int Base)
>{
> unsigned char Byte = ((unsigned char *)Data)[BytePos];
> string Res = "";
> const char LT[] = "0123456789ABCDEF";
>
> if (Base > 1 && Base <= 16)
> for (; Byte > 0; Byte /= Base)
> Res = LT[Byte % Base] + Res;

here just above the compiler said one error, so not compile...
i think the reason why is the const above...
so i wrote it this

Res = (char) LT[Byte % Base] + Res;

and compile...

> return Res;
>}

Rosario193

unread,
Jan 30, 2015, 2:13:29 PM1/30/15
to
On Thu, 29 Jan 2015 14:48:02 +0100, jak wrote:

your program print:

Base 2:
1) 10001000
2) 11100001
3) 11110101
4) 10010

Premere un tasto per continuare . . .

Base 3:
1) 12001
2) 22100
3) 100002
4) 200

Premere un tasto per continuare . . .

Base 4:
1) 2020
2) 3201
3) 3311
4) 102

Premere un tasto per continuare . . .

Base 5:
1) 1021
2) 1400
3) 1440
4) 33

Premere un tasto per continuare . . .

Base 6:
1) 344
2) 1013
3) 1045
4) 30

Premere un tasto per continuare . . .

Base 7:
1) 253
2) 441
3) 500
4) 24

Premere un tasto per continuare . . .

Base 8:
1) 210
2) 341
3) 365
4) 22

Premere un tasto per continuare . . .

Base 9:
1) 161
2) 270
3) 302
4) 20

Premere un tasto per continuare . . .

Base 10:
1) 136
2) 225
3) 245
4) 18

Premere un tasto per continuare . . .

Base 11:
1) 114
2) 195
3) 203
4) 17

Premere un tasto per continuare . . .

Base 12:
1) B4
2) 169
3) 185
4) 16

Premere un tasto per continuare . . .

Base 13:
1) A6
2) 144
3) 15B
4) 15

Premere un tasto per continuare . . .

Base 14:
1) 9A
2) 121
3) 137
4) 14

Premere un tasto per continuare . . .

Base 15:
1) 91
2) 100
3) 115
4) 13

Premere un tasto per continuare . . .

Base 16:
1) 88
2) E1
3) F5
4) 12

Premere un tasto per continuare . . .

my print:
base=0 r=4294967295 #base=1 r=4294967295 #base=2 r=29
10010111101011110000110001
000#base=3 r=18 211011120100120120
base=4 r=15 102331132012020#base=5 r=13 1122413311234#base=6 r=11
51322023240
base=7 r=11 10611560514#base=8 r=10 2275360610#base=9 r=9 734510516
base=10 r=9 318103944#base=11 r=9 15361A224#base=12 r=8 8A647B20
base=13 r=8 50B991B2#base=14 r=8 30367144#base=15 r=8 1CDD8049
base=16 r=8 12F5E188

so result in other base except 16 swapped are different...

Rosario193

unread,
Jan 31, 2015, 3:09:17 AM1/31/15
to
On Fri, 30 Jan 2015 19:54:24 +0100, Rosario193 wrote:

better 2 functions...
nice execise exendible to biger numbers
possible there are errors...

#include <stdio.h>
#include <string.h>
#include <limits.h>

#define u8 unsigned char
#define u32 unsigned
#define i32 int
#define P printf
#define F for
#define R return

// convert u32 "argnumber" to
// u8 array of digits "rstr" in base "base" little endian form
// result ok if return one number > 0
// that is the len of the array "rstr" that contain "argnumber"
i32 u32ToA(u8* rstr, u32 rstrsize, u32 argnum, u32 base)
{u32 i, j, k;

if(rstr==0) R -1;
if(rstrsize>0xFFFFFFF|| rstrsize<2)
R -1;
rstr[0]=0;
if(base<=1 || base>256) R -1;
if(argnum==0){rstr[1]=0; R 1;}

F(i=0; argnum!=0 && i<rstrsize; ++i)
{rstr[i]=argnum%base; argnum/=base;}
if(i==rstrsize) R -1;
rstr[i]=0; /* the last 0 is not in the number digits */
R i;
}

i32 NumStrToDisplayStr(u8* rstr, u32 len)
{static u8 str[64]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
u32 tmp, i, j, a, b;

if(rstr==0) R -1;
if(len>0xFFFFFFF||len<1)
R -1;
F(i=0, j=len-1; i<j; ++i, --j)
{a=rstr[i]; b=rstr[j]; // we are not arabs so swap
if(a>35||b>35) R -1;
rstr[i]=str[b]; rstr[j]=str[a];
}
if(i==j) {a=rstr[i];
if(a>35) R -1;
rstr[i]=str[a];
}
R len;
}

int main(void)
{u32 m=0x12f5e188, i, x, base;
i32 r;
u8 result[128]; // 0..126 127[0]

if(CHAR_BIT!=8 || sizeof(u32)!=4)
{P("this can not to run here\n"); R 0;}

F(base=0; base<=257; ++base)
{r=u32ToA(result, 128, m, base);
P("base=%u r=%u ", base, r);
if(r>0)
{if(base<=36)
{if(NumStrToDisplayStr(result, (u32) r)==-1) R 0;}

Jorgen Grahn

unread,
Jan 31, 2015, 3:09:23 AM1/31/15
to
On Fri, 2015-01-30, Christopher Pisz wrote:
> On 1/30/2015 4:04 AM, Rosario193 wrote:
>> On Thu, 29 Jan 2015 10:06:05 -0600, Christopher Pisz wrote:
>>> On 1/29/2015 1:51 AM, Rosario193 wrote:
...
>>>> #include <stdio.h>
>>>> #define u8 unsigned char
>>>>
>>>> // return one pointer to pointer to arrays
>>>> // one array is 8 bit partition of the argument a [swapped]
>>>> // one as 4 bit partition of the argument a [swapped]
>>>> // and conversion to digits they rapresent
>>>> u8** intToHexChar(int a)
>>>> {static u8 r0[ sizeof(int)+4];
>>>> static u8 r1[2*sizeof(int)+4];
>>>> static u8 *r[2]={r0, r1};
>>>> static u8 hexStr[]="0123456789ABCDEF";
>>>> unsigned i, aa;
>>>>
>>>> aa=a;
>>>> for( i=sizeof(int)-1; ; --i )
...

>>> Naming all variables with a single or double letter rather than a
>>> meaningful name.
>>
>> not for the words one always use
>
> Every single variable you have in your code is a single or double
> letter. No one knows wtf aa=a; means without having to study your entire
> listing. employeePay = twoWeekSalary is immediately obvious. You are
> writing C++ (supposedly) code for humans to read. You are not working in
> matlab.

Well, I think it's ok with short, meaningless names if the scope is
small and the code is otherwise clear ... but I suspect
intToHexChar(int) above would not be helped by better naming alone.

/Jorgen

PS I haven't followed this thread, so I don't understand what's wrong
with a std::sprintf(), wrapped with a std::string. Or any of the
other obvious solutions which don't require duplicating that logic
manually.

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Rosario193

unread,
Jan 31, 2015, 3:13:22 AM1/31/15
to
On Sat, 31 Jan 2015 09:09:09 +0100, Rosario193
<Ros...@invalid.invalid> wrote:


>i32 NumStrToDisplayStr(u8* rstr, u32 len)
>{static u8 str[64]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

if someone use static array in a function... Is that function not
multithread? is it possible something goes wrong if 2 cpu
read in the same time the same position of mem?

Geoff

unread,
Jan 31, 2015, 9:52:03 AM1/31/15
to
On Sat, 31 Jan 2015 09:09:09 +0100, Rosario193
<Ros...@invalid.invalid> wrote:

>possible there are errors...

Does the fact you are posting erroneous code, fixing it, fixing the
fixes and refactoring the fixed fixes not tell you there is something
wrong with your method and style?

Rosario193

unread,
Jan 31, 2015, 10:50:24 AM1/31/15
to
yes it is a little difficult write error free code...
expecially in numeric algos...

Rosario193

unread,
Jan 31, 2015, 10:53:52 AM1/31/15
to
On Sat, 31 Jan 2015 06:51:50 -0800, Geoff <ge...@invalid.invalid>
wrote:
yes why have to write routines in this f... group...

Rosario193

unread,
Jan 31, 2015, 10:56:48 AM1/31/15
to
the comunication is 1 to many so post and its reaply can be from the
same person

Rosario193

unread,
Jan 31, 2015, 11:46:11 AM1/31/15
to
yes why have I to write routines in this f... group???

Geoff

unread,
Jan 31, 2015, 12:32:11 PM1/31/15
to
On Sat, 31 Jan 2015 17:46:02 +0100, Rosario193
Yes, indeed.

Geoff

unread,
Jan 31, 2015, 12:50:35 PM1/31/15
to
On Sat, 31 Jan 2015 16:56:40 +0100, Rosario193
<Ros...@invalid.invalid> wrote:

>
>the comunication is 1 to many so post and its reaply can be from the
>same person

When you are replying to a specific person, it is logical, proper and
courteous to reply to that post, not to your own post from an earlier
time.

Christopher Pisz

unread,
Feb 2, 2015, 1:09:44 PM2/2/15
to
On 1/31/2015 2:09 AM, Jorgen Grahn wrote:
> On Fri, 2015-01-30, Christopher Pisz wrote:
SNIP
>> Every single variable you have in your code is a single or double
>> letter. No one knows wtf aa=a; means without having to study your entire
>> listing. employeePay = twoWeekSalary is immediately obvious. You are
>> writing C++ (supposedly) code for humans to read. You are not working in
>> matlab.
>
> Well, I think it's ok with short, meaningless names if the scope is
> small and the code is otherwise clear ... but I suspect
> intToHexChar(int) above would not be helped by better naming alone.
>
> /Jorgen
>
> PS I haven't followed this thread, so I don't understand what's wrong
> with a std::sprintf(), wrapped with a std::string. Or any of the
> other obvious solutions which don't require duplicating that logic
> manually.
>

Evidently, the OP wanted to print each byte seperately, rather than the
contents of a variable, but nothing is wrong with sprintf or
stringstream for the task if you set it up first.

The topic got sidetracked when this Rosario person posted numerous
horrible solutions, tried to fix the, and posted more horrible solutions.


0 new messages