MCPcopy Create free account
hub / github.com/TASEmulators/fceux / BytesToString

Function BytesToString

src/utils/xstring.cpp:206–258  ·  view source on GitHub ↗

Converts the provided data to a string in a standard, user-friendly, round-trippable format

Source from the content-addressed store, hash-verified

204
205///Converts the provided data to a string in a standard, user-friendly, round-trippable format
206std::string BytesToString(const void* data, int len)
207{
208 char temp[16];
209 if(len==1) {
210 sprintf(temp,"%d",*(const unsigned char*)data);
211 return temp;
212 } else if(len==2) {
213 sprintf(temp,"%d",*(const unsigned short*)data);
214 return temp;
215 } else if(len==4) {
216 sprintf(temp,"%d",*(const unsigned int*)data);
217 return temp;
218 }
219
220 std::string ret;
221 if(1) // use base64
222 {
223 const unsigned char* src = (const unsigned char*)data;
224 ret = "base64:";
225 for(int n; len > 0; len -= n)
226 {
227 unsigned char input[3] = {0,0,0};
228 for(n=0; n<3 && n<len; ++n)
229 input[n] = *src++;
230 unsigned char output[4] =
231 {
232 Base64Table[ input[0] >> 2 ],
233 Base64Table[ ((input[0] & 0x03) << 4) | (input[1] >> 4) ],
234 static_cast<unsigned char> (n<2 ? '=' : Base64Table[ ((input[1] & 0x0F) << 2) | (input[2] >> 6) ]),
235 static_cast<unsigned char> (n<3 ? '=' : Base64Table[ input[2] & 0x3F ])
236 };
237 ret.append(output, output+4);
238 }
239 }
240 else // use hex
241 {
242 ret.resize(len*2+2);
243 ret[0] = '0';
244 ret[1] = 'x';
245 for(int i=0;i<len;i++)
246 {
247 int a = (((const unsigned char*)data)[i]>>4);
248 int b = (((const unsigned char*)data)[i])&15;
249 if(a>9) a += 'A'-10;
250 else a += '0';
251 if(b>9) b += 'A'-10;
252 else b += '0';
253 ret[2+i*2] = a;
254 ret[2+i*2+1] = b;
255 }
256 }
257 return ret;
258}
259
260///returns -1 if this is not a hex string
261int HexStringToBytesLength(const std::string& str)

Callers 3

dumpMethod · 0.85
rom_gethashFunction · 0.85
cfg_OldToNewFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected